You have the terms backwards.  You have a list of dicts, not a dict of
lists.  As such, you will want to use `sort` instead of `dictsort`

In python dicts are not guaranteed to maintain insertion order, depending
on the python version.  However, lists are guaranteed to maintain insertion
order.

So you have a few options, either manually sort them the way you want.  Or
use the `sort` filter.  One thing to note is that the `sort` filter, while
you can sort by attribute, it doesn't allow you to specify what value you
want first.  It just does alphanumeric sorting, which should be ok with
`present`/`absent`.  If you need more control, you would need to use
something like `selectattr` and concatenate results.

On Mon, Jan 21, 2019 at 2:49 PM java_cat33 <[email protected]> wrote:

> Hi - I've got a few questions....I have a variable (dictionary with lists
> I think is what it is? Is this the correct name/term?)
>
> dns_nat_dc1_records:
>   - { zone: 'test.com', name: 'server1', target: '192.168.1.10', type:
> 'ARecord', state: 'present' }
>   - { zone: 'test.com', name: 'server2', target: '192.168.1.11', type:
> 'ARecord', state: 'present' }
>   - { zone: 'test.com', name: 'crl', target: '192.168.1.12', type:
> 'ARecord', state: 'present' }
>   - { zone: 'test.com', name: 'server1', target: '192.168.100.10', type:
> 'ARecord', state: 'absent' }
>   - { zone: 'test.com', name: 'server2', target: '192.168.100.11', type:
> 'ARecord', state: 'absent' }
>   - { zone: 'test.com', name: 'crl', target: '192.168.100.12', type:
> 'ARecord', state: 'absent' }
>
>
>
> dns_nat_dc2_records:
>   - { zone: 'test.com', name: 'server1', target: '192.168.100.10', type:
> 'ARecord', state: 'present' }
>   - { zone: 'test.com', name: 'server2', target: '192.168.100.11', type:
> 'ARecord', state: 'present' }
>   - { zone: 'test.com', name: 'crl', target: '192.168.100.12', type:
> 'ARecord', state: 'present' }
>   - { zone: 'test.com', name: 'server1', target: '192.168.1.10', type:
> 'ARecord', state: 'absent' }
>   - { zone: 'test.com', name: 'server2', target: '192.168.1.11', type:
> 'ARecord', state: 'absent' }
>   - { zone: 'test.com', name: 'crl', target: '192.168.1.12', type:
> 'ARecord', state: 'absent' }
>
> I have a couple of plays.....
>
> - name: Ensure DNS NAT A/CNAME Records are present/absent for running in
> DC1
> win_dsc:
> resource_name: xDnsRecord
> Zone: "{{ item.zone }}"
> Name: "{{ item.name }}"
> Target: "{{ item.target }}"
> Type: "{{ item.type }}"
> Ensure: "{{ item.state }}"
> loop: "{{ dns_nat_dc1_records }}"
>
> when: (env == 'dev' or env == 'stg') and active_site == 'dc1'
>
> - name: Ensure DNS NAT A/CNAME Records are present/absent for running in
> DC2
> win_dsc:
> resource_name: xDnsRecord
> Zone: "{{ item.zone }}"
> Name: "{{ item.name }}"
> Target: "{{ item.target }}"
> Type: "{{ item.type }}"
> Ensure: "{{ item.state }}"
> loop: "{{ dns_nat_dc2_records }}"
>
> when: (env == 'dev' or env == 'stg') and active_site == 'dc2'
>
>
> What I've observed (whether I'm running either task in the play) is the
> DNS entries aren't present if I specify the state of "absent". If these
> lines are commented out the entries are created (and if running the play
> multiple times and specifying the remote site (e.g dc2 instead of dc1 to
> trigger the dc2 play task) duplicate DNS entries will be created of the
> hostname (different IP as expected).
>
> I've debugged the play and it appears to be looping through in correct
> order - I've read older posts on forums where apparently dictionary's are
> looped in random order (is this still true - I haven't seen it). For
> clarification - the vars I listed above - that is a dictionary with a list
> right?
>
> Can I loop through the var by first running through the key of "state" and
> value of "absent". I was trying to use dictsort on the loop but couldn't
> get the correct syntax. E.g loop: "{{ dns_nat_dc1_records |
> dictsort(false,'absent') }}"
>
> Have I missed anything obvious?
>
> --
> 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/19398950-3814-4a07-8435-a6b9fc49e56d%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/19398950-3814-4a07-8435-a6b9fc49e56d%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>


-- 
Matt Martz
@sivel
sivel.net

-- 
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/CAD8N0v-LcqE1FNHwgmtcygqXaHbtoJN1O200em8CikQLhyNVAw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to