ansibl库存:使用add_host或group_by创建具有多个组的临时库存 [英] ansibl inventory : create temporary inventory with multiple groups with add_host or group_by

查看:40
本文介绍了ansibl库存:使用add_host或group_by创建具有多个组的临时库存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据指定任务分配了{{创建新主机}}invonteroy文件,例子

I have an assignment to provision {{ create new hosts }} according to a given invonteroy file, example

[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地址.我想做什么

Here is the problem. The inventory file contains set of ip addresses that do not exists yet. What I want to do

- 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 

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

requires administrative permissions which I don't have. The only way to hack around this issue is to try and create an in memory or temporary inventory during provisioning task. Here is my role:

- 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

我的问题:有什么方法可以创建具有多个组和与给定清单相匹配的多用途变量的临时清单!

my question: is there any way to create a temp inventory with multiple groups and mutilple variables that matches the given inventory!?

推荐答案

更新我破解了一种方法:

update I hacked a way around it:

- 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}}

this works since the machine name is equal to {{ play_host }}

这篇关于ansibl库存:使用add_host或group_by创建具有多个组的临时库存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆