如何在一个脚本中为不同的剧本指定不同的主机

据我所知,每个剧本都需要一个主机条目。 我想知道,如果我创build了一个包含其他剧本的容器剧本,我可以参数化每个剧本包含的主机。 所以像

--- - include playbook_1.yml hosts: tag_postgres - include: playbook_2.yml hosts: tag_rabbitmq 

我可以把所有不同的剧本放在一个脚本中,然后调用,但是这样我就无法从一个剧本中重用一些set_fact到另一个剧本,因此有很多任务重复。

问题的另一个必然结果是,我可以在亚马逊上推出ec2s

  hosts: localhost 

并启动ec2s的configuration,这基本上将configuration来自-i ec2.py的库存

并具有指定的主机

  hosts: tag_<some_tag> 

通过相同的剧本或一套包含的剧本(不同的angular色)

更新:没有令人满意的解决scheme呢。

http://docs.ansible.com/ansible/playbooks_variables.html#information-discovered-from-systems-facts

…还有其他地方的variables可以来自,但是这些variables的types被发现,而不是由用户设置。

事实是从与您的远程系统交谈中获得的信息…

事实来自您的远程主机。 所以这就是为什么当你的* .yml文件有几个剧本的时候,你通常可以在不同的主机之间共享事实的原因。

现在,如果你在你的剧本的另一部分做了这样的事情,你可以访问其他主机的事实:

 {{ hostvars['server01.example.com']['ansible_eth0']['ipv4']['address'] }} ... ... {{ hostvars[groups['servers'][0]]['ansible_eth0']['ipv4']['address'] }} 

但是在这种情况下,你需要记住, 使用这个之前你需要知道事实。 然后你可以在你的剧本中设置第一部分,获取所有主机的所有事实,或者使用事实caching来实现这一点(请参阅: http : //docs.ansible.com/ansible/playbooks_variables.html#fact-caching )

现在,如果你想分享你的剧本中的选项,也许可以更好地重新考虑信息并inputvariables。 你可以用相同的include指令“分享”你的variables,看看这个:

http://docs.ansible.com/ansible/playbooks_variables.html#variables-defined-from-included-files-and-roles

其实,你可以有多个主机:每个剧本的部分。 看来,主持人:开始一个新的戏剧。 例如,请参阅http://www.tecmint.com/use-ansible-playbooks-to-automate-complex-tasks-on-multiple-linux-servers/

像这样的东西对我来说很合适(可靠的2.2):

 --- - hosts: localhost connection: local roles: - { role: ec2, tag: 'master', instance_type: t2.2xlarge, count: 1 } tasks: - shell: hostname # reports localhost - hosts: tag_master tasks: - shell: hostname # reports instance(s) with tag 'master' 

所以,把主机:在每个包括.yml的顶部,而不是在包括:之后。