调用第二本Playbook时如何将Ansible Playbook切换到另一台主机 [英] How to switch Ansible playbook to another host when calling second playbook

查看:200
本文介绍了调用第二本Playbook时如何将Ansible Playbook切换到另一台主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two playbooks - my first playbook iterates on the list of ESXi servers getting list of all VMs, and then passes that list to the second playbook, that should iterates on the IPs of the VMs. Instead it is still trying to execute on the last ESXi server. I have to switch host to that VM IP that I'm currently passing to the second playbook. Don't know how to switch... Anybody?

第一本剧本:

- 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服务器上运行...

I do receive the correct VM's ip_address but vmware_vm_facts is still trying to run on the ESXi server instead...

推荐答案

如果您无力为VM设置动态清单,则您的剧本应该扮演两个角色:一个角色用于收集VM的IP,另一个角色用于该VM上的任务. ,就像这样(伪代码):

If you can't afford to setup dynamic inventory for VMs, your playbook should have two plays: one for collecting VMs' IPs and another for tasks on that VMs, like this (pseudocode):

---
- 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组.在第二个播放中,我们为myvms组中的每个主机运行apt模块.

Within the first play we collect facts from each hypervisor and populate myvms group of inmemory inventory. Within second play we run apt module for every host in myvms group.

这篇关于调用第二本Playbook时如何将Ansible Playbook切换到另一台主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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