Skip to content

Instantly share code, notes, and snippets.

@halberom
Last active October 17, 2017 23:16
Show Gist options
  • Save halberom/c2e5948a5811bd839a7f0bd31a344d9a to your computer and use it in GitHub Desktop.
Save halberom/c2e5948a5811bd839a7f0bd31a344d9a to your computer and use it in GitHub Desktop.
ansible - copy module returns different keys depending on change status - inconsistent
---
- 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'
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
@halberom
Copy link
Author

halberom commented Jun 7, 2017

Why does this matter?

I want to use assemble against a bunch of generated files, but only those generated.

- name: check contents of the temp dir
  find:
    paths: "{{ temp_dir }}"
  register: find_results
  changed_when: False

- name: configure the sections
  template:
    src: "{{ item.src }}.j2"
    dest: "{{ temp_dir }}/{{ item.order }}_{{ item.src }}.txt"
  with_items: "{{ templates }}"
  register: generated_templates

- name: remove any obsolete files
  file:
    path: "{{ item }}"
    state: absent
  # well this is awkward - dest or path, or both?
  with_items: "{{ find_results.files|map(attribute='path') | difference(generated_templates.results|map(attribute='dest')) }}"

- name: do something assembleeish
  assemble:
    ...

@halberom
Copy link
Author

halberom commented Aug 4, 2017

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