Accessing a sub-variable from a JSON output

27 views
Skip to first unread message

JS

unread,
Dec 18, 2018, 6:15:18 AM12/18/18
to Ansible Project
Hello

I have a variable that contains output from a json file.

- name: set fact
  set_fact:
    test123: "{{ jsonVar | json_query(name_query) }}"
  vars:
    name_query: "[].{name: name, ssh_url_to_repo: ssh_url_to_repo namespace_path: namespace.full_path }"



This is the output the command above produces:
ok: [host1] => {
    "ansible_facts": {
        "test123": [
            {
                "name": "appscomb",
                "namespace_path": "aws/docker",
                "ssh_url_to_repo": "ssh://g...@gitlab.example.local:4444/aws/docker/appscomb.git"
            },
            {
                "name": "appscomb",
                "namespace_path": "software-repositories",
                "ssh_url_to_repo": "ssh://g...@gitlab.example.local:4444/software-repositories/appscomb.git"
            },
            {
                "name": "java",
                "namespace_path": "software-repositories",
                "ssh_url_to_repo": "ssh://g...@gitlab.example.local:4444/software-repositories/java.git"
            },
            {
                "name": "DMI",
                "namespace_path": "dream-it/docker",
                "ssh_url_to_repo": "ssh://g...@gitlab.example.local:4444/dream-it/docker/dmi.git"
            },
            {
                "name": "gitlab",
                "namespace_path": "aws/docker",
                "ssh_url_to_repo": "ssh://g...@gitlab.example.local:4444/aws/docker/gitlab.git"
            },
            {
                "name": "os-management",
                "namespace_path": "aws/ansible",
                "ssh_url_to_repo": "ssh://g...@gitlab.example.local:4444/aws/ansible/os-management.git"
            },
            {
                "name": "appscomb",
                "namespace_path": "aws/ansible",
                "ssh_url_to_repo": "ssh://g...@gitlab.example.local:4444/aws/ansible/appscomb.git"
            },
            {
                "name": "xen-management",
                "namespace_path": "aws/ansible",
                "ssh_url_to_repo": "ssh://g...@gitlab.example.local:4444/aws/ansible/xen-management.git"
            },
            {
                "name": "aws-docker",
                "namespace_path": "aws/docker",
                "ssh_url_to_repo": "ssh://g...@gitlab.example.local:4444/aws/docker/aws-docker.git"
            },
            {
                "name": "dbarchery",
                "namespace_path": "dream-it/ansible",
                "ssh_url_to_repo": "ssh://g...@gitlab.example.local:4444/dream-it/ansible/dbarchery.git"
            },
            {
                "name": "dbarchery",
                "namespace_path": "dream-it/docker",
                "ssh_url_to_repo": "ssh://g...@gitlab.example.local:4444/dream-it/docker/dbarchery.git"
            }
        ]
    },
    "changed": false
}



How do I access for example, the name or namespace_path or ssh_url_to_repo of any or all of the above?

I have tried this:

- name: debug
  debug:
    msg: "{{ test123.name }}"
#  with_items: "{{ test123 }}"



and this:
- name: debug
  debug:
    msg: "{{ test123['name'] }}"
#  with_items: "{{ test123 }}"


But they both seem to be failing with the following error:
fatal: [host1]: FAILED! => {
    "msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'name'\n\nThe error appears to have been in '/home/gitlab/roles/gitlab-backup_repos/tasks/main.yml': line 25, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: debug\n  ^ here\n"


Many Thanks in Advance!
JS

Eric Brewer

unread,
Dec 18, 2018, 11:39:09 AM12/18/18
to Ansible Project
I think you need to declare ansible_facts before your variable, and then you need to iterate through the array so try this:

- name: debug
  debug
:
    msg
: "{{ item['name'] }}"
  with_items
: "{{ ansible_facts['test123'] }}"


And if you don't want to iterate through all of them, you need to call the specific array location since you have placed them in a list:

ansible_facts['test123'][0]['name']

That should get you the first one.

JS

unread,
Dec 19, 2018, 6:39:58 AM12/19/18
to Ansible Project
Hi Eric

Brilliant! Thank you very much! - That put me down the right path.

Once I understood your syntax, I managed to get all of these to work:

- name: debug 0
  debug:
    var: test123


- name: debug 1
  debug:
    var: test123[0].namespace_path

- name: debug 2
  debug:
    msg: The value is "{{ test123[0].namespace_path }}"

- name: debug 3
  debug:
    var:  item['namespace_path']
  with_items: "{{ test123 }}"

- name: debug 4
  debug:
    msg: The value is "{{ item['name'] }}"
  with_items: "{{ test123 }}"


Once again, your assistance has been very much appreciated!

Regards
Jinal
Reply all
Reply to author
Forward
0 new messages