如何在调用第二个剧本时将Ansible剧本切换到另一个主持人

时间:2017-12-05 02:06:45

标签: ansible ansible-facts

我有两个剧本 - 我的第一本剧本在获取所有虚拟机列表的ESXi服务器列表上进行迭代,然后将该列表传递给第二个剧本,该剧本应该迭代虚拟机的IP。相反,它仍在尝试在最后一台ESXi服务器上执行。我必须将主机切换到我当前传递给第二个剧本的VM IP。不知道如何切换......任何人?

第一本剧本:

- name: get VM list from ESXi
  hosts: all

  tasks:
  - name: get facts
    vmware_vm_facts:
      hostname: "{{ inventory_hostname }}"
      username: "{{ ansible_ssh_user }}"
      password: "{{ ansible_ssh_pass }}"
    delegate_to: localhost
    register: esx_facts

  - name: Debugging data
    debug:
      msg: "IP of {{ item.key }} is {{ item.value.ip_address }} and is {{ item.value.power_state }}"
    with_dict: "{{ esx_facts.virtual_machines }}"

  - name: Passing data to include file
    include: includeFile.yml ip_address="{{ item.value.ip_address }}"
    with_dict: "{{ esx_facts.virtual_machines }}"

我的第二本剧本:

- name: <<Check the IP received
  debug:
    msg: "Received IP: {{ ip_address }}"

- name: <<Get custom facts
  vmware_vm_facts:
    hostname: "{{ ip_address }}"
    username: root
    password: passw
    validate_certs: False
  delegate_to: localhost
  register: custom_facts

我确实收到了正确的VM的ip_address,但vmware_vm_facts仍在尝试在ESXi服务器上运行...

1 个答案:

答案 0 :(得分:3)

如果您无法为VM设置动态广告资源,那么您的游戏手册应该有两个游戏:一个用于收集虚拟机和游戏。 IP和另一个用于VM上的任务,如下所示(伪代码):

---
- hosts: hypervisors
  gather_facts: no
  connection: local
  tasks:
    - vmware_vm_facts:
        ... params_here ...
      register: vmfacts
    - add_host:
        name: "{{ item.key }}"
        ansible_host: "{{ item.value.ip_address }}"
        group: myvms
      with_dict: "{{ vmfacts.virtual_machines }}"

- hosts: myvms
  tasks:
    - apt:
        name: "*"
        state: latest

在第一个游戏中,我们从每个虚拟机管理程序收集事实并填充myvms内存库存组。在第二次播放中,我们为apt组中的每个主机运行myvms模块。