【问题标题】:ansible-inventory --list host groups command lineansible-inventory --list 主机组命令行
【发布时间】:2021-01-13 10:29:45
【问题描述】:

我想(仅)从 ansible-inventory 获取主机组名称列表,但是我必须使用 grep 根据已知的组名称模式来缩减列表 - 例如

  • 输出干净但命令行混乱,需要提前了解组名模式:

ansible-inventory -i inventory/production --list --yaml | grep webserver_.*:$

  • 干净的命令行,不需要知道组名模式,但输出混乱:

ansible-playbook my-playbook.yml -i inventory/production --list-hosts

有没有一种干净的方法可以只从库存中提取组名?

hosts.yml 示例:

# NGINX
webserver_1:
  hosts:
    ws1.public.example.com

webserver_2:
  hosts:
    ws2.public.example.com

webserver_2:
  hosts:
    ws2.public.example.com

# EC2 back-ends
backend_ec2_1:
  hosts:
    be1.internal.example.com

backend_ec2_2:
  hosts:
    be2.internal.example.com

backend_ec2_3:
  hosts:
    be3.internal.example.com

[Ansible v2.9.7]

【问题讨论】:

  • 请添加样品库存

标签: ansible ansible-inventory


【解决方案1】:

您可以使用jq 命令解析来自ansible-inventory --list 的json 输出,如下所示:

$ ansible-inventory -i hosts --list | jq .all.children
[
  "backend_ec2_1",
  "backend_ec2_2",
  "backend_ec2_3",
  "ungrouped",
  "webserver_1",
  "webserver_2"
]

或者,如果您只想要裸名:

$ ansible-inventory -i hosts --list | jq -r '.all.children[]'
backend_ec2_1
backend_ec2_2
backend_ec2_3
ungrouped
webserver_1
webserver_2

【讨论】:

  • 很好,昨天我打算建议使用yq,但正在等待this bug fix?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-27
  • 1970-01-01
  • 2019-06-20
  • 1970-01-01
  • 1970-01-01
  • 2019-12-31
相关资源
最近更新 更多