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
Add a comment  | 

2 Answers 2

Reset to default 3

You 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