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

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

问题描述

我有一项任务要根据给定的条件配置 {{ create new hosts }}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我绕过了它:

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天全站免登陆