Last active
October 17, 2017 23:16
-
-
Save halberom/c2e5948a5811bd839a7f0bd31a344d9a to your computer and use it in GitHub Desktop.
ansible - copy module returns different keys depending on change status - inconsistent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: localhost | |
gather_facts: False | |
connection: local | |
vars: | |
foo: bar | |
tasks: | |
- name: first time - dest is changed, 'dest' is returned in results | |
# yes I'm using template, not copy, but the template action plugin calls the copy module... | |
template: | |
src: 01_test.j2 | |
dest: /tmp/foobar | |
register: template_result | |
- debug: | |
var: template_result | |
- name: second time - nothing is changed, 'path' is returned in results | |
template: | |
src: 01_test.j2 | |
dest: /tmp/foobar | |
register: template_result | |
- debug: | |
var: template_result | |
- debug: | |
msg: "To do a conditional, I need to handle both scenarios now - fugly" | |
when: template_result.get('path', template_result.get('dest')) == '/tmp/foobar' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{ foo }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PLAY [localhost] *************************************************************************************************************************************************************************************************************************************************************** | |
TASK [first time - dest is changed, 'dest' is returned in results] ************************************************************************************************************************************************************************************************************* | |
changed: [localhost] | |
TASK [debug] ******************************************************************************************************************************************************************************************************************************************************************* | |
ok: [localhost] => { | |
"changed": false, | |
"template_result": { | |
"changed": true, | |
"checksum": "e242ed3bffccdf271b7fbaf34ed72d089537b42f", | |
"dest": "/tmp/foobar", | |
"gid": 137676021, | |
"group": "137676021", | |
"md5sum": "c157a79031e1c40f85931829bc5fc552", | |
"mode": "0644", | |
"owner": "lynchg", | |
"size": 4, | |
"src": "/Users/lynchg/.ansible/tmp/ansible-tmp-1496845609.21-59671882598024/source", | |
"state": "file", | |
"uid": 1920260155 | |
} | |
} | |
TASK [second time - nothing is changed, 'path' is returned in results] ********************************************************************************************************************************************************************************************************* | |
ok: [localhost] | |
TASK [debug] ******************************************************************************************************************************************************************************************************************************************************************* | |
ok: [localhost] => { | |
"changed": false, | |
"template_result": { | |
"changed": false, | |
"diff": { | |
"after": { | |
"path": "/tmp/foobar" | |
}, | |
"before": { | |
"path": "/tmp/foobar" | |
} | |
}, | |
"gid": 137676021, | |
"group": "137676021", | |
"mode": "0644", | |
"owner": "lynchg", | |
"path": "/tmp/foobar", | |
"size": 4, | |
"state": "file", | |
"uid": 1920260155 | |
} | |
} | |
TASK [debug] ******************************************************************************************************************************************************************************************************************************************************************* | |
ok: [localhost] => { | |
"changed": false, | |
"msg": "To do a conditional, I need to handle both scenarios now - fugly" | |
} | |
PLAY RECAP ********************************************************************************************************************************************************************************************************************************************************************* | |
localhost : ok=5 changed=1 unreachable=0 failed=0 |
It would be nice if the bug I filed were fixed, but in the meantime, the workaround is
- name: purge invalid files step 1 - generate a list of expected filenames
set_fact:
expected_files: "{{ expected_files | default([]) + [item.order + '_' + item.src] }}"
with_items: "{{ templates }}"
run_once: True
- name: purge invalid files step 2 - check contents of the temp dir
find:
paths: "{{ temp_dir }}"
register: find_results
changed_when: False
- name: purge invalid files step 3 - remove any files that aren't in the expected list
file:
path: "{{ temp_dir }}/{{ item }}"
state: absent
with_items: "{{ find_results.files | map(attribute='path') | map('basename') | list | difference(expected_files) }}"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why does this matter?
I want to use assemble against a bunch of generated files, but only those generated.