get the value of Json vars ansible

27 views
Skip to first unread message

Brunet Julien

unread,
Mar 6, 2018, 9:09:59 AM3/6/18
to Ansible Project
Hello 

I faced a issue with items , i would like to get the value of Json vars.
But i can't access/get the value.

I write this ansible script :

---
# tasks file for ansible-f5

- name: Define  f5_environment vars
  set_fact:
    env_f5: "QLF"


- name: Collect BIG-IP facts
  bigip_facts:
    server: "{{f5_infos[env_f5].bigip_server}}"
    user: "{{f5_infos[env_f5].bigip_user}}"
    password: "{{f5_infos[env_f5].bigip_password}}"
    include: node
    validate_certs: "false"
  connection: local
  register: bigip_fact_out
  #delegate_to: localhost

- debug: var=bigip_fact_out


- name: Debug Msg Node
  debug:
      msg: "I have : {{ node['/Common/10.70.40.74'].address }} : {{ node['/Common/10.70.40.74'].monitor_status }} :  {{ node['/Common/10.70.40.74'].session_status }}"

- name: Printing Node Item
  shell: |
    #> /tmp/test
    echo "{{ item.address }}"  | sed  -e "s/\/Common\///g" >> /tmp/test
  with_items: "{{node}}"



i would like to get the list of address IP , {{ node[i].address }} where  i means the loop items.

could you please help me to solve this issue.


when i play the book i got this result : 

#ansible-playbook-2 play.yml

PLAY [localhost] ***************************************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************************************
ok: [localhost]

TASK [GetFact F5 with Ansible] *************************************************************************************************************************************

TASK [ansible-f5 : Define  f5_environment vars] ********************************************************************************************************************
ok: [localhost]

TASK [ansible-f5 : Collect BIG-IP facts] ***************************************************************************************************************************
ok: [localhost]

TASK [ansible-f5 : debug] ******************************************************************************************************************************************
ok: [localhost] => {
    "bigip_fact_out": {
        "ansible_facts": {
            "node": {
                "/Common/10.70.40.74": {
                    "address": "10.70.40.74",
                    "connection_limit": 0,
                    "description": "",
                    "dynamic_ratio": 1,
                    "monitor_instance": [],
                    "monitor_rule": {
                        "monitor_templates": [],
                        "quorum": 0,
                        "type": "MONITOR_RULE_TYPE_NONE"
                    },
                    "monitor_status": "MONITOR_STATUS_UNCHECKED",
                    "object_status": {
                        "availability_status": "AVAILABILITY_STATUS_BLUE",
                        "enabled_status": "ENABLED_STATUS_ENABLED",
                        "status_description": "Node address does not have service checking enabled"
                    },
                    "rate_limit": 0,
                    "ratio": 1,
                    "session_status": "SESSION_STATUS_ENABLED"
                },
                "/Common/10.70.50.148": {
                    "address": "10.70.50.148",
                    "connection_limit": 0,
                    "description": "",
                    "dynamic_ratio": 1,
                    "monitor_instance": [
                        {
                            "enabled_state": true,
                            "instance": {
                                "instance_definition": {
                                    "address_type": "ATYPE_EXPLICIT_ADDRESS",
                                    "ipport": {
                                        "address": "10.70.50.148",
                                        "port": 0
                                    }
                                },
                                "template_name": "/Common/gateway_icmp"
                            },
                            "instance_state": "INSTANCE_STATE_DOWN"
                        }
                    ],
                    "monitor_rule": {
                        "monitor_templates": [
                            "/Common/gateway_icmp"
                        ],
                        "quorum": 0,
                        "type": "MONITOR_RULE_TYPE_SINGLE"
                    },
                    "monitor_status": "MONITOR_STATUS_DOWN",
                    "object_status": {
                        "availability_status": "AVAILABILITY_STATUS_RED",
                        "enabled_status": "ENABLED_STATUS_ENABLED",
                        "status_description": "/Common/gateway_icmp: No successful responses received before deadline. @2018/03/05 10:25:50. "
                    },
                    "rate_limit": 0,
                    "ratio": 1,
                    "session_status": "SESSION_STATUS_ENABLED"
                },
                "/Common/192.168.108.49": {
                    "address": "192.168.108.49",
                    "connection_limit": 0,
                    "description": "",
                    "dynamic_ratio": 1,
                    "monitor_instance": [],
                    "monitor_rule": {
                        "monitor_templates": [],
                        "quorum": 0,
                        "type": "MONITOR_RULE_TYPE_NONE"
                    },
                    "monitor_status": "MONITOR_STATUS_UNCHECKED",
                    "object_status": {
                        "availability_status": "AVAILABILITY_STATUS_BLUE",
                        "enabled_status": "ENABLED_STATUS_ENABLED",
                        "status_description": "Node address does not have service checking enabled"
                    },
                    "rate_limit": 0,
                    "ratio": 1,
                    "session_status": "SESSION_STATUS_ENABLED"
                },
                "/Common/sv0133": {
                    "address": "10.70.50.128",
                    "connection_limit": 0,
                    "description": "",
                    "dynamic_ratio": 1,
                    "monitor_instance": [],
                    "monitor_rule": {
                        "monitor_templates": [],
                        "quorum": 0,
                        "type": "MONITOR_RULE_TYPE_NONE"
                    },
                    "monitor_status": "MONITOR_STATUS_UNCHECKED",
                    "object_status": {
                        "availability_status": "AVAILABILITY_STATUS_BLUE",
                        "enabled_status": "ENABLED_STATUS_ENABLED",
                        "status_description": "Node address does not have service checking enabled"
                    },
                    "rate_limit": 0,
                    "ratio": 1,
                    "session_status": "SESSION_STATUS_ENABLED"
                }
            }
        },
        "changed": false,
        "failed": false
    }
}

TASK [ansible-f5 : Debug Msg Node] *********************************************************************************************************************************
ok: [localhost] => {
    "msg": "I have : 10.70.40.74 : MONITOR_STATUS_UNCHECKED :  SESSION_STATUS_ENABLED"
}

TASK [ansible-f5 : Printing Node Item] *****************************************************************************************************************************

changed: [localhost] => (item=/Common/10.70.40.74)
changed: [localhost] => (item=/Common/10.70.50.148)
changed: [localhost] => (item=/Common/192.168.108.49)
changed: [localhost] => (item=/Common/SV0133)

PLAY RECAP *********************************************************************************************************************************************************
localhost                  : ok=6    changed=1    unreachable=0    failed=0

[root@sv0815 ansible-f5]#


if i can store/get the field  "address":  for each item in the file /tmp/test it would be great .....


Kai Stian Olstad

unread,
Mar 6, 2018, 2:06:53 PM3/6/18
to ansible...@googlegroups.com
On Tuesday, 6 March 2018 15.09.59 CET Brunet Julien wrote:
> *Hello *
>
> *I faced a issue with items , i would like to get the value of Json vars.*
> *But i can't access/get the value.*
>
> *I write this ansible script :*
> *i would like to get the list of address IP , {{ node[i].address }} where
> i means the loop items.*
>

<snip>

>
> *if i can store/get the field "address": for each item in the file
> /tmp/test it would be great .....*

I don't know where you get node variable from, but the node in bigip_fact_out is dictonary and not a list so you can do this

- copy:
dest: /tmp/test
content: |
{% for key, value in bigip_fact_out.ansible_facts.node | dictsort %}
{{ value.address }}
{% endfor %}

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages