Warm tip: This article is reproduced from stackoverflow.com, please click
ansible json loops key-pair fact

ansible

发布于 2020-04-08 23:43:41

I have set a fact

set_fact:
  props: "{{ parse_result.stdout | from_json }}"

The fact looks something like this:

{
    "changed": false,
    "ansible_facts": {
        "props": [
            {
                "build_number": "1.0.0.2",
                "build_name": "AppXYZ"
            },
            {
                "build_number": "1.2.0.2",
                "build_name": "AppABC"
            }
        ]
    },
    "_ansible_no_log": false
}

I want to loop through the fact and process each set of build_name and build_number. I have tried the below code, but sometimes it would throw me an error like 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'build_definition_name'. What am I doing wrong?

my_deploy_module:
  build_name: "{{ item.build_name }}"
  build_number: "{{ item.build_number }}"
with_items: "{{ props }}"
Questioner
TDN
Viewed
53
TDN 2020-02-01 12:47

I came to find answer to my own question in this post:

https://serverfault.com/questions/927855/ansible-loop-over-custom-facts

Here is the syntax:

my_deploy_module:
  build_name: "{{ item.build_name }}"
  build_number: "{{ item.build_number }}"
with_items: "{{ props | json_query('[*]') | flatten }}"