多次创建相同的角色,但具有不同的项目 [英] Creating multiple times the same role, but with different items
问题描述
我有一个剧本,在我的机器中准备了 3 个不同的 Vagrant,所以我创建了一个角色来创建这个 Vagrant.我没有找到正确的语法.看起来 roles
不是模块,所以我不没有所有选项,只有教程.
I have a playbook that prepare 3 different Vagrants in my machine, so I have create a role that create this Vagrant. I don't find the correct syntax. It looks like roles
is not a module, so I don't have all options, only tutorials.
剧本文件:
- 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 }
以及 vagrant 角色中的任务
And the tasks in the vagrant role
- 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
确实有效,但它甚至会伤害我祖母的眼睛
The error is 'item.name' undefined. It does work using with_items
inside the role, but it would even hurt my grandmother 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 }
...
推荐答案
再次阅读您的问题,我注意到您实际上并没有提到它必须是某种循环.也许应用 3 次不同参数的相同角色适合您的需求:
Reading your question again, I noticed you did actually not mention it has to be some kind of loop. Maybe applying 3 times the same role with different parameters suits your needs:
- 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
等
Then in your role you can use the vars index
, ip
etc.
这篇关于多次创建相同的角色,但具有不同的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!