我正在使用Ansible在ESXi中部署一些虚拟机。我将为库存中的每个项目分配名为iso和floppy_image的变量。变量名是提供的代码片段中的安装程序。
我想遍历清单并创建一个变量“安装媒体”,该变量需要复制到ESXi。之后,我将变量发送到另一个剧本,该剧本将复制文件。
我目前在创建数据结构时遇到问题。我希望它看起来如下:
{"esxi1": [file1, ile2], "esxi2": [file1,file2]}
我尝试使用Combine过滤器,该过滤器创建字典键,但不追加到数组。我的代码段创建了以下数据结构,但它不是我要使用的格式。
{"esxi1": {"file1": true, "file2": true}, "esxi2": {"file1": true, "file2": true}}
---
- name: create variable for installation media
gather_facts: no
hosts: localhost
tasks:
- set_fact:
files: {}
- name: playbook to deploy domain controllers
connection: local
gather_facts: no
hosts: domain_controller
vars:
base_path: "{{ playbook_dir }}/../../../"
tasks:
- name: create/set string variable for vsphere
set_fact:
vsphere: "{{ item }}"
when: >
(ansible_host in hostvars[item]['virtual_machines']) or
(inventory_hostname in hostvars[item]['virtual_machines'])
loop: "{{ groups.vsphere }}"
- debug: var=vsphere
- debug: var=installer
- name: set dictionary key of installation media
set_fact:
files: "{{ hostvars['localhost']['files'] | combine({ vsphere: { installer: true }}) }}"
delegate_to: localhost
delegate_facts: yes
when: >
(installer is defined) and
(hostvars['localhost']['files'][vsphere] is not defined) or
(installer not in hostvars['localhost']['files'][vsphere])
- name: results
gather_facts: no
hosts: localhost
tasks:
- debug: var=files