为什么 Ansible 忽略 delegate_to 参数?

Mis*_*sko 2 ansible

我有一个非常简单的剧本:

- hosts: test
  gather_facts: no

  tasks:
    - name: debug
      debug: msg="{{ inventory_hostname }}"
      run_once: yes
      delegate_to: "host2"
Run Code Online (Sandbox Code Playgroud)

和库存文件:

host1 ansible_ssh_host="1.2.3.4"
host2 ansible_ssh_host="1.2.3.5"

[test]
host1
host2
Run Code Online (Sandbox Code Playgroud)

以及玩的结果:

TASK [debug] *************************************************************************************************************************************************************************
ok: [host1 -> host2] => {
    "changed": false,
    "msg": "host1"
}
Run Code Online (Sandbox Code Playgroud)

为了完整起见,无论我添加什么delegate_to,即使是一些随机字符串,结果总是"msg": "host1"

如何正确将此任务委托给 groups.test.1 或任何其他主机?

编辑:

- hosts: test
  gather_facts: no

  tasks:
    - name: 1
      shell: "hostname -f"
      run_once: yes
      delegate_to: "host2"
      register: result

    - name: debug
      debug: msg="{{ ansible_host }} {{ inventory_hostname }} {{ result.stdout }}"
      run_once: yes
Run Code Online (Sandbox Code Playgroud)

播放输出:

TASK [command] ***************************************************************************************************************************************************************************************
changed: [host1 -> None]

TASK [debug] *****************************************************************************************************************************************************************************************
ok: [host1] => {
    "msg": "1.2.3.4 host1 host2"
}
Run Code Online (Sandbox Code Playgroud)

安塞布尔 2.3.x

安塞布尔2.4.0

Kon*_*rov 6

inventory_hostnamedelegate_to当您使用它来“跟踪”您所处的上下文时,它不会改变,但其他变量确实会改变。

您可以使用以下方法进行测试:

- name: debug
  debug: msg="{{ inventory_hostname }} {{ ansible_host }}"
  run_once: yes
  delegate_to: "host2"
Run Code Online (Sandbox Code Playgroud)

你应该得到:host1 1.2.3.5