camdixon
camdixon

Reputation: 892

Ansible - Create multiple config files using the same template in a role

I have found a similar question: Creating multiple times the same role, but with different items but I do not know how I can use it with the group_vars directory structure.

My Question

How can I use a role with a template inside of it to create multiple config files with different names and values? I also am using the group_vars directory which exists on the same level as the playbook I run with the hosts it runs.

Here is my current sample playbook

---
- hosts: emea-stg-web
  vars_files:
    - group_vars/ssh_user.yml
  remote_user: "{{ ssh_user }}"
  become_user: root

  roles:
    - nginx-install
    - php5.6-install

How can I have each "item" in a role use different variable values. Such as a variable "name" can have ["test 1", "test 2", "test 3"] as it iterates through? Can I then store these values in a file?

---
- hosts: emea-stg-web
  vars_files:
    - group_vars/ssh_user.yml
  remote_user: "{{ ssh_user }}"
  become_user: root

  roles:
    - nginx-install
      with_items:
      - test1
      - test2
      - test3
    - php5.6-install

Upvotes: 0

Views: 3442

Answers (1)

techraf
techraf

Reputation: 68639

Have a look at the include_role module with the vars_from parameter:

tasks:
  - include_role:
      role: nginx-install
      vars_from: "{{ item }}"
    with_items:
      - vars_file1
      - vars_file2

And do not use group_vars directory for storing arbitrary vars files, it's intended to store vars files for host groups.

Upvotes: 6

Related Questions