Ansible:delegate_to 组无法正常工作

Ansible: delegate_to group not working correctly

在我的 dockerized Ansible 2.8 中,我正在尝试更改已使用 add_host

动态添加到清单的远程主机上的 ssh 设置

playbook.yml

# configure new VMs
- name: Configure new Azure VM
  hosts: localhost
  connection: local
  gather_facts: no
  roles:
    - az-vm-configure
  tags:
    - az-vm-configure

main.yml

- name: Configure inventory
  include: inventory.yml

- name: Configure sshd
  include: sshd.yml
  delegate_to: '{{ groups.new[0] }}'

当我使用以下构造时它工作正常:delegate_to: '{{ groups.new[0] }}' 但是当我像这样为组中的所有主机实现它时:

delegate_to: '{{ item }}'
with_items: "{{ groups['new'] }}"

我的任务忽略了上面的构造并尝试在本地主机上执行任务: task execution result

似乎 delegate_to: '{{ item }}' 在这种情况下不起作用。 有人可以建议任何解决方法吗?

Q: "dynamically using add_host"

- name: Configure sshd
  include: sshd.yml
  delegate_to: '{{ groups.new[0] }}'

答:可以用创建的'new'组主机开始下一场比赛

- hosts: new
  tasks:
    - name: Configure sshd
      include: sshd.yml

当我尝试将 hosts 放在 main.yml 中时,如上所述,我给出了一个错误 ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path. 无论如何,这是我的解决方案:

playbook.yml

# configure VM
- name: Add new VM to inventory
  hosts: localhost
  connection: local
  gather_facts: no
  tasks:
    - include_role:
        name: az-vm-configure
        tasks_from: inventory.yml
  tags:
    - az-vm-configure

- name: Configure new Azure VM
  hosts: new
  gather_facts: no
  tasks:
    - include_role:
        name: az-vm-configure
        tasks_from: sshd.yml
  tags:
    - az-vm-configure

roles/az-vm-configure/tasks/main.yml

- include_tasks: '{{ tasks }}'
  with_items:
    - inventory.yml
    - sshd.yml
  loop_control:
    loop_var: tasks