Creating multiple times the same role, but with different items

99封情书 提交于 2019-12-05 11:07:06

Reading your question again, I noticed you did actually not mention it has to be some kind of loop. Maybe applying 3 times the same role with different parameters suits your needs:

- hosts: localhost
  connection: local

  roles :

    - role: vagrant
      index: 1
      ip: 192.168.222.1
      name: mongo1
      user: nicorama

    - role: vagrant
      index: 2,
      ip: 192.168.222.2
      name: mongo2
      user: nicorama

    - role: vagrant
      index: 3
      ip: 192.168.222.3
      name: mongo3
      user: nicorama

Then in your role you can use the vars index, ip etc.

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 }}"

Define a variable and use it inside your role.

- hosts: localhost
  connection: local
  vars:
    my_list:
      - {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 }
  roles :
    - vagrant

Use the variable inside your playbook using with_items:

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

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

- template: src=Vagrantfile dest=/linux/{{item.name}}/Vagrantfil
  with_items: my_list
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!