0

I try to add a local KVM maschine dynamically to ansible inventory with ansible 2.11.6.

ansible [core 2.11.6]
  config file = /home/ansible/ansible.cfg
  configured module search path = ['/home/ansible/library']
  ansible python module location = /usr/local/lib/python3.9/dist-packages/ansible
  ansible collection location = /home/ansible/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.9.2 (default, Feb 28 2021, 17:03:44) [GCC 10.2.1 20210110]
  jinja version = 3.0.2
  libyaml = True

I create the KVM successful, start it, wait for port 22 and try to add it to inventory with following task in play "A":

- name: "{{libvirt_maschine_name}}: Add VM to in-memory inventory"
  local_action:
    module: add_host
    name: "{{libvirt_maschine_name}}"
    groups: libvirt
    ansible_ssh_private_key_file: "{{ansible_user_home}}/.ssh/{{libvirt_maschine_name}}-ssh.key"
    ansible_default_ipv4: "{{vm_ip}}"
    ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
    ansible_host: "{{vm_ip}}"

When i output the content of hostvars in Play "B" i see the groups and hostname as expected:

...
            "group_names": [
                "libvirt"
            ],
            "groups": {
                "all": [
                    "ansible",
                    "k8smaster"
                ],
                "libvirt": [
                    "k8smaster"
                ],
                "local_ansible": [
                    "ansible"
                ],
                "ungrouped": []
            },
...

When i add

- debug: var=group_names
- debug: var=play_hosts

to my play "B", i get just the static information of my inventory.

TASK [debug] ****************************************************************************************************************************************************************************************************
ok: [ansible] => {
    "group_names": [
        "local_ansible"
    ]
}

TASK [debug] ****************************************************************************************************************************************************************************************************
ok: [ansible] => {
    "play_hosts": [
        "ansible"
    ]
}

My inventory.ini looks like

[all]
ansible ansible_host=localhost

[local_ansible]
ansible ansible_host=localhost

[local_ansible:vars]
ansible_ssh_private_key_file=~/.ssh/ansible.key
ansible_ssh_common_args='-o StrictHostKeyChecking=no'
ansible_user=ansible

Here is a minimal example:

---

- name: "Play A"
  hosts: all
  become: yes
  gather_facts: yes

  tasks:

    - name: "Import variables from file"
      include_vars:
        file: k8s-single-node_vars.yaml

    - name: "Do some basic stuff"
      include_role:
        name: ansible-core

    - name: "Add VM to in-memory inventory"
      add_host:
        name: "myMaschine"
        groups: myGroup
        ansible_ssh_private_key_file: "test.key"
        ansible_default_ipv4: "192.168.1.1"
        ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
        ansible_host: "192.168.1.1"

- name: "Play B"
  hosts: all
  become: yes
  gather_facts: no
  tasks:

    - debug: var=hostvars
    - debug: var=group_names
    - debug: var=play_hosts

    - name: test-ping
      ping:

Therefore, i am not able to run any task against the VM, because ansible is completely ignoring them. A ping is just working against the host "ansible". Any idea, what i do wrong here?

2
  • Without knowing what the hosts: looks like for "play B" it's hard to help you. Please read the how to ask page, and pay especial attention to the MCVE section Commented Oct 20, 2021 at 4:40
  • Thanks. I added an example to the original post. Commented Oct 20, 2021 at 5:33

2 Answers 2

0

Minimal inventory and minimal playbook show that everything works as expected with Python 3.8.

You are running ansible with Python 3.9 Try to downgrade.

shell> ansible --version
ansible [core 2.11.5]
...
python version = 3.8.5 (default, Jan 27 2021, 15:41:15) [GCC 9.3.0]
jinja version = 3.0.1
shell> cat inventory.ini
localhost
shell> cat playbook.yml
---
- name: Play A
  hosts: all
  gather_facts: false
  tasks:
    - add_host:
        name: myMaschine
        groups: myGroup

- name: Play B
  hosts: all
  gather_facts: false
  tasks:
    - debug:
        var: group_names
    - debug:
        var: ansible_play_batch
shell> ansible-playbook -i inventory.ini playbook.yml

PLAY [Play A] ***************************************

TASK [add_host] *************************************
changed: [localhost]

PLAY [Play B] ***************************************

TASK [debug] ****************************************
ok: [myMaschine] =>
  group_names:
  - myGroup
ok: [localhost] =>
  group_names:
  - ungrouped

TASK [debug] ****************************************
ok: [myMaschine] =>
  ansible_play_batch:
  - myMaschine
  - localhost
ok: [localhost] =>
  ansible_play_batch:
  - myMaschine
  - localhost

PLAY RECAP ******************************************
localhost:  ok=3 changed=1 unreachable=0 failed=0 ...
myMaschine: ok=2 changed=0 unreachable=0 failed=0 ...

The special variable play_hosts is deprecated but is still working, i.e. this is not the reason for the problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the tip. But thats not the reason. I downgraded to python 3.8.12 and have the exact same behavior.
0

Found the solution. I used the limit option to limit the execution to the localhost. Since last week i used an old version of ansible which used the additional host i add dynamically. With the current version of Ansible it is needed to add the new hosts to the limit option. With "--limit ansible,libvirt" it works (when the new hosts are part of the libvirt group).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.