Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select dictionary from list of dictionaries where key = value in ansible playbook

I've seen a few other questions that are similar to my need, however, I'm still having a hard time figuring out how I need to get exactly what I need from this JSON. Here's my Ansible debug task:

    - debug:
        msg: "{{ foreman_environments.stdout }}"

and here's the output:

ok: [127.0.0.1] => { "msg": "[{'id': 12, 'name': 'CDE', 'label': 'CDE', 'permissions': {'readable': True}}, {'id': 7, 'name': 'QA3', 'label': 'QA3', 'permissions': {'readable': True}}, {'id': 4, 'name': 'Library', 'label': 'Library', 'permissions': {'readable': True}}, {'id': 6, 'name': 'Development', 'label': 'Development', 'permissions': {'readable': True}}, {'id': 8, 'name': 'Production', 'label': 'Production', 'permissions': {'readable': True}}]" }

I need to capture the 'id' value where the 'name' key equals x. In this exact case, I want to get the 'id' of QA3, but I might need to capture the 'id' of other environments as well. So I need to take this list of environments and return the value of 'id' where name == some name. Then I need to search for that 'id' in a different list that contains all the other versions.

Here's what I've tried:

    - debug:
        msg: "{{ foreman_environments.stdout |
                 selectattr('name', 'equalto', 'QA3') |
                 list | last | to_json }}"

and here's the output I get:

{"msg": "The task includes an option with an undefined variable. The error was: 'str object' has no attribute 'name'\n\nThe error appears to be in '/opt/git/ci_cd_ansible/playbook_publish_foreman_view.yml': line 36, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - debug:\n ^ here\n"}

I want to register the result and then use that registered result to functionally do this same thing but against a different set of data. Can someone help me understand what I'm missing?

EDIT:

I've changed my method to use

  "{{ foreman_environments.stdout |
      selectattr('name', 'equalto', 'QA3') }}"

and that returns a generator object. If I try to translate that to a list, though, I get an error:

fatal: [127.0.0.1]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'str object' has no attribute 'name'\n\nThe error appears to be in '/opt/git/ci_cd_ansible/playbook_publish_foreman_view.yml': line 36, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - debug:\n ^ here\n"}

like image 414
Todd Pettit Avatar asked Nov 30 '25 17:11

Todd Pettit


1 Answers

The filter json_query gives the same result

  id_qa3: "{{ foreman_environments.stdout|
              json_query('[?name == `QA3`].id')|
              first }}"

Example of a complete playbook for testing

shell> cat pb.yml
- hosts: all

  vars:

    foreman_environments:
      stdout:
        - id: 12
          label: CDE
          name: CDE
          permissions: {readable: true}
        - id: 7
          label: QA3
          name: QA3
          permissions: {readable: true}
        - id: 4
          label: Library
          name: Library
          permissions: {readable: true}
        - id: 6
          label: Development
          name: Development
          permissions: {readable: true}
        - id: 8
          label: Production
          name: Production
          permissions: {readable: true}

    id_qa3_a: "{{ foreman_environments.stdout|
                  json_query('[?name == `QA3`].id')|
                  first }}"

    id_qa3_b: "{{ (foreman_environments.stdout|
                  selectattr('name', '==', 'QA3')|
                  first).id }}"

  tasks:

    - debug:
        var: id_qa3_a
    - debug:
        var: id_qa3_b

gives

shell> ansible-playbook pb.yml -l test_11

PLAY [all] ************************************************************************************

TASK [debug] **********************************************************************************
ok: [test_11] => 
  id_qa3_a: '7'

TASK [debug] **********************************************************************************
ok: [test_11] => 
  id_qa3_b: '7'

PLAY RECAP ************************************************************************************
test_11: ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
like image 144
Vladimir Botka Avatar answered Dec 02 '25 05:12

Vladimir Botka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!