【问题标题】:How to list all currently targeted hosts in an Ansible play如何在 Ansible 中列出所有当前目标主机
【发布时间】:2015-04-26 21:14:23
【问题描述】:

我正在运行 Ansible 游戏,并希望列出它所针对的所有主机。 Ansible docs mentions that this is possible,但他们的方法似乎不适用于复杂的目标群体(目标如主机:web_servers:&data_center_primary)

我确信这是可行的,但似乎找不到任何进一步的文档。是否存在包含所有当前目标主机的 var?

【问题讨论】:

  • 不确定您在文档中指的是哪个部分。 (可能是错误的链接?) - 如果您不是故意/不知道,您可以使用开关 --list-hosts 仅列出剧本会影响的主机。
  • --list-hosts 是我想要的数据,但是对于特定的游戏,目标与整体输入不同

标签: ansible ansible-playbook


【解决方案1】:

您可以使用选项 --list-hosts 仅列出 playbook 会影响的主机。

此外,还有一个字典 hostvars,它包含 Ansible 当前已知的所有主机。但我认为setup 模块必须在所有主机上运行,​​因此您不能通过gather_facts: no 跳过该步骤。

【讨论】:

  • --check 也很方便。它们(列表主机,检查)都不是完美的,但很高兴了解它们。
  • 列出命令中的所有主机我需要知道 ad_hoc 中的--limit 值是什么,有什么方法可以得到它吗? --list-hosts 正在向控制台提供结果,但我需要在最后一个剧本的最后一个任务中处理结果
【解决方案2】:

您正在寻找“play_hosts”变量

---
- hosts: all

  tasks:
    - name: Create a group of all hosts by app_type
      group_by: key={{app_type}}

    - debug: msg="groups={{groups}}"
      run_once: true

- hosts: web:&some_other_group

  tasks:
   - debug: msg="play_hosts={{play_hosts}}"
     run_once: true

会导致

TASK: [Create a group of all hosts by app_type] *******************************
changed: [web1] => {"changed": true, "groups": {"web": ["web1", "web2"], "load_balancer": ["web3"]}}

TASK: [debug msg="play_hosts={{play_hosts}}"] *********************************
ok: [web1] => {
    "msg": "play_hosts=['web1']"
}

库存:

[proxy]
web1 app_type=web
web2 app_type=web
web3 app_type=load_balancer

[some_other_group]
web1
web3

【讨论】:

猜你喜欢
  • 2017-02-10
  • 2022-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多