【问题标题】:Add a host to inventory dynamically in ansible在ansible中动态添加主机到库存
【发布时间】: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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    最少的库存和最少的剧本表明 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 ...
    

    special variable play_hosts 已弃用,但仍在工作,也就是说,这不是问题的原因。

    【讨论】:

    • 感谢您的提示。但这不是原因。我降级到 python 3.8.12 并且具有完全相同的行为。
    猜你喜欢
    • 1970-01-01
    • 2022-06-22
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 2018-02-17
    相关资源
    最近更新 更多