Ansible 多次创建同一角色,但使用不同的项目
我有一个剧本,在我的机器里准备了3个不同的流浪者,所以我创造了一个角色,创造了这个流浪者。我找不到正确的语法。它看起来像是Ansible 多次创建同一角色,但使用不同的项目,ansible,ansible-playbook,Ansible,Ansible Playbook,我有一个剧本,在我的机器里准备了3个不同的流浪者,所以我创造了一个角色,创造了这个流浪者。我找不到正确的语法。它看起来像是角色,所以我没有所有选项,只有教程 剧本文件: - hosts: localhost connection: local roles : - role: vagrant with_items: - {index: 1, ip: 192.168.222.1, name: mongo1, user: nicorama }
角色
,所以我没有所有选项,只有教程
剧本文件:
- 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”未定义。在角色内部使用和_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 }
...
定义一个变量并在您的角色中使用它
- 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
使用带有项目的在剧本中使用变量:
- 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次不同。但在你的角色中,你可以处理所有的循环。如果这是一个选项,那么让我们对其重新建模:
剧本:
- 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 }}"
再次阅读你的问题,我注意到你实际上没有提到它必须是某种循环。也许使用不同参数应用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索引
,ip
等。我知道这是一个古老的问题,但您现在可以使用:
聪明。最初我认为会有一个范围问题。但这是完美的。ThanksIt最好给出一个例子,说明这个问题的循环是什么样子的。
- 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