我有一本可在我的机器上准备3种不同游民的剧本,所以我创建了一个角色来创建这个游民。我找不到正确的语法。看起来像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 }
...
再次阅读您的问题,我注意到您实际上没有提到它必须是某种循环。也许用不同的参数应用相同角色的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
等变量
实际上,您不能将循环直接应用于角色。循环用于执行任务。
但是角色可以采用您可以在角色中使用的任何参数。这与通过不同参数将角色应用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 }}"
定义一个变量并在您的角色中使用它。
- 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
我知道这是一个古老的问题,但是您现在可以使用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