1
votes

I'm querying an API with Ansible command's command. The API returns a JSON object with network information.

I'd like to get a server's private ip based on its public ip. I know this is possible with JSON query filter but I can't figure out how.

The code:

- name: Get RPN topology
  command: 'curl -X GET -H "Authorization: Bearer {{ onlineApiToken }}" "https://api.online.net/api/v1/rpn/group"'
  register: RPN
  delegate_to: 127.0.0.1

This is what RPN.stdout output looks like:

    TASK [debug] ****************************************************************************************
ok: [ps1] => {
    "changed": false, 
    "msg": [
        {
            "id": 7406, 
            "members": [
                {
                    "id": 0000, 
                    "ip": "x.x.x.x", 
                    "owner": "buzut", 
                    "private_ip": "10.91.154.39", 
                    "speed": 100, 
                    "status": "active"
                }, 
                {
                    "id": 1111, 
                    "ip": "y.y.y.y", 
                    "owner": "buzut", 
                    "private_ip": "10.91.120.148", 
                    "speed": 100, 
                    "status": "active"
                }, 
                {
                    "id": 2222, 
                    "ip": "z.z.z.z", 
                    "owner": "buzut", 
                    "private_ip": "10.91.165.215", 
                    "speed": 1000, 
                    "status": "active"
                }
            ], 
            "name": "MySQL", 
            "owner": "buzut", 
            "shared": false, 
            "status": "updating"
        }
    ]
}

The question: how do I get a server's private ip, based on its public one?

2

2 Answers

2
votes

You probably want to take some time to read through the JMESPath Examples to familiarize yourself with the syntax supported by the json_query filter. The following gives you the result you want:

- hosts: localhost
  gather_facts: false
  vars:
    RPN:
      stdout: >-
        [
          {
            "id": 7406,
            "members": [
              {
                "id": 0,
                "ip": "x.x.x.x",
                "owner": "buzut",
                "private_ip": "10.91.154.39",
                "speed": 100,
                "status": "active"
              },
              {
                "id": 1111,
                "ip": "y.y.y.y",
                "owner": "buzut",
                "private_ip": "10.91.120.148",
                "speed": 100,
                "status": "active"
              },
              {
                "id": 2222,
                "ip": "z.z.z.z",
                "owner": "buzut",
                "private_ip": "10.91.165.215",
                "speed": 1000,
                "status": "active"
              }
            ],
            "name": "MySQL",
            "owner": "buzut",
            "shared": false,
            "status": "updating"
          }
        ]

  tasks:
    - name: lookup server based on public ip
      debug:
        var: item
      with_items: "{{RPN.stdout|from_json|json_query(public_to_private_ip)}}"
      vars:
        public_to_private_ip: >-
          [].members[?ip=='{{ public_ip }}'].private_ip

If I call this like:

ansible-playbook playbook.yml -e public_ip=y.y.y.y

I get:

TASK [lookup server based on public ip] ****************************************
ok: [localhost] => (item=10.91.120.148) => {
    "item": "10.91.120.148"
}

And if I call it as:

ansible-playbook playbook.yml -e public_ip=z.z.z.z

I get:

TASK [lookup server based on public ip] ****************************************
ok: [localhost] => (item=10.91.165.215) => {
    "item": "10.91.165.215"
}
0
votes
---
- name: play1
  hosts: localhost
  gather_facts: false
  vars:
    public_ip: "x.x.x.x"

  tasks:

  - name: Print data
    debug: var=RPN

  - name: Lookup value in json var
    debug: var=item
    with_items: "{{RPN|json_query(public_to_private_ip)}}"
    vars:
      public_to_private_ip: "members[?ip=='{{ public_ip }}'].private_ip"

Gives you:

PLAY [play1] *******************************************************************

TASK [Print data] **************************************************************
ok: [localhost] => {
    "RPN": {
        "id": 7406,
        "members": [
            {
                "id": "0000",
                "ip": "x.x.x.x",
                "owner": "buzut",
                "private_ip": "10.91.154.39",
                "speed": 100,
                "status": "active"
            },
            {
                "id": 1111,
                "ip": "y.y.y.y",
                "owner": "buzut",
                "private_ip": "10.91.120.148",
                "speed": 100,
                "status": "active"
            },
            {
                "id": 2222,
                "ip": "z.z.z.z",
                "owner": "buzut",
                "private_ip": "10.91.165.215",
                "speed": 1000,
                "status": "active"
            }
        ],
        "name": "MySQL",
        "owner": "buzut",
        "shared": false,
        "status": "updating"
    }
}

TASK [Lookup value in json var] ************************************************
ok: [localhost] => (item=10.91.154.39) => {
    "item": "10.91.154.39"
}

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0

Where members[?ip=='{{ public_ip }}'].private_ip does the magic.