【问题标题】:Ansible: Retrieve Data from inventoryAnsible:从库存中检索数据
【发布时间】:2021-10-28 09:16:06
【问题描述】:

我需要从清单文件中检索 bob_server 的 IP。我不清楚在filterlookupwhen 中使用什么组合?根据库存文件bob_serveralice_server 名称可以更改,但app_type 不会更改。我的剧本逻辑显然是错误的,有人可以指导我在app_type = bob时获取IP地址的正确方法

我当前的库存文件:

---
all:
  hosts:
  children:
    bob_server:
      hosts: 10.192.2.6
      vars:
        app_type: bob
    alice_server:
      hosts: 10.192.2.53
      vars:
        app_type: alice

我的剧本

---
- hosts: localhost
  name: Retrive data
  tasks:
    - name: Set Ambari IP
      set_fact:
        ambariIP: "{{ lookup('hosts', children) }}"
      when: "hostvars[app_type] == 'bob'"

【问题讨论】:

  • 您的库存文件无效。请参考documentation 并修复它。此外,您似乎混淆了组和主机的概念。这也在文档中进行了解释。一旦所有这些都解决了,您将不需要任何花哨的查找:使用 inventory_hostnameansible_host(取决于您如何命名主机)将为您提供预期的数据。
  • 是的,组和主机的概念令人困惑。但是,不,库存不是无效的。见下文,ansible-inventory -i hosts-01 --list 工作正常。我认为没有理由关闭。这是一个很好的例子,说明如何(不)使用(仍然有效)YAML inventory format

标签: ansible ansible-2.x ansible-inventory ansible-facts


【解决方案1】:

给定库存

shell> cat hosts-01
---
all:
  hosts:
  children:
    bob_server:
      hosts: 10.192.2.6
      vars:
        app_type: bob
    alice_server:
      hosts: 10.192.2.53
      vars:
        app_type: alice

简单的选项是使用ansible-inventory,例如

- hosts: localhost
  tasks:
    - command: ansible-inventory -i hosts-01 --list
      register: result
    - set_fact:
        my_inventory: "{{ result.stdout|from_yaml }}"
    - debug:
        var: my_inventory.bob_server.hosts

给予

  my_inventory.bob_server.hosts:
  - 10.192.2.6

如果您想自己解析文件,请将其读入字典并展平路径,例如(安装 ansible.utils ansible-galaxy collection install ansible.utils

    - include_vars:
        file: hosts-01
        name: my_hosts
    - set_fact:
        my_paths: "{{ lookup('ansible.utils.to_paths', my_hosts) }}"
    - debug:
        var: my_paths

给予

  my_paths:
    all.children.alice_server.hosts: 10.192.2.53
    all.children.alice_server.vars.app_type: alice
    all.children.bob_server.hosts: 10.192.2.6
    all.children.bob_server.vars.app_type: bob
    all.hosts: null

现在选择以bob_server.hosts结尾的键

    - set_fact:
        bob_server_hosts: "{{ my_paths|
                              dict2items|
                              selectattr('key', 'match', '^.*bob_server\\.hosts$')|
                              items2dict }}"

给予

  bob_server_hosts:
    all.children.bob_server.hosts: 10.192.2.6

并选择 IP

    - set_fact:
        bob_server_ips: "{{ bob_server_hosts.values()|list }}"

给予

  bob_server_ips:
  - 10.192.2.6

库存缺少组的概念。见Inventory basics: formats, hosts, and groups。通常,children 的值是一组主机。在此清单中,children 的值是单个主机。这在概念上是错误的,但仍然有效,.e.g

- hosts: bob_server
  gather_facts: false
  tasks:
    - debug:
        var: inventory_hostname

给予

shell> ansible-playbook -i hosts-01 playbook.ym
  ...
TASK [debug] ****************************************************
ok: [10.192.2.6] =>
  inventory_hostname: 10.192.2.6

【讨论】:

  • 感谢您的解释,我的问题是库存是动态的。另外,bob_server 和 Alice_server 也是动态的,只有app_type 是一致的
猜你喜欢
  • 2015-02-06
  • 2011-12-30
  • 2019-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多