ansibl inventory:使用add_host或group_by创建具有多个组的临时库存

时间:2016-12-22 18:19:08

标签: ansible vsphere

我根据给定的内容提供{{create new hosts}} invonteroy文件, 示例

[SET1]
1.1.1.1 new_ip=1.1.1.1

[SET2]
1.1.1.2 new_ip=1.1.1.2

[SET3]
1.1.1.3 new_ip=1.1.1.3 

这是问题所在。清单文件包含尚不存在的一组IP地址。 我想做什么

- connect to vsphere host(5.5)
- create new machines based on amount of hosts in the inventory
- change the machines ip address to match the one in the given inventory 

模块,如

vmware_shell
vmware_guest 

需要我没有的管理权限。 解决此问题的唯一方法是在配置任务期间尝试创建内存或临时库存。 这是我的角色:

- vsphere_guest:
    vcenter_hostname: {{ vcenter }}
    validate_certs: no
    username: "{{ vsphere.username }}"
    password: "{{ vsphere.password }}"
    guest: "{{ item }}"
    from_template: yes
    template_src: Centos_base
    resource_pool: "/Resources"
    esxi:
      datacenter: Remote
      hostname: {{ esx_host }}
  with_items:
      - "{{ play_hosts }}"

- name: wait for power on to finish
  pause: seconds=5

- name: get facts from vm
  vsphere_guest:
    validate_certs: False
    vcenter_hostname: {{ vcenter }} 
    username:  "{{ vsphere.username }}"
    password:  "{{ vsphere.password }}"
    guest: "{{ item  }}"
    vmware_guest_facts: yes
  with_items:
      - "{{ play_hosts }}"
  register: vm_facts
  until: vm_facts.ansible_facts.hw_eth0.ipaddresses[0] is defined
  retries: 10
  delay: 10

- name: grab new ipaddresses
  set_fact: myIp="{{ inventory_hostname }}"

- name: Add production VMs to inventory
  add_host: hostname={{item.ansible_facts.hw_eth0.ipaddresses[0] }} groups=temp
            new_ip={{ myIp }}
  with_items: "{{ vm_facts.results }}"
  ignore_errors: yes

我的问题: 有没有办法创建一个临时库存与多个组和多个变量匹配给定的库存!?

1 个答案:

答案 0 :(得分:1)

更新 我破解了一条路:

- name: Add production VMs to inventory
  add_host: hostname={{item.ansible_facts.hw_eth0.ipaddresses[0] }} groups=temp
            new_ip="{{ item.ansible_facts.hw_name }}"
  with_items: "{{ vm_facts.results }}"
  ignore_errors: yes

这可行,因为机器名称等于{{play_host}}