Ansible iterate over hash with condition

0 votes

I have a fixed structure of vars that I need to use. Those files should already be on the destination server. There one specific case, where someone adds a template entry for file which is not on the destination server.

 I would like to secure my playbook against this case. The biggest issue here for me is finding a way of either writing it in one with_subelements statement or iterating with_subelements over 2 tasks.

My vars structure

artifacts:
  - name: demo
    version: v1
 templates:
      - source: "/opt/source/file.txt"
        destination: "/opt/destination/file.txt"

 I would now like to iterate over this structure as in the next snippet:

- name: "Archive files"
  synchronize:
    src: "{{ item.1.destination }}"
    dest: "/some/backup/dir/"
    archive: yes
  delegate_to: "{{ inventory_hostname }}"
  when: item[1].destination.isfile
  with_subelements:
    - "{{ artifacts }}"
    - templates
Jul 23, 2018 in Ansible by Hannah
• 18,570 points
1,121 views

1 answer to this question.

0 votes

Your when statement is wrong.

you either split it into two tasks like stat + archive...

Or just add --ignore-missing-args if you don't care about missing files, like:

- name: "Archive files"
  synchronize:
    src: "{{ item.1.destination }}"
    dest: "/some/backup/dir/"
    archive: yes
    rsync_opts: ['--ignore-missing-args']
  delegate_to: "{{ inventory_hostname }}"
  with_subelements:
    - "{{ artifacts }}"
    - templates
answered Jul 23, 2018 by Kalgi
• 52,360 points

Related Questions In Ansible

0 votes
2 answers

Issue creating AWS VPC with Ansible

You can use the details given in ...READ MORE

answered Aug 16, 2018 in Ansible by Priyaj
• 58,090 points
931 views
0 votes
1 answer

How to use Ansible git module pull a branch with local changes?

You cannot achieve it using the git ...READ MORE

answered Jul 9, 2018 in Ansible by Atul
• 10,240 points
6,551 views
0 votes
1 answer

Testing server response with timeout using ansible

Got it working using wait_for and until ...READ MORE

answered Aug 7, 2018 in Ansible by Atul
• 10,240 points
2,410 views
0 votes
1 answer

What all can I do with Ansible?

Ansible is capable of doing a lot ...READ MORE

answered Jan 8, 2019 in Ansible by Takeshi
523 views
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
3,516 views
+2 votes
1 answer
0 votes
1 answer

One loop over multiple Ansible tasks

With_items has unfortunately stopped but it’s coming ...READ MORE

answered Jul 23, 2018 in Ansible by Kalgi
• 52,360 points
3,111 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP