ansible - 获取 list 主机名列表

标签 ansible ansible-inventory ansible-template

host1: abc
host2: xyz

host1 和 host2 列在测试主机下

[test-hosts]
abc
xyz

当我调试 inventory_hostnames 时,我看到它们如下所示

    > TASK [debug inventory_hostname]
    > ************************************************************************************************************************************************************************* 
ok: [abc] => {
    >     "inventory_hostname": "abc" } 
ok: [xyz] => {
    >     "inventory_hostname": "xyz" }

有什么方法可以通过将 inventory_hostname 分配给变量来像列表一样收集它。

预期结果:

exp_result: [abc, xyz]

最佳答案

您可以使用 groups['test-hosts'] 将这些主机作为列表获取。

例如:

---
- hosts: all
  gather_facts: false
  tasks:
    - set_fact:
        host_list: "{{ groups['test-hosts'] }}"
    - debug:
        msg:  "{{host_list}}"

关于ansible - 获取 list 主机名列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56567057/

相关文章:

windows - Ansible Windows Kerberos 身份验证从服务器代码 500 返回错误的 HTTP 响应

docker - Ansible:如何复制环境文件?

loops - Ansible - 同时使用 with_items 和 with_sequence

ansible - 如何访问以编程方式构造的 ansible 变量?

Ansible/jinja2 : Use filter result in if condition

ansible - 如何使用 ansible 设置 logrotation?

ansible - 如何在 Ansible 动态 list EC2 中设置用户名

ansible - 如何设置 Ansible 层特定的库存变量?

Ansible concat 默认变量和文字字符串

Ansible,set_fact使用if then else语句