[ansible-project] accessing yaml dictionary in playbook

2020-06-12 Thread Doug OLeary
Hi

I have a dictionary of disks in my host vars:

disks:
  sdc:
dev: sdc
vg: vg01
lv: app
mp: '/opt/app'
  sdd:
dev: sdd
vg: vg02tmp
lv: shared
mp: '/opt/app/oracle/shared'

A **very** basic playbook, just to access the dictionary::

  tasks:
  - debug:
  msg: "disk name: {{item}}"
loop:
  - "{{disks}}"


shows good results so I'm pretty sure I have the dictionary defined 
correctly:

TASK [debug] 
**
ok: [mynewhost] => (item=[{'key': 'sdc', 'value': {'dev': 'sdc', 'vg': 
'vg01', 'lv': 'app', 'mp': '/opt/app'}}, {'key': 'sdd', 'value': {'dev': 
'sdd', 'vg': 'vg02tmp', 'lv': 'shared', 'mp': '/opt/app/oracle/shared'}}]) 
=> {
"msg": "disk name: [{'key': 'sdc', 'value': {'dev': 'sdc', 'vg': 
'vg01', 'lv': 'app', 'mp': '/opt/app'}}, {'key': 'sdd', 'value': {'dev': 
'sdd', 'vg': 'vg02tmp', 'lv': 'shared', 'mp': '/opt/app/oracle/shared'}}]"
}

How do I loop over the dictionary key?  in this case, it'd be sdc and 
sdd...  I've been trying iteration of {{item.key}}, and {{disks.key}} but 
nothing has worked yet.  

Thanks for any hints/tips/suggestions.

Doug O'Leary 




-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/70b7a9d0-45a9-424f-8757-c46b17401e32o%40googlegroups.com.


Re: [ansible-project] accessing yaml dictionary in playbook

2020-06-12 Thread Stefan Hornburg (Racke)
On 6/12/20 2:35 PM, Doug OLeary wrote:
> Hi
> 
> I have a dictionary of disks in my host vars:
> 
> |
> disks:
>   sdc:
>     dev: sdc
>     vg: vg01
>     lv: app
>     mp: '/opt/app'
>   sdd:
>     dev: sdd
>     vg: vg02tmp
>     lv: shared
>     mp: '/opt/app/oracle/shared'
> |
> 
> A **very** basic playbook, just to access the dictionary::
> 
> |
>   tasks:
>   -debug:
>       msg:"disk name: {{item}}"
>     loop:
>       -"{{disks}}"
> 
> |
> 
> shows good results so I'm pretty sure I have the dictionary defined correctly:
> 
> |
> TASK
> [debug]**
> ok:[mynewhost]=>(item=[{'key':'sdc','value':{'dev':'sdc','vg':'vg01','lv':'app','mp':'/opt/app'}},{'key':'sdd','value':{'dev':'sdd','vg':'vg02tmp','lv':'shared','mp':'/opt/app/oracle/shared'}}])=>{
>     "msg":"disk name: [{'key': 'sdc', 'value': {'dev': 'sdc', 'vg': 'vg01', 
> 'lv': 'app', 'mp': '/opt/app'}}, {'key':
> 'sdd', 'value': {'dev': 'sdd', 'vg': 'vg02tmp', 'lv': 'shared', 'mp': 
> '/opt/app/oracle/shared'}}]"
> }
> |
> 
> How do I loop over the dictionary key?  in this case, it'd be sdc and sdd...  
> I've been trying iteration of
> {{item.key}}, and {{disks.key}} but nothing has worked yet.  
> 
> Thanks for any hints/tips/suggestions.

Old style:

with_dict: "{{ disks }}"

New style:

loop: "{{ disks | dict2items }}"

In both cases the key is in item.key and values can be accessed with 
item.value, e.g item.value.dev.

Reference: 
https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#iterating-over-a-dictionary

Regards
  Racke

> 
> Doug O'Leary 
> 
> 
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to
> ansible-project+unsubscr...@googlegroups.com 
> .
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/70b7a9d0-45a9-424f-8757-c46b17401e32o%40googlegroups.com
> .


-- 
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/c8575e46-6e3e-39c6-47a4-649ffa0b0a5c%40linuxia.de.


signature.asc
Description: OpenPGP digital signature