【发布时间】:2021-12-06 18:35:09
【问题描述】:
我尝试使用 ansible 2.11.6 将本地 KVM 机器动态添加到 ansible 库存。
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
我成功创建 KVM,启动它,等待端口 22 并尝试将其添加到清单中,并在播放“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}}"
当我在播放“B”中输出主机变量的内容时,我看到了预期的组和主机名:
...
"group_names": [
"libvirt"
],
"groups": {
"all": [
"ansible",
"k8smaster"
],
"libvirt": [
"k8smaster"
],
"local_ansible": [
"ansible"
],
"ungrouped": []
},
...
当我添加时
- debug: var=group_names
- debug: var=play_hosts
对于我的游戏“B”,我只获得了我的库存的静态信息。
TASK [debug] ****************************************************************************************************************************************************************************************************
ok: [ansible] => {
"group_names": [
"local_ansible"
]
}
TASK [debug] ****************************************************************************************************************************************************************************************************
ok: [ansible] => {
"play_hosts": [
"ansible"
]
}
我的 inventory.ini 看起来像
[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
这是一个最小的例子:
---
- 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:
因此,我无法针对 VM 运行任何任务,因为 ansible 完全忽略了它们。 ping 只是对主机“ansible”起作用。 任何想法,我在这里做错了什么?
【问题讨论】:
-
如果不知道“play B”的
hosts:是什么样子,很难为您提供帮助。请阅读how to ask页面,特别注意MCVE部分 -
谢谢。我在原始帖子中添加了一个示例。
标签: ansible qemu kvm ansible-inventory