大网数据广告活动
服务器运维问答

How to specify different hosts for different playbooks in one ansible script

· 52CE 我爱测

As I understand, each playbook takes one hosts entry. I want to know that if I create a container playbook that includes other playbooks, can I parameterize the hosts for each playbook include. So something like --- - include pla…

问题原文

原帖链接 · Pankaj Lal · CC BY-SA 3.0

As I understand, each playbook takes one hosts entry. I want to know that if I create a container playbook that includes other playbooks, can I parameterize the hosts for each playbook include. So something like

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

I am able to put all different playbooks in one script and call, but then this way I am not able to reuse some set_fact from one playbook into another and hence there is a lot of task duplication.

Another corollary of the question is, can I launch ec2s on amazon which would have

hosts: localhost

and configuration of the launched ec2s, which would basically configure inventory from -i ec2.py, and have hosts specified as

hosts: tag_<some_tag>

happening through the same playbook or a set of included playbooks (different roles)?

社区高分解答

原帖链接 · Bill Welch · CC BY-SA 3.0

Actually, you can have more than one hosts: section per playbook. It appears that a hosts: starts a new play. See http://www.tecmint.com/use-ansible-playbooks-to-automate-complex-tasks-on-multiple-linux-servers/, for example.

Something like this works for me (ansible 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'

So, put hosts: at the top of each included .yml, not after the include:.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-04-02)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。