首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ansible中动态添加主机到清单

在ansible中动态添加主机到清单
EN

Stack Overflow用户
提问于 2021-10-19 20:52:54
回答 2查看 265关注 0票数 0

我尝试使用ansible 2.11.6将本地KVM机器动态添加到ansible清单中。

代码语言:javascript
复制
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”中的以下任务将其添加到清单中:

代码语言:javascript
复制
- 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}}"

当我在Play "B“中输出hostvars的内容时,我看到了预期的组和主机名:

代码语言:javascript
复制
...
            "group_names": [
                "libvirt"
            ],
            "groups": {
                "all": [
                    "ansible",
                    "k8smaster"
                ],
                "libvirt": [
                    "k8smaster"
                ],
                "local_ansible": [
                    "ansible"
                ],
                "ungrouped": []
            },
...

当我添加

代码语言:javascript
复制
- debug: var=group_names
- debug: var=play_hosts

对于我的游戏"B",我只得到我的库存的静态信息。

代码语言:javascript
复制
TASK [debug] ****************************************************************************************************************************************************************************************************
ok: [ansible] => {
    "group_names": [
        "local_ansible"
    ]
}

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

我的inventory.ini看起来像

代码语言:javascript
复制
[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

下面是一个最小的例子:

代码语言:javascript
复制
---

- 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“起作用。你知道我做错了什么吗?

EN

回答 2

Stack Overflow用户

发布于 2021-10-20 06:26:17

Minimal inventory和minimal playbook表明,使用Python 3.8,一切都如预期的那样工作。

您正在使用Python 3.9运行ansible,请尝试降级。

代码语言:javascript
复制
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
代码语言:javascript
复制
shell> cat inventory.ini
localhost
代码语言:javascript
复制
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
代码语言:javascript
复制
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 ...

special variable play_hosts已弃用,但仍在运行,即这不是问题的原因。

票数 0
EN

Stack Overflow用户

发布于 2021-10-20 11:53:48

找到了解决方案。我使用limit选项将执行限制到本地主机。从上周开始,我使用了一个旧版本的ansible,它使用了我动态添加的额外主机。对于当前版本的Ansible,需要将新主机添加到limit选项中。使用"--limit ansible,libvirt“可以工作(当新主机是libvirt组的一部分时)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69637510

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档