Creating several times the same role, but with different elements

I have a tutorial that prepares 3 different strollers on my machine, so I created a role that creates this stroller. I did not find the correct syntax. It roles doesn't seem to be a module , so I don't have all the parameters, just the lessons.

file to play:

- 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 tasks as vagrants

- 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

Error: "item.name" undefined. It works using with_itemsinside roles, but it will even hurt my grandmother's 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 }
...
+4
source share
3 answers

, , , - . , 3- :

- 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

vars index, ip ..

+4

.

- 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

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
+1

, . .

, . , 3 . . , :

:

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

:

- 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

, , . Ansible 2 , . :

- 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

main.yml :

- include: other-file.yml
  with_items: instances
  instance: "{{ item }}"
+1

Source: https://habr.com/ru/post/1627024/


All Articles