Ansible 2.9 hostvars localhost issue

23 views
Skip to first unread message

Michael Garris

unread,
Jan 28, 2020, 12:43:00 PM1/28/20
to Ansible Project
I have a local playbook on my laptop which I use to build local vms for testing. I have noticed recently (with the update to Ansible 2.9) that I have been having issues with the playbook running successfully. It used to work perfectly fine. 

ansible version = ansible-2.9.4-1.el8ae.noarch


[]$ cat vars.yml 
---
qcow_image: /home/name/KVM-Images/rhel-8.1-x86_64-kvm.qcow2
libvirt_path: /var/lib/libvirt/images
qemu_path: /usr/libexec/qemu-kvm
password: <removed>
vm_name: rhel8fixingbook
vm_domain: <removed>.local
vm_cpu: 2
vm_ram: 8192
vm_size: 40G
nameserver: "@192.168.122.1"




[]$ cat custom-vm.yml 
---
- hosts: localhost
  connection: local
  become: true
  gather_facts: false
  vars_files:
    - vars.yml

  tasks:

  - name: Copying Image to libvirt path
    command: cp {{ qcow_image }} {{ libvirt_path }}/{{vm_name}}.qcow2

  - name: Resizing VM
    command: qemu-img resize {{ libvirt_path }}/{{ vm_name }}.qcow2 {{ vm_size }}
  
  - name: Copy meta-data template to tmp
    template:
      src: templates/meta-data
      dest: /tmp/meta-data
      owner: root
      group: root
      mode: 0644

  - name: Copy user-data template to tmp
    template:
      src: templates/user-data-custom
      dest: /tmp/user-data
      owner: root
      group: root
      mode: 0644

  - name: Create config
    command: genisoimage -o /tmp/config.iso -V cidata -r -J /tmp/meta-data /tmp/user-data

  - name: Create VM
    virt:
      name: "{{ vm_name }}"
      command: define
      xml: "{{ lookup('template', 'libvirt-domain.xml.j2') }}"
      uri: 'qemu:///system'

  - name: Start VM
    virt:
      name: "{{ vm_name }}"
      state: running

  - name: Add config to VM
    command: virsh change-media {{ vm_name }} hda /tmp/config.iso --insert 

  - pause:
      seconds: 20

  - name: Get IP Address of New VM
    command: dig {{ vm_name }} {{ nameserver }} +short
    register: vm_ipaddr

  - name: Output New VM IPaddress
    debug:
      msg: "New VM IPaddress: {{ vm_ipaddr.stdout }}"

  - name: Set VM IP Address Fact
    set_fact:
      new_vm_ipaddr: "{{ vm_ipaddr.stdout }}"

- hosts: "{{ hostvars['localhost']['new_vm_ipaddr'] }}"
  become: true
  tasks:

  - include_role:
      name: "{{ item }}"
    with_items:
      - reg_n_packs
#      - ansible_tower





Play run (ansible-playbook -K custom-vm.yml):

.........
TASK [Get IP Address of New VM] ************************************************************************************************************
changed: [localhost]

TASK [Output New VM IPaddress] *************************************************************************************************************
ok: [localhost] => {
    "msg": "New VM IPaddress: 192.168.122.206"
}

TASK [Set VM IP Address Fact] **************************************************************************************************************
ok: [localhost]
[WARNING]: Could not match supplied host pattern, ignoring: 192.168.122.206


PLAY [192.168.122.206] *********************************************************************************************************************
skipping: no hosts matched

PLAY RECAP *********************************************************************************************************************************
localhost                  : ok=12   changed=7    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Kai Stian Olstad

unread,
Jan 28, 2020, 2:03:27 PM1/28/20
to ansible...@googlegroups.com
In you subject you are hinting of problem with hostvars but that is
working fine.
As you can see it is trying to run against 192.168.122.206 but you get
"no hosts matched".

Your new host, 192.168.122.206, is not in your Ansible inventory so it
can't find it.
How is this new host supposed to appear/be added to the the inventory?

--
Kai Stian Olstad

Michael Garris

unread,
Jan 28, 2020, 3:14:15 PM1/28/20
to Ansible Project
And that is why I opened this!!! I recently did an upgrade (RHEL7 -> RHEL8) so it wiped my /etc/ansible/hosts file. Totally forgot I had added 192.168.122.[1:255] to it!

Thanks Kai!
Reply all
Reply to author
Forward
0 new messages