多次创建相同的角色,但项目不同

Nic*_*zol 5 ansible ansible-playbook

我有一本可在我的机器上准备3种不同游民的剧本,所以我创建了一个角色来创建此游民。我找不到正确的语法。看来这roles 不是一个模块,所以我没有所有选择,只有教程。

剧本文件:

- 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 }
Run Code Online (Sandbox Code Playgroud)

和任务中的游民角色

- 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
Run Code Online (Sandbox Code Playgroud)

错误是未定义的“ item.name”。它确实可以with_items在角色内部使用,但是甚至会伤害我祖母的眼睛

- 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 }
...
Run Code Online (Sandbox Code Playgroud)

udo*_*dan 5

再次阅读您的问题,我注意到您实际上没有提到它必须是某种循环。也许应用 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
Run Code Online (Sandbox Code Playgroud)

然后在你的角色可以使用增值经销商indexip等等。