admin管理员组文章数量:1122832
Here is my sample YAML file input.yaml
.
name: "My name"
surname: "Bar"
address:
city: "My city"
street: "My street"
number: 1
I am trying print full path to each key and its value, while iterating over it.
list.yaml
- name: "Playing with Ansible"
hosts: localhost
tasks:
- name: "Reading yaml file"
ansible.builtin.shell: "cat input.yaml"
register: input_yaml_content
- name: "Parse yaml into variable"
set_fact:
my_yaml: "{{ input_yaml_content.stdout | from_yaml }}"
- name: "Finding all paths"
set_fact:
paths: "{{ my_yaml | ansible.utils.to_paths }}"
- name: "Printing key-values"
debug:
msg: "Key: {{ item }}, value: **{{ my_yaml[item] }}**"
with_items: "{{ paths }}"
How can I print the value of each path?
Above code works for name, surname. But it fails for address.
ok: [localhost] => (item=name) => {
"msg": "Key: name, value: My name"
}
ok: [localhost] => (item=surname) => {
"msg": "Key: surname, value: Bar"
}
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'address.city'. 'dict object' has no attribute 'address.city'
Here is my sample YAML file input.yaml
.
name: "My name"
surname: "Bar"
address:
city: "My city"
street: "My street"
number: 1
I am trying print full path to each key and its value, while iterating over it.
list.yaml
- name: "Playing with Ansible"
hosts: localhost
tasks:
- name: "Reading yaml file"
ansible.builtin.shell: "cat input.yaml"
register: input_yaml_content
- name: "Parse yaml into variable"
set_fact:
my_yaml: "{{ input_yaml_content.stdout | from_yaml }}"
- name: "Finding all paths"
set_fact:
paths: "{{ my_yaml | ansible.utils.to_paths }}"
- name: "Printing key-values"
debug:
msg: "Key: {{ item }}, value: **{{ my_yaml[item] }}**"
with_items: "{{ paths }}"
How can I print the value of each path?
Above code works for name, surname. But it fails for address.
ok: [localhost] => (item=name) => {
"msg": "Key: name, value: My name"
}
ok: [localhost] => (item=surname) => {
"msg": "Key: surname, value: Bar"
}
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'address.city'. 'dict object' has no attribute 'address.city'
Share
Improve this question
edited yesterday
jonrsharpe
122k30 gold badges263 silver badges470 bronze badges
asked yesterday
ArjunArjun
6,73110 gold badges34 silver badges36 bronze badges
2 Answers
Reset to default 3You are making this overly complicated with too many tasks. Just get the file, parse it as yaml and transform it to paths:
The following playbook
---
- name: Yaml as paths
hosts: localhost
gather_facts: false
vars:
my_data: "{{ lookup('ansible.builtin.file', 'input.yaml') | from_yaml }}"
tasks:
- name: Print key/values
ansible.builtin.debug:
var: my_data | ansible.utils.to_paths
gives using your above file as input:
$ ansible-playbook list.yaml
PLAY [Yaml as paths] ***********************************************************************************************************************************************************
TASK [Print key/values] ********************************************************************************************************************************************************
ok: [localhost] => {
"my_data | ansible.utils.to_paths": {
"address.city": "My city",
"address.number": 1,
"address.street": "My street",
"name": "My name",
"surname": "Bar"
}
}
PLAY RECAP *********************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
If you still want to loop over each entry for whatever reason, simply transform the paths dict to a list of key/values:
---
- name: Yaml as paths
hosts: localhost
gather_facts: false
vars:
my_data: "{{ lookup('ansible.builtin.file', 'input.yaml') | from_yaml }}"
tasks:
- name: Loop over key/values
ansible.builtin.debug:
msg: "{{ item.key }} has value {{ item.value }}"
loop: "{{ my_data | ansible.utils.to_paths | dict2items }}"
gives:
$ ansible-playbook list.yaml
PLAY [Yaml as paths] ***********************************************************************************************************************************************************
TASK [Loop over key/values] ****************************************************************************************************************************************************
ok: [localhost] => (item={'key': 'name', 'value': 'My name'}) => {
"msg": "name has value My name"
}
ok: [localhost] => (item={'key': 'surname', 'value': 'Bar'}) => {
"msg": "surname has value Bar"
}
ok: [localhost] => (item={'key': 'address.city', 'value': 'My city'}) => {
"msg": "address.city has value My city"
}
ok: [localhost] => (item={'key': 'address.street', 'value': 'My street'}) => {
"msg": "address.street has value My street"
}
ok: [localhost] => (item={'key': 'address.number', 'value': 1}) => {
"msg": "address.number has value 1"
}
PLAY RECAP *********************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Here is the code of to_path
. It is a python dict. Therefore, by using paths[item]
- name: "Playing with Ansible"
hosts: localhost
tasks:
- name: "Reading yaml file"
ansible.builtin.shell: "cat input.yaml"
register: input_yaml_content
- name: "Parse yaml into variable"
set_fact:
my_yaml: "{{ input_yaml_content.stdout | from_yaml }}"
- name: "Finding all paths"
set_fact:
paths: "{{ my_yaml | ansible.utils.to_paths }}"
- name: "Printing key-values"
debug:
msg: "Key: {{ item }}, value: {{ paths[item] }}"
with_items: "{{ paths }}"
本文标签: ansibleLook up YAML file with given pathStack Overflow
版权声明:本文标题:ansible - Look up YAML file with given path - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736281729a1926412.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论