2016-02-02 19 views
3

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

回答

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 
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 }}" 
3

重读你的问题,我注意到你这样做其实并不提及它必须是某种循环。也许用不同的参数应用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 

然后在你的角色可以使用瓦尔indexip

+0

聪明。原本我以为会有一个范围问题。但是,这工作完美。谢谢 – Banjocat

相关问题