使用ansible访问字典文件列表



我试着从文件中获取值,我们把它命名为"test。yaml">

文件看起来像这样(很抱歉输出很长,但它是包含所有模式和结构的最短路径):

---
results:
- failed: false
item: XXX.XX.XX.XX
invocation:
module_args:
validate_certs: false
vm_type: vm
show_tag: false
username: DOMAINdomain-user
proxy_host:
proxy_port:
show_attribute: false
password: VALUE_SPECIFIED_IN_NO_LOG_PARAMETER
port: XXX
folder:
hostname: XXX.XX.XX.XX
changed: false
virtual_machines:
- ip_address: XXX.XX.XX.XX
mac_address:
- XX:XX:XX:aa:XX:XX
uuid: XXXX-XX-XX-XXXX-XXXXX
guest_fullname: Red Hat Enterprise Linux X (XX-bit)
moid: vm-XXX
folder: "/DOMAIN-INTERXION/vm"
cluster:
attributes: {}
power_state: poweredOn
esxi_hostname: esx.hostname 
tags: []
guest_name: VMnameXX
vm_network:
XX:XX:XX:aa:XX:XX:
ipv6:
- XX::XXX:XX:XXXX
ipv4:
- XXX.XX.XX.XX

例如,我希望有这样的内容:

results.invocation.virtual_machines.ip_address
results.invocation.module_args.user_name

我试了所有的东西,但它不工作:)

最后一次尝试是:

---
- name: demo how register works
hosts: localhost
tasks:
- name: Include all .json and .jsn files in vars/all and all nested directories (2.3)
include_vars: 
file: test.yml
name: vm
- name: debug
debug: 
msg: "{{ item.0.item }}"
with_subelements:
- "{{ vm.results }}"
- virtual_machines
register: subelement

在你的结构和修复一些错误后:

results.invocation.virtual_machines。ip_addressisresults[0].virtual_machines[0].ip_addressresults.invocation.module_args。user_nameisresults[0].invocation.module_args.username

(results和virtual_machines是数组,写results[0]或results。0相同)

所以剧本做工作的例子:

- name: vartest
hosts: localhost
tasks: 
- name: Include all .json and .jsn files in vars/all and all nested directories (2.3)
include_vars: 
file: test.yml
name: vm
- name: ip
set_fact:
ipadress: "{{ vm.results[0].virtual_machines[0].ip_address }}"
- name: username
set_fact:
username: "{{ vm.results[0].invocation.module_args.username }}"
- name: display
debug:
msg: "ip: {{ ipadress }}  and username: {{ username }}"

结果:

ok: [localhost] => 
msg: 'ip: XXX.XX.XX.XX  and username: DOMAINdomain-user'

相关内容

  • 没有找到相关文章

最新更新