how are you doing ?
So, I am trying to extract two informations from a json. it is a response from API with all information about a specific server in a cloud plataform. So, with curl, I can show the raw data:
curl --request GET --url "https://manage.runcloud.io/api/v2/servers/IDRANDOM/" -u apikey:apisecret --header 'accept: application/json' --header 'content-type: application/json'
we will get this response:
{"id":IDRANDOM,"name":"ServerName","provider":"OVH","ipAddress":"1.1.1.1","os":"Ubuntu","osVersion":"focal","connected":true,"online":true,"agentVersion":"2.5.8-1+ubuntu20.04+2","phpCLIVersion":"php74rc","softwareUpdate":true,"securityUpdate":true,"transferStatus":"AVAILABLE","created_at":"2018-06-26 19:24:53"}
From that, I want to get two informations:
"ipAddress":"1.1.1.1" "name":"ServerName"
These informations will update my ansible host file. So, it is important to get IP and Name.
So, using ansible, I've tried this code:
- name: "Set header for access token key"
set_fact:
batatas:
Accept: application/json
Content-Type: application/json
- name: Server info
uri:
url: "https://manage.runcloud.io/api/v2/servers/{{ item }}"
method: GET
user: "{{ apikey }}"
password: "{{ apisecret }}"
force_basic_auth: yes # necessary to force authentication
headers: "{{ batatas }}"
body_format: json
loop: "{{ id_servers }}"
register: page
- name: Debug Page
debug:
msg: "{{ page.results }}"
- name: Show Name and IP
debug:
msg: "{{ page.results | json_query(info) }}"
vars:
info: "page.results[*].{Name: name, IP: IpAddress}"
id_servers (Here, I replaced with IDRANDOM for security reasons) contains all the servers ID in runcloud API. So, I am using a loop to read the content and do a request to the runcloud API to get my information.
Running that, I am getting nothing:
Executing playbook get_info_server.yml
......
Debug Page...
127.0.0.1 ok: {
"changed": false,
"msg": [
+++++ I've cutted some information +++++++
{
"alt_svc": "h3=\":443\"; ma=86400, h3-29=\":443\"; ma=86400",
"ansible_loop_var": "item",
"cache_control": "no-cache, private",
"cf_cache_status": "DYNAMIC",
"cf_ray": "76e09a1cfdcd5d27-LIS",
"changed": false,
"connection": "close",
"content_type": "application/json",
"cookies": {},
"cookies_string": "",
"date": "Tue, 22 Nov 2022 09:22:41 GMT",
"elapsed": 1,
"failed": false,
"invocation": {
"module_args": {
"attributes": null,
"body": null,
"body_format": "json",
"ca_path": null,
"client_cert": null,
"client_key": null,
"creates": null,
"dest": null,
"follow_redirects": "safe",
"force": false,
"force_basic_auth": true,
"group": null,
"headers": {
"Accept": "application/json",
"Content-Type": "application/json"
},
"http_agent": "ansible-httpget",
"method": "GET",
"mode": null,
"owner": null,
"password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"remote_src": false,
"removes": null,
"return_content": false,
"selevel": null,
"serole": null,
"setype": null,
"seuser": null,
"src": null,
"status_code": [
200
],
"timeout": 30,
"unix_socket": null,
"unredirected_headers": [],
"unsafe_writes": false,
"url": "https://manage.runcloud.io/api/v2/servers/IDRANDOM",
"url_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"url_username": "apikey",
"use_gssapi": false,
"use_proxy": true,
"user": "apikey",
"validate_certs": true
}
},
"item": "IDRANDOM",
"json": {
"agentVersion": "2.5.8-1+ubuntu16.04+2",
"connected": true,
"created_at": "2018-04-19 16:05:05",
"id": 13430,
"ipAddress": "1.1.1.1",
"name": "Servername",
"online": true,
"os": "Ubuntu",
"osVersion": "xenial",
"phpCLIVersion": "php74rc",
"provider": "Vultr",
"securityUpdate": true,
"softwareUpdate": true,
"transferStatus": "AVAILABLE"
},
"msg": "OK (unknown bytes)",
"nel": "{\"success_fraction\":0,\"report_to\":\"cf-nel\",\"max_age\":604800}",
"redirected": false,
"report_to": "{\"endpoints\":[{\"url\":\"https:\\/\\/a.nel.cloudflare.com\\/report\\/v3?s=lfVjvut37PXPVmmCPdsiNjd72K0z5Nq%2BZgKeKOeKRcM9k5eiBnwpdkbYz3erDLG85Yj10S6lA0FqXIKT0U7gt1zzo4OOk5d9TFUWzWhK4OBqJ7Ppn9WmBLx3ilfBzzqijrEQqw%3D%3D\"}],\"group\":\"cf-nel\",\"max_age\":604800}",
"server": "cloudflare",
"status": 200,
"strict_transport_security": "max-age=31536000",
"transfer_encoding": "chunked",
"url": "https://manage.runcloud.io/api/v2/servers/IDRANDOM",
"vary": "Accept-Encoding",
"x_content_type_options": "nosniff",
"x_frame_options": "SAMEORIGIN",
"x_ratelimit_limit": "60",
"x_ratelimit_remaining": "58",
"x_xss_protection": "1; mode=block"
}
]
}
**Show Name and IP...
127.0.0.1 ok: {
"changed": false,
"msg": ""**
}
- Play recap -
127.0.0.1 : ok=5 changed=0 unreachable=0 failed=0 rescued=0 ignored=0
So, What Am I doing wrong ?
I've tried to follow two articles about Json_query and JMESPath, however, maybe I am using the wrong syntax.
- name: Show Name and IP
debug:
msg: "{{ page.results | json_query(info) }}"
vars:
info: "page.results[*].{Name: name, IP: IpAddress}"
URL: https://www.middlewareinventory.com/blog/ansible_json_query/ https://docs.ansible.com/ansible/5/collections/community/general/docsite/filter_guide_selecting_json_data.html
Thanks!