jsonloopsansiblekey-pairfact

ansible - loop through fact and process each set of values


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 }}"

Solution

  • 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 }}"