delegate_to and delegate_facts not working as expected

343 views
Skip to first unread message

Neil Watson

unread,
Jan 3, 2017, 2:40:11 PM1/3/17
to Ansible Project
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?

Brian Coca

unread,
Jan 3, 2017, 2:53:58 PM1/3/17
to ansible...@googlegroups.com
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.

----------
Brian Coca

Neil Watson

unread,
Jan 3, 2017, 3:09:55 PM1/3/17
to Ansible Project
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

Brian Coca

unread,
Jan 3, 2017, 3:52:15 PM1/3/17
to ansible...@googlegroups.com
no, its a new play

​​another solution:

- name: install prereqs
  delegate_to: "{{ new_vm }}"
  action:
    module: "{{hostvars[new_vm]['ansible_pkg_manager']}}"
Reply all
Reply to author
Forward
0 new messages