Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating multiple times the same role, but with different items

Tags:

ansible

I have a playbook that prepare 3 different Vagrants in my machine, so I have create a role that create this Vagrant. I don't find the correct syntax. It looks like roles is not a module, so I don't have all options, only tutorials.

playbook file :

- hosts: localhost
  connection: local

  roles :
    - role: vagrant
      with_items:
        - {index: 1, ip: 192.168.222.1, name: mongo1, user: nicorama }
        - {index: 2, ip: 192.168.222.2, name: mongo2, user: nicorama }
        - {index: 3, ip: 192.168.222.3, name: mongo3, user: nicorama }

And the tasks in the vagrant role

- file: path=/linux/{{item.name}} state=directory  owner={{item.user}} group={{item.user}} mode="u=rwx,g=rwx,o=rx"
- file: src=playbook.yml dest=/linux/{{item.name}}
- template: src=Vagrantfile dest=/linux/{{item.name}}/Vagrantfile

The error is 'item.name' undefined. It does work using with_items inside the role, but it would even hurt my grandmother eyes

- file: path=/linux/{{item.name}} state=directory  owner={{item.user}} group={{item.user}} mode="u=rwx,g=rwx,o=rx"
  with_items:
        - {index: 1, ip: 192.168.222.1, name: mongo1, user: nicorama }
        - {index: 2, ip: 192.168.222.2, name: mongo2, user: nicorama }
        - {index: 3, ip: 192.168.222.3, name: mongo3, user: nicorama }
- copy: src=playbook.yml dest=/linux/{{item.name}}/playbook.yml
  with_items:
        - {index: 1, ip: 192.168.222.1, name: mongo1, user: nicorama }
        - {index: 2, ip: 192.168.222.2, name: mongo2, user: nicorama }
        - {index: 3, ip: 192.168.222.3, name: mongo3, user: nicorama }
...
like image 940
Nicolas Zozol Avatar asked Feb 02 '16 13:02

Nicolas Zozol


2 Answers

Indeed, you can not apply loops directly to roles. Loops are meant for tasks.

But roles can take any parameters which you can use in your role. This is not the same as applying the role 3 times with different parameters. But in your role you can handle all the looping. If that's an option, then let's re-model it a bit:

The playbook:

- hosts: localhost
  connection: local

  roles :
    - role: vagrant
      instances:
        - {index: 1, ip: 192.168.222.1, name: mongo1, user: nicorama }
        - {index: 2, ip: 192.168.222.2, name: mongo2, user: nicorama }
        - {index: 3, ip: 192.168.222.3, name: mongo3, user: nicorama }

The tasks of your role:

- file: path=/linux/{{item.name}} state=directory  owner={{item.user}} group={{item.user}} mode="u=rwx,g=rwx,o=rx"
  with_items: instances

- file: src=playbook.yml dest=/linux/{{item.name}}
  with_items: instances

- template: src=Vagrantfile dest=/linux/{{item.name}}/Vagrantfile
  with_items: instances

Of course, this is extremely uncomfortable if you have to loop on every single task. In Ansible 2 it is (again) possible to loop over includes and this might get handy here. You can move all your tasks into a separate file:

- file: path=/linux/{{instance.name}} state=directory  owner={{instance.user}} group={{instance.user}} mode="u=rwx,g=rwx,o=rx"
- file: src=playbook.yml dest=/linux/{{instance.name}}
- template: src=Vagrantfile dest=/linux/{{instance.name}}/Vagrantfile

Then in your main.yml you'd only have this task:

- include: other-file.yml
  with_items: instances
  instance: "{{ item }}"
like image 193
udondan Avatar answered Oct 31 '22 18:10

udondan


I know this is an ancient question, but you can now use loop with the newer include_role syntax:

- hosts: localhost
  connection: local

  tasks:
    - include_role: 
        name: vagrant
      vars:
        index: "{{ vagrant_vars.index }}"
        ip: "{{ vagrant_vars.ip }}"
        name: "{{ vagrant_vars.name }}"
        user: "{{ vagrant_vars.user }}"
      loop:
        - {index: 1, ip: 192.168.222.1, name: mongo1, user: nicorama }
        - {index: 2, ip: 192.168.222.2, name: mongo2, user: nicorama }
        - {index: 3, ip: 192.168.222.3, name: mongo3, user: nicorama }
      loop_control: 
        loop_var: vagrant_vars
like image 4
serversorcery Avatar answered Oct 31 '22 18:10

serversorcery