delegate_to and delegate_facts not working as expected

Consider these tasks:

`

  • name: Gather facts about new VM
    delegate_to: “{{ new_vm }}”
    delegate_facts: true
    setup:
    register: host_info

  • name: install prereqs
    delegate_to: “{{ new_vm }}”
    package:
    name: “{{ item }}”
    state: present
    with_items:

  • at
    `

This results in:

TASK [provision_vm_and_set_ip : Gather facts about new VM] *********************
ok: [jenkins2 → 10.120.8.220]

TASK [provision_vm_and_set_ip : install prereqs] *******************************
failed: [jenkins2 → 10.120.8.220] (item=[u’at’]) => {“failed”: true, “item”: [“at”], “msg”: “Could not detect which package manager to use. Try gathering facts or setting the "use" option.”}

Why does the package task fail when the setup task appeared to pass?

Because you are consuming the facts of the inventory_hostname, not delegate_to host (this only overrides the connection variables).

  • hosts: new_vm
    tasks:
  • package:

will work.

Can you set a new host in a role?

roles/vm/tasks/main.yml

  • name task 1

  • name task 2

-host

tasks:

  • name: task on delegate host

no, its a new play