【问题标题】:Ansible print created hostsAnsible 打印创建的主机
【发布时间】:2018-01-16 16:39:19
【问题描述】:

我编写了一个创建多个 VM 的 Ansible Playbook。 Playbook 分为两个文件。 Main.yaml 和 vars.yaml。它创建了虚拟机,它似乎运行良好。我没有收到任何错误,所以我假设它成功地将创建的主机添加到库存中。我想检查创建的主机是否已添加到库存中。我如何打印/列出清单的主机?我的目标是稍后在创建的虚拟机上运行脚本。谢谢。

**Main.yaml**
#########CREATING VM#########
---
- hosts: localhost
  vars:
    http_port: 80
max_clients: 200
  vars_files:
   - vars.yaml
  tasks:
  - name: create VM
    os_server:
      name: "{{ item.name }}"
      state: present
      image: "{{ item.image }}"
      boot_from_volume: True
      security_groups: ssh
      flavor: "{{ item.flavor }}"
      key_name: mykey
      region_name: "{{ lookup('env', 'OS_REGION_NAME') }}"
      nics:
        - net-name: private
      wait: yes
    register: instances
    with_items: "{{ instance_definitions }}"
 ############################################
  - name: whait 15 seconds
    pause: seconds=15
    when: instances.changed
######DEBUG#################################
  - name: display results
    debug:
      msg: "{{ item }}"
    with_items: "{{ instances.results }}"
############################################
  - name: Add new VM to ansible Inventory
    add_host:
      name: "{{ item.server.name}}"
      ansible_host: "{{item.server.public_v4}}"
      ansible_user: "{{ansible_user}}"
      ansible_ssh_common_args: -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no
      groups: just_created
    with_items: "{{ instances.results }}"



**vars.yaml**
---
  instance_definitions:
     - { name: Debian Jessie, image: Debian Jessie 8, flavor: c1.small, loginame: debian }
     - { name: Debian Stretch, image: Debian Stretch 9, flavor: c1.small, loginame: debian }

【问题讨论】:

    标签: ansible openstack ansible-inventory


    【解决方案1】:

    这就是magic variables 的用途。

    您的所有主机都将在一个列表中:

    groups['just_created']
    

    【讨论】:

      【解决方案2】:

      下面的示例说明了如何创建内存中的主机并列出它们。神奇的酱汁是add_hosts 需要单独播放。

      ---
       - name: adding host playbook
         hosts: localhost
         connection: local
         tasks:
         - name: add host to in-memory inventory
           add_host:
             name: awesome_host_name
             groups: in_memory
      
       - name: checking hosts
         hosts: in_memory
         connection: local
         gather_facts: false
         tasks:
           - debug: var=group_names
           - debug: msg="{{ inventory_hostname }}"
           - debug: var=hostvars[inventory_hostname]
      

      【讨论】:

        【解决方案3】:

        您可以使用以下方法从 playbook 打印库存文件的内容:

        - debug: msg="the hosts are is {{lookup('file', '/etc/ansible/hosts') }}"
        

        或者,您可以使用以下命令从命令行列出主机:

        ansible --list-hosts all
        

        或者使用剧本中的这个命令:

        tasks:
          - name: list hosts
        command: ansible --list-hosts all
        register: hosts
          - debug:
            msg: "{{hosts.stdout_lines}}"
        

        【讨论】:

        • 这不是 OP 所要求的。 OP 使用 add_host 模块创建内存清单。您的方法(除了对于在原始ansible-playbook 调用中指定特定库存的任何情况都是错误的)不会显示这些主机。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-11
        • 1970-01-01
        • 1970-01-01
        • 2020-09-08
        • 1970-01-01
        相关资源
        最近更新 更多