我有一个在我的机器上准备3个不同的Vagrant的剧本,所以我创建了一个创建这个Vagrant的角色。我找不到正确的语法。它看起来像roles
is not a module,所以我没有所有选项,只有教程。
剧本文件:
- 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 }
和流浪者角色的任务
- 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
错误是' 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 }
...
答案 0 :(得分:4)
再次阅读你的问题,我注意到你确实没有提到它必须是某种循环。也许应用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
然后在您的角色中,您可以使用变量index
,ip
等。
答案 1 :(得分:2)
我知道这是一个古老的问题,但是您现在可以使用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
答案 2 :(得分:1)
定义变量并在角色中使用它。
- 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
答案 3 :(得分: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 }}"