Passing complex data to included tasks

When wanting to pass complex data to included tasks, such as a list or hash, an alternative syntax can be used when including the file. Let's repeat the last scenario, only this time instead of including the task file twice, we'll include it once and pass a hash of the paths and files in. First, we'll recreate the files.yaml file:

--- 
- name: create leading path 
  file: 
    path: "{{ item.value.path }}" 
    state: directory 
  with_dict: "{{ files }}" 
 
- name: touch the file 
  file: 
    path: "{{ item.value.path + '/' + item.key }}" 
    state: touch 
  with_dict: "{{ files }}" 

Now we'll alter our includer.yaml playbook to provide the files hash in a single include statement:

---
- name: touch files
hosts: localhost
gather_facts: false

tasks:
- include: files.yaml
vars:
files:
herp:
path: /tmp/foo
derp:
path: /tmp/foo

If we run this new playbook and task file, we should see similar but slightly different output, the end result of which is the /tmp/foo directory already in place and the two herp and derp files being created as empty files (touched) within:

Using this manner of passing in a hash of data allows for the growth of the set of things created without having to grow the number of include statements in the main playbook.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.18.220.243