Inevitable - iterate over several hashes in one game

I have a data structure in a json file that is loaded into an additional variables file and looks something like this:



These three sections list files that are downloaded from different sources.

They come from different sources and some of them are known to be different types (tar files come from s3, zip from local network and ftp server), so I have three different pieces to handle each download. But at the end of every game, there is a file whose name is the value full_name

(all entries in these hashes will have an item named full_name

) in the directory /home/ubuntu/downloaded_files


Once loaded, they are all treated the same way. Right now I have three separate pieces that do the same thing but repeat over a different hash, like this:

- name: process files from ftp
  shell: some_command -f /home/ubuntu/downloaded_files/{{ item.value.full_name }}
  with_dict: files_from_ftp | default({})

- name: process files from network
  shell: some_command -f /home/ubuntu/downloaded_files/{{ item.value.full_name }}
  with_dict: files_from_network | default({})

- name: process files from s3
  shell: some_command -f /home/ubuntu/downloaded_files/{{ item.value.full_name }}
  with_dict: files_from_s3 | default({})


I would like to have one game that handles all these hashes, but I'm not sure if this is possible. I cannot understand the syntax. I'm thinking of hypothetical (and probably syntactically invalid) code:

- name: process all downloaded files
  shell: some_command -f /home/ubuntu/downloaded_files/{{ item.value.full_name }}
  with_dict: ( files_from_s3 | default({}) )
             and ( files_from_ftp | default({}) )
             and ( files_from_network | default({}) )


In this code example, I imagine I with_dict

will iterate over the concatenation of three other hashes.

It makes sense? Is this what I want to do, even possible? It would be easier to change the pieces than the data structure, since the data structure is in this situation.

source to share

1 answer

I personally think you should go from using a file hash to a list of hashes. However:

In Ansible 2.0, you can do hash_merge


- shell: echo "{{ item.value.full_name }}"
  with_dict: files_from_s3|hash_merge(files_from_ftp)|hash_merge(files_from_network)


Which you can already customize by adding this hash_merge filter filter



All Articles