如何在调用第二个playbook时将Ansible playbook切换到另一个主机

如何在调用第二个playbook时将Ansible playbook切换到另一个主机,ansible,ansible-facts,Ansible,Ansible Facts,我有两个剧本-我的第一个剧本在ESXi服务器列表上迭代,得到所有虚拟机的列表,然后将该列表传递给第二个剧本,该剧本应该在虚拟机的IP上迭代。相反,它仍在尝试在最后一台ESXi服务器上执行。我必须将主机切换到当前传递给第二个剧本的VM IP。不知道如何切换。。。有人吗 第一个剧本: - name: get VM list from ESXi hosts: all tasks: - name: get facts vmware_vm_facts: hostname:

我有两个剧本-我的第一个剧本在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
我的第二个剧本:

- 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

-name:如果您负担不起为虚拟机设置动态资源清册,您的剧本应该有两个剧本:一个用于收集虚拟机的IP,另一个用于在该虚拟机上执行任务,如以下(伪代码):


在第一个重头戏中,我们从每个虚拟机监控程序收集事实,并填充
myvms
内存库存组。在第二个重头戏中,我们为
myvms
组中的每个主机运行
apt
模块

首先,请使用适当的术语:你有一个剧本和一个剧本,包括一个任务文件。所以您声称您将任务委托给localhost,但它在别处执行?如果是这样,您可以将发布的代码减少80%并获得相同的结果。此外,
以及当然,以这种方式运行
vmware\u vm\u facts
两次的整个想法似乎很奇怪。谢谢,康斯坦丁!我还必须添加ansible_ssh_用户并通过,b/c用户显示为“无”