多次创建相同的角色,但使用不同的项目 [英] Creating multiple times the same role, but with different items

查看:47
本文介绍了多次创建相同的角色,但使用不同的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个剧本,可以在我的机器中准备3种不同的流浪者",所以我创建了一个角色来创建流浪者".我找不到正确的语法.看起来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 }

以及那些无业游民的任务

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

然后按照您的角色,可以使用变量indexip等.

Then in your role you can use the vars index, ip etc.

这篇关于多次创建相同的角色,但使用不同的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆