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?
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