Just for completeness in case anyone else stumbles across this issue,
my final task was:
# Use long-form for task, Ansible issue #9172
- name: Write out SSLs to files
copy:
dest: /etc/ssl/{{item.tls.domain}}.pem
owner: root
group: root
mode: 0600
content: "{{item.tls.pem}}"
with_items: l7config.frontends
when: item.tls is defined
Thanks
Dane
On 23 October 2014 09:21, Dane Elwell <[email protected]> wrote:
> Hi Michael,
>
> Thanks for the response. Your example does work for me, however
> looking at your example I've managed to figure out why my original
> example wasn't working:
>
> - name: Write out SSLs to files
> copy: dest=/etc/ssl/{{item.tls.domain}}.pem owner=root
> group=root mode=0600 content={{item.tls.pem}}
> with_items:
> - l7config.frontends
> when: item.tls is defined
>
> Does not work, whereas:
>
> - name: Write out SSLs to files
> copy: dest=/etc/ssl/{{item.tls.domain}}.pem owner=root
> group=root mode=0600 content={{item.tls.pem}}
> with_items: l7config.frontends
> when: item.tls is defined
>
> Works fine. The with_items can't be a list itself, as Ansible doesn't
> appear to 'see' inside the dictionary then, and just treats it as a
> string.
>
> Didn't know about selectattr, so that's one to remember, but now I've
> got my original example working I'll stick with it as it seems more
> natural to me out of the two!
>
> Thanks
>
> Dane
>
>
> On 23 October 2014 00:39, Michael Hoglan <[email protected]> wrote:
>> Have you thought about breaking out some jinja expressions to manipulate the
>> structure? In particular the selectrattr() filter.
>> http://jinja.pocoo.org/docs/dev/templates/
>>
>> Here is a similar example I did
>> - hosts: localhost
>> connection: local
>> vars:
>> config:
>> frontend:
>> - id: 1
>> value: test1
>> tls:
>> - key: key1
>> domain: domain1
>> - id: 2
>> value: test2
>> backend:
>> - id: 3
>> - id: 4
>> tasks:
>> - debug: var=item
>> with_items: config.frontend
>> - debug: var=item
>> with_items: config.frontend | selectattr("tls", "defined") | list
>>
>> And then the output
>> (ansible)[ec2-user@ip-10-0-0-226 playbooks]$ ansible-playbook
>> test_withitems.yml
>>
>> PLAY [localhost]
>> **************************************************************
>>
>> GATHERING FACTS
>> ***************************************************************
>> ok: [localhost]
>>
>> TASK: [debug var=item]
>> ********************************************************
>> ok: [localhost] => (item={'tls': [{'domain': 'domain1', 'key': 'key1'}],
>> 'id': 1, 'value': 'test1'}) => {
>> "item": {
>> "id": 1,
>> "tls": [
>> {
>> "domain": "domain1",
>> "key": "key1"
>> }
>> ],
>> "value": "test1"
>> }
>> }
>> ok: [localhost] => (item={'id': 2, 'value': 'test2'}) => {
>> "item": {
>> "id": 2,
>> "value": "test2"
>> }
>> }
>>
>> TASK: [debug var=item]
>> ********************************************************
>> ok: [localhost] => (item={'tls': [{'domain': 'domain1', 'key': 'key1'}],
>> 'id': 1, 'value': 'test1'}) => {
>> "item": {
>> "id": 1,
>> "tls": [
>> {
>> "domain": "domain1",
>> "key": "key1"
>> }
>> ],
>> "value": "test1"
>> }
>> }
>>
>> PLAY RECAP
>> ********************************************************************
>> localhost : ok=3 changed=0 unreachable=0 failed=0
>>
>> That should give a list of all frontends that have a tls attribute defined.
>>
>> Thanks!
>> Michael
>>
>> --
>> 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 [email protected].
>> To post to this group, send email to [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/ansible-project/7d540723-1226-4574-a067-482c1c916c99%40googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/CAA93y1AUBMLiZMtN7Swt%3DCCAiPEoT9b5b2W7q8BG9SjqCB7wXA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.