ansible - 在ansible中动态添加主机到库存
问题描述
我尝试使用 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”起作用。任何想法,我在这里做错了什么?
解决方案
最少的库存和最少的剧本表明 Python 3.8 一切正常。
您正在使用 Python 3.9 运行 ansible 尝试降级。
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 ...
特殊变量 play_hosts已弃用但仍在工作,即这不是问题的原因。
推荐阅读
- r - 使用 plot_grid 和 cowplot 删除 NULL 图上的标签
- angularjs - ng-controller 如何定位正确的控制器文件?
- android - 如何使用 sourceSets 替换/覆盖 gradle 中的应用程序图标
- javascript - 调试 JS 代码,为什么值更新而不执行代码
- python - 通过 URL 的路径解析
- python - 如何访问函数结果中的记录或 frecord 变量,以便我可以使用它来函数提交?
- reactjs - 我们可以在 AsyncStorage 中有监听器吗?
- javascript - 夏令时区的 react-native-modal-datetime-picker 问题
- android - Android键盘工具栏通过调整调整大小切断了视图的底部
- json - 解析 Multipart/Form 消息并处理