Re: [ansible-project] How to loop over this local_fact in Ansible?

37 views
Skip to first unread message
Message has been deleted

Kai Stian Olstad

unread,
Feb 12, 2018, 4:48:15 AM2/12/18
to ansible...@googlegroups.com
On 12.02.2018 08:58, Pieter Steyn wrote:
> I need some help with looping my local_facts as I am struggling to
> match
> the key/value
>
> Example of my facts file:
>
> {
> "env": [
> "ppe",
> "trn"
> ],
> "websphere_dir": "/WebSphere8",
> "tech": "was8",
> "profiles": {
> "epn-was8-dmgr-ppe": {
> "env": "ppe"
> },
> "epn-was8-dmgr-trn": {
> "env": "trn"
> },
> "epn-was8-node-ppe-01": {
> "env": "ppe"
> },
> "epn-was8-node-trn-01": {
> "env": "trn"
> }
> }
> }
>
>
>
> Code in playbook:
>
> - set_fact:
> profiles_dicts: |
> {% set res = [] -%}
> {% for key in ansible_local.was.profiles.keys() -%}
> {% for value in ansible_local.was.profiles[key] -%}
> {% set ignored = res.extend([{'Server': key,
> 'ENV':value}])
> -%}
> {%- endfor %}
> {%- endfor %}
> {{ res }}

You could do something like this

- set_fact:
profiles_dicts: |
{% set res = [] -%}
{% for key, value in ansible_local.was.profiles.iteritems() -%}
{% set ignored = res.extend([{'Server': key,
'ENV':value.env}]) -%}
{%- endfor %}
{{ res }}


--
Kai Stian Olstad

J Hawkesworth

unread,
Feb 12, 2018, 4:48:43 AM2/12/18
to Ansible Project
Its not clear to me exactly what you want the result to be after your set_fact processing but I suggest you take a look at json_query plugin as you might be able to use this to just pick out the bits of the result that you need.  See http://docs.ansible.com/ansible/latest/playbooks_filters.html#json-query-filter  

One of the nice things about json_query is you can use the JMESPath examples page here: http://jmespath.org/examples.html to try out your queries (just paste your json into an input box and tinker with the query until you get results you need.

Hope this helps,

Jon

On Monday, February 12, 2018 at 7:58:47 AM UTC, Pieter Steyn wrote:
Hi guys
Output:

{
    "profiles_dicts": [
        {
            "ENV": "env",
            "Server": "epn-was8-dmgr-trn"
        },
        {
            "ENV": "env",
            "Server": "epn-was8-node-trn-01"
        },
        {
            "ENV": "env",
            "Server": "epn-was8-node-ppe-01"
        },
        {
            "ENV": "env",
            "Server": "epn-was8-dmgr-ppe"
        }


Is it possible to change my loop in order for my output to be ENV to match PPE or TRN and not env?

I am not sure how to achieve this


Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
0 new messages