Using when conditional with set_facts and with_items

494 views
Skip to first unread message

Jason Malobicky

unread,
Feb 25, 2016, 3:35:47 PM2/25/16
to Ansible Project
Hello,

Using ansible v1.9.4, I am having an issue using set_facts if combining with_items and when conditional. I am tackling a new use case for our environment and have not tested with this any other version, since 1.9.4 is the production version.

When 'when' is not used, the 'all_files' fact lists the basename properly.

---

- hosts: 127.0.0.1
  connection: local
  gather_facts: no
  tasks:
    - set_fact:
        files_fact: "{{ item | basename }}"
      with_items: files
#      when: "{{ item | search('^/tmp/foo.*') }}"
      register: files_result

    - debug:
        msg: "{{ files_result }}"

    - set_fact:
        all_files: "{{ files_result.results | map(attribute='ansible_facts.files_fact') | list }}"

  vars:
    files:
      - /tmp
      - /tmp/foo1
      - /tmp/foo2
      - /tmp/bar1

TASK: [set_fact ] ************************************************************* 
ok: [127.0.0.1] => {"ansible_facts": {"all_files": ["tmp", "foo1", "foo2", "bar1"]}}

When the 'when' is used to match the search string, for values that are 'skipped' due positive match, the files_result.results contains for the matching items

{
    "changed": false, 
    "skipped": true
}

Thus causing an error since the results list does not contain 'ansible_facts'

TASK: [set_fact ] ************************************************************* 
fatal: [127.0.0.1] => One or more undefined variables: 'dict object' has no attribute 'ansible_facts'


The {{ files }} variable is dynamic in my use case but I have been testing with static values as listed.

Thanks
Jason

Reply all
Reply to author
Forward
0 new messages