我想在 ansible playbook 中一个一个地访问 ansible 库存值?

I want to access ansible inventory value one by one in ansible playbook?

我有一个非常简单的用例。我想将我的库存变量一个一个地访问到剧本中。

文件夹结构:-

所以我运行ansible-playbook -vvv playbooks/deploy-sevice-sub.yaml -i inventory/inventorysub.

inventorysub是这个。

[dev]
host=127.0.0.1 user=test pass=test
host=127.0.0.2 user=test pass=test

deploy-service-sub.yaml

---
- name: Step to push sub
  hosts: dev
  roles:
    - deploy-service-package

role/deploy-service-package\task\main.yaml

- name: Print the gateway for each host when defined
  ansible.builtin.debug:
    msg: "System {{ hostvars['dev'].host }} has gateway {{ hostvars['dev'].host }}"

我可以 运行 在那些服务器上一个一个地命令,我只想访问那些值,没有 ssh 连接。

它抛出错误

fatal: [nso_host=127.0.0.1]: UNREACHABLE! => {

    "changed": false,

    "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname nso_host=127.0.0.1: Name or service not known",

    "unreachable": true

}

fatal: [nso_host=127.0.0.1]: UNREACHABLE! => {

    "changed": false,

    "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname nso_host=127.0.0.2: Name or service not known",

    "unreachable": true

}

我不需要它来连接,只需从库存中获取价值。 如何将主机逐一访问到我的任务文件中?有什么想法吗?

如果您使用 hostvars 变量访问您的 hostvars,您不需要在游戏中以远程主机为目标:您可以编写一个以 localhost 为目标的游戏。

我们可以使用 loop 来遍历特定的库存组(或 all 对于所有主机):

- hosts: localhost
  gather_facts: false
  tasks:
    - name: Print the gateway for each host when defined
      ansible.builtin.debug:
        msg: "System {{ item }} has gateway {{ hostvars[item].host }}"
      loop: "{{ groups.dev }}"

给定清单文件,例如:

all:
  children:
    dev:
      hosts:
        node1:
          host: 127.0.0.1
          user: test
          pass: test
        node2:
          host: 127.0.0.2
          user: test
          pass: test

这会产生输出:

PLAY [localhost] ***************************************************************

TASK [Print the gateway for each host when defined] ****************************
ok: [localhost] => (item=node1) => {
    "msg": "System node1 has gateway 127.0.0.1"
}
ok: [localhost] => (item=node2) => {
    "msg": "System node2 has gateway 127.0.0.2"
}

PLAY RECAP *********************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

或者,您可以针对您的主机中的 playbook 库存,但设置 gather_facts: false。如果你只是 运行 debug 个任务,游戏不需要连接到远程主机:

- hosts: dev
  gather_facts: false
  tasks:
    - name: Print the gateway for each host when defined
      ansible.builtin.debug:
        msg: "System {{ inventory_hostname }} has gateway {{ host }}"

产生:

PLAY [dev] *********************************************************************

TASK [Print the gateway for each host when defined] ****************************
ok: [node1] => {
    "msg": "System node1 has gateway 127.0.0.1"
}
ok: [node2] => {
    "msg": "System node2 has gateway 127.0.0.2"
}

PLAY RECAP *********************************************************************
node1                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
node2                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0