Categories
DevOps

How to lookup specific element in Ansible dictionary or list of these

Lookup specific element in Ansible dictionary or list of these.

Sample playbook to look for elements from elements_to_look_for in first_variable, second_variable and third_variable.

$ cat playbook.yml
---
- hosts: localhost
  vars:
    elements_to_look_for:
      - alpha
      - beta
      - gamma
      - delta
    first_variable:
      - key: beta
        value: beta_value
      - key: gamma
        value: gamma_value
      - key: delta
        value: delta_value
    second_variable:
      alpha:
        value: "alpha_value"
      gamma:
        value: "gamma_value"
      delta:
        value: "delta_value"
    third_variable:
      - "alpha": "alpha_value"
      - "beta":  "beta_value"
      - "delta": "delta_value"

  tasks:
    - name: Display elements to look for
      debug:
        var: elements_to_look_for

    # first dictionary
    # beta, gamma, delta

    - name: Display first variable
      debug:
        var: first_variable

    - name: Look for elements missing in first variable
      debug:
        msg: "{{ element }}"
      when:
        - element not in (first_variable | map(attribute='key'))
      loop: "{{ elements_to_look_for }}"
      loop_control:
        loop_var: element

    - name: Look for elements present in first variable
      debug:
        msg: "{{ first_variable | selectattr('key','equalto',element) }}"
      when:
        - element in (first_variable | map(attribute='key'))
      loop: "{{ elements_to_look_for }}"
      loop_control:
        loop_var: element

    # second dictionary
    # alpha, gamma, delta

    - name: Display second dictionary
      debug:
        var: second_variable

    - name: Look for elements missing in second variable
      debug:
        msg: "{{ element }}"
      when:
         - element not in second_variable.keys()
      loop: "{{ elements_to_look_for }}"
      loop_control:
        loop_var: element

    - name: Look for elements present in second variable
      debug:
        msg: "{{ second_variable[element] }}"
      when:
        - element in second_variable.keys()
      loop: "{{ elements_to_look_for }}"
      loop_control:
        loop_var: element

    # third dictionary
    # alpha, beta, delta

    - name: Display third dictionary
      debug:
        var: third_variable

    - name: Look for elements missing in third variable
      debug:
        msg: "{{ element }}"
      when:
        - not (third_variable | selectattr(element, 'defined'))
      loop: "{{ elements_to_look_for }}"
      loop_control:
        loop_var: element

    - name: Look for elements present in third variable
      debug:
        msg: "{{ third_variable | selectattr(element, 'defined') }}"
      when:
        - third_variable | selectattr(element, 'defined')
      loop: "{{ elements_to_look_for }}"
      loop_control:
        loop_var: element

Execute this playbook to see how it works.

$ ansible-playbook playbook.yml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [localhost] ***********************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************
ok: [localhost]

TASK [Display elements to look for] ****************************************************************************************************************************
ok: [localhost] => {
    "elements_to_look_for": [
        "alpha",
        "beta",
        "gamma",
        "delta"
    ]
}

TASK [Display first variable] **********************************************************************************************************************************
ok: [localhost] => {
    "first_variable": [
        {
            "key": "beta",
            "value": "beta_value"
        },
        {
            "key": "gamma",
            "value": "gamma_value"
        },
        {
            "key": "delta",
            "value": "delta_value"
        }
    ]
}

TASK [Look for elements missing in first variable] *************************************************************************************************************
ok: [localhost] => (item=alpha) => {
    "msg": "alpha"
}
skipping: [localhost] => (item=beta) 
skipping: [localhost] => (item=gamma) 
skipping: [localhost] => (item=delta) 

TASK [Look for elements present in first variable] *************************************************************************************************************
skipping: [localhost] => (item=alpha) 
ok: [localhost] => (item=beta) => {
    "msg": [
        {
            "key": "beta",
            "value": "beta_value"
        }
    ]
}
ok: [localhost] => (item=gamma) => {
    "msg": [
        {
            "key": "gamma",
            "value": "gamma_value"
        }
    ]
}
ok: [localhost] => (item=delta) => {
    "msg": [
        {
            "key": "delta",
            "value": "delta_value"
        }
    ]
}

TASK [Display second dictionary] *******************************************************************************************************************************
ok: [localhost] => {
    "second_variable": {
        "alpha": {
            "value": "alpha_value"
        },
        "delta": {
            "value": "delta_value"
        },
        "gamma": {
            "value": "gamma_value"
        }
    }
}

TASK [Look for elements missing in second variable] ************************************************************************************************************
skipping: [localhost] => (item=alpha) 
ok: [localhost] => (item=beta) => {
    "msg": "beta"
}
skipping: [localhost] => (item=gamma) 
skipping: [localhost] => (item=delta) 

TASK [Look for elements present in second variable] ************************************************************************************************************
ok: [localhost] => (item=alpha) => {
    "msg": {
        "value": "alpha_value"
    }
}
skipping: [localhost] => (item=beta) 
ok: [localhost] => (item=gamma) => {
    "msg": {
        "value": "gamma_value"
    }
}
ok: [localhost] => (item=delta) => {
    "msg": {
        "value": "delta_value"
    }
}

TASK [Display third dictionary] ********************************************************************************************************************************
ok: [localhost] => {
    "third_variable": [
        {
            "alpha": "alpha_value"
        },
        {
            "beta": "beta_value"
        },
        {
            "delta": "delta_value"
        }
    ]
}

TASK [Look for elements missing in third variable] *************************************************************************************************************
skipping: [localhost] => (item=alpha) 
skipping: [localhost] => (item=beta) 
ok: [localhost] => (item=gamma) => {
    "msg": "gamma"
}
skipping: [localhost] => (item=delta) 

TASK [Look for elements present in third variable] *************************************************************************************************************
ok: [localhost] => (item=alpha) => {
    "msg": [
        {
            "alpha": "alpha_value"
        }
    ]
}
ok: [localhost] => (item=beta) => {
    "msg": [
        {
            "beta": "beta_value"
        }
    ]
}
skipping: [localhost] => (item=gamma) 
ok: [localhost] => (item=delta) => {
    "msg": [
        {
            "delta": "delta_value"
        }
    ]
}

PLAY RECAP *****************************************************************************************************************************************************
localhost                  : ok=11   changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0