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
• 14,080 points
138 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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
• 36,650 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
• 56,140 points
131 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,220 points
901 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,220 points
178 views
0 votes
1 answer

What all can I do with Ansible?

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

answered Jan 8 in Ansible by Takeshi
17 views
+13 votes
2 answers
0 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
• 36,650 points
494 views
0 votes
1 answer

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.