Ansible 从 ansible play/playbook 中获取“hosts”键的值

Sam*_*dra 5 variables ansible ansible-2.x ansible-facts

有什么方法可以获取正在执行戏剧的一组主机的组名称吗?我知道 ansible 有一个名为ansible_play_hosts的变量,它是正在执行特定操作的所有主机的列表。我想要包含所有这些主机的实际组名称。

我使用的是ansible版本2.3.2.0

例子:

# file: hosts

[my-host-group]
hostname-1
hostname-2
Run Code Online (Sandbox Code Playgroud)


# file: playbook.yml
---

- hosts: my-host-group
  tasks:
    - name: "Print group name for 'hosts'"
      debug:
        msg: "Hosts var is '{{ hosts }}'"  
Run Code Online (Sandbox Code Playgroud)

我想要打印该消息Hosts var is 'my-host-group'

Zei*_*tor 1

我的感觉是,你是从一个具体的例子中得出概括性的结论。不存在这样的事情the group name for the set of hosts that a play is executing on。游戏针对您在hosts给定游戏的密钥中定义的一组主机执行。该参数所期望的是一个模式。尽管在您的示例中模式是单个组,但它可能比这复杂得多。

该模式在戏剧开始时被读出。它可以作为变量内的原始给定字符串使用ansible_play_name。因此,如果您 100% 确定只提供组名称,则此 var 可以解决问题。但这远非在所有情况下都可靠。

一旦在游戏开始时读取该模式,它就会转化为一组要运行游戏的单独目标。该列表在变量中可用(与您在问题中引用的ansible_play_hosts_all相比,它包含所有主机,无论是否失败)。ansible_play_hosts

all现在,如果您想可靠地找到除和ungrouped特殊组之外的所有目标主机所共有的组名称,这里有一种解决方案。

为了更好地理解这个问题,我在以下位置创建了以下示例清单inventories/demo/hosts.yml

---
all:
  children:
    cluster_members:
      children:
        controllers:
          hosts:
            host1:
            host2:
        workers:
          hosts:
            host3:
            host4:
    random1:
      hosts:
        toto:
    random2:
      hosts:
        pipo:
  hosts:
    bingo:

Run Code Online (Sandbox Code Playgroud)

以下剧本:

---
- hosts: "{{ target }}"
  gather_facts: false

  tasks:
    - name: Calculate common groups for hosts in play
      vars:
        current_groups: "{{ hostvars[item].group_names | difference(['all', 'ungrouped']) }}"
      ansible.builtin.set_fact:
        common_groups: "{{ common_groups | d(hostvars[ansible_play_hosts_all.0].group_names) | intersect(current_groups) }}"
      loop: "{{ ansible_play_hosts_all }}"
      run_once: true

    - ansible.builtin.debug:
        msg:
          - "This play targets the following hosts: {{ ansible_play_hosts_all | join(', ') }}"
          - "These targets where expanded from the following hosts play pattern: {{ ansible_play_name }}"
          - "These hosts are part of {{ common_groups | count }} common group(s): {{ common_groups | join(', ') }}"
      run_once: true
Run Code Online (Sandbox Code Playgroud)

给出(针对不同目标模式的多次执行):

$ ansible-playbook -i inventories/demo/ playbook.yml -e target='cluster_members'

PLAY [cluster_members] ********************************************************************************************************************************************************************************************************

TASK [Calculate common groups for hosts in play] ******************************************************************************************************************************************************************************
ok: [host1] => (item=host1)
ok: [host1] => (item=host2)
ok: [host1] => (item=host3)
ok: [host1] => (item=host4)

TASK [ansible.builtin.debug] **************************************************************************************************************************************************************************************************
ok: [host1] => {
    "msg": [
        "This play targets the following hosts: host1, host2, host3, host4",
        "These targets where expanded from the following hosts play pattern: cluster_members",
        "These hosts are part of 1 common group(s): cluster_members"
    ]
}

PLAY RECAP ********************************************************************************************************************************************************************************************************************
host1                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   


$ ansible-playbook -i inventories/demo/ playbook.yml -e target='workers'

PLAY [workers] ****************************************************************************************************************************************************************************************************************

TASK [Calculate common groups for hosts in play] ******************************************************************************************************************************************************************************
ok: [host3] => (item=host3)
ok: [host3] => (item=host4)

TASK [ansible.builtin.debug] **************************************************************************************************************************************************************************************************
ok: [host3] => {
    "msg": [
        "This play targets the following hosts: host3, host4",
        "These targets where expanded from the following hosts play pattern: workers",
        "These hosts are part of 2 common group(s): cluster_members, workers"
    ]
}

PLAY RECAP ********************************************************************************************************************************************************************************************************************
host3                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   


$ ansible-playbook -i inventories/demo/ playbook.yml -e target='all:!controllers'

PLAY [all:!controllers] *******************************************************************************************************************************************************************************************************

TASK [Calculate common groups for hosts in play] ******************************************************************************************************************************************************************************
ok: [bingo] => (item=bingo)
ok: [bingo] => (item=toto)
ok: [bingo] => (item=pipo)
ok: [bingo] => (item=host3)
ok: [bingo] => (item=host4)

TASK [ansible.builtin.debug] **************************************************************************************************************************************************************************************************
ok: [bingo] => {
    "msg": [
        "This play targets the following hosts: bingo, toto, pipo, host3, host4",
        "These targets where expanded from the following hosts play pattern: all:!controllers",
        "These hosts are part of 0 common group(s): "
    ]
}

PLAY RECAP ********************************************************************************************************************************************************************************************************************
bingo                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
Run Code Online (Sandbox Code Playgroud)