DEV Community

drewmullen
drewmullen

Posted on

creating a sublist from a deep element in Ansible

ansible docs
requirements: pip install jmespath

---
- hosts: localhost
  gather_facts: false
  vars:
    parent_var:
      child_list: [{name: 'test'},{ name: 'test2'}]

  tasks:
    - debug:
        msg:  "{{ parent_var | json_query('child_list[].name') }}"
Enter fullscreen mode Exit fullscreen mode
ansible-playbook list_test.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 [debug] ****************************************
ok: [localhost] =>
  msg:
  - test
  - test2

Enter fullscreen mode Exit fullscreen mode

Special thanks to https://github.com/derekmwright

Top comments (0)