Johnny Gillespie Johnny Gillespie - 3 years ago 210
YAML Question

Creating multiple directories from vars in ansible vars file

I am looking to create a multitude of directories on my given server by passing in vars from an ansible .yml vars file. I am currently trying to use Jinja templating to pass the directory names in.
The vars file looks like this;

type: PBSTP
accept: 1010
type: PBSTP
connect: 1010
type: FXSIM
type: FXSIM
type: PBSTP
connect: 32620
accept: 33620

And I wish to create a directory for each customer name listed. So ideally I will end up with a directory in a given folder for each 'TYPOBANK','MAINBANK' etc.
The way I am trying to this at the minute is as follows in my playbook;

- include_vars:
file: /home/vagrant/stunnelSimAnsPractice/roles/ns16/vars/customers.yml
name: customers
- file:
path: /home/vagrant/stunnelSimAnsPractice/roles/ns16/sessions/{%for cust, config in customers.items() %}{{cust}}{% endfor %}
state: directory

But this outputs one directory with all the customer names combinded, as shown below. Any suggestions on how to create individual directories for each customer? Thank you

├── sessions

Answer Source

You can't loop inside a single parameter in Ansible.

Use loops as Ansible was designed to:

- file:
    path: /home/vagrant/stunnelSimAnsPractice/roles/ns16/sessions/{{ item }}
    state: directory
  with_items:     ### and here you need to put the list containing the directory names

Unfortunately you did not provide the full definition of variables, so you need to customise the code to suite your needs.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download