This document provides a high level overview of using ansible to communication with the MobiledgeX API.
Note: This is a very rough example that illustrates logging in, generating a JWT, and using that token to access the API. Additional work could be done to add logic to parse the JSON returned from the calls and to take additional steps based on that data.
Since these commands are designed to run against an API and not a host we will
just run the commands against localhost
. Note that the default all
host grouping
does not include localhost
so you will need to directly specify your local host
in the YAML file.
Alternatively, you can setup a host file and use that as part of the workflow, just be aware that if you have multiple hosts that match the playbook these steps will be run once for each host.
The playbook is rather simple and begins with us setting the hostname and turning off the automatic fact gathering:
- hosts: localhost
gather_facts: false
Next we break down our tasks; the first task is to log into the API and retrieve
our JWT. To do this, we use the uri
module. In order to be able to parse the
JWT, we register the output as thereturn
.
- name: Log into the server
uri:
url: https://console.mobiledgex.net/api/v1/login
user: demo
password: helloworld
method: POST
body: '{"username": "someuser", "password":"somepass"}'
body_format: json
register: thereturn
Our next step is to pull the token from the returned JSON. We do this by invoking
set_fact
and using the json_query
filter to parse out the token. Following this
step we can use thetoken
in our subsequent calls.
- name: Pull the token
set_fact:
thetoken: ""
Now we can use the JWT to authenticate against the api and check our running instances. Once again, we will take the output and register it to a variable so that we can utilize it later in our processing.
- name: Check running Instances
uri:
url: https://console.mobiledgex.net/api/v1/auth/ctrl/ShowAppInst
method: POST
body: '{"region": "EU"}'
body_format: json
return_content: yes
headers:
Authorization: "Bearer "
register: theresult
Finally, we print out the content of the results from the earlier call using
the debug
module.
- name: Print Out Our Results
debug:
var: theresult.content
The full YAML file is provided here for completeness.
- hosts: localhost
remote_user: root
gather_facts: false
tasks:
- name: Log into the server
uri:
url: https://console.mobiledgex.net/api/v1/login
user: demo
password: helloworld
method: POST
body: '{"username": "someuser", "password":"somepass"}'
body_format: json
register: thereturn
- name: Pull the token
set_fact:
thetoken: ""
- name: Check running Instances
uri:
url: https://console.mobiledgex.net/api/v1/auth/ctrl/ShowAppInst
method: POST
body: '{"region": "EU"}'
body_format: json
return_content: yes
headers:
Authorization: "Bearer "
register: theresult
- name: Print Out Our Results
debug:
var: theresult.content
$ ansible-playbook ./ansible-test.yaml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [localhost] ***********************************************************************************************************************************************************************************************
TASK [Log into the server] *************************************************************************************************************************************************************************************
ok: [localhost]
TASK [Pull the token] ******************************************************************************************************************************************************************************************
ok: [localhost]
TASK [Check running Instances] *********************************************************************************************************************************************************************************
ok: [localhost]
TASK [Print Out Our Results] ***********************************************************************************************************************************************************************************
ok: [localhost] => {
"theresult.content": "{\"data\":{\"key\":{\"app_key\":{\"developer_key\":{\"name\":\"demoorg\"},\"name\":\"mobiledgexsdkdemo20\",\"version\":\"2020-3-26\"},\"cluster_inst_key\":{\"cluster_key\":{\"name\":\"sdkdemocluster\"},\"cloudlet_key\":{\"operator_key\":{\"name\":\"Orange-Spain\"},\"name\":\"madrid-coslada\"},\"developer\":\"demoorg\"}},\"cloudlet_loc\":{\"latitude\":40.4168,\"longitude\":-3.7038},\"uri\":\"sdkdemocluster.madrid-coslada.orange-spain.mobiledgex.net\",\"liveness\":1,\"mapped_ports\":[{\"proto\":1,\"internal_port\":8008,\"public_port\":8008,\"fqdn_prefix\":\"mobiledgexsdkdemo20-tcp.\"},{\"proto\":1,\"internal_port\":8011,\"public_port\":8011,\"fqdn_prefix\":\"mobiledgexsdkdemo20-tcp.\"}],\"flavor\":{\"name\":\"x1.medium\"},\"state\":5,\"runtime_info\":{\"container_ids\":[\"mobiledgexsdkdemo20-deployment-759fd4cf79-qjd6k\"]},\"created_at\":{\"seconds\":1586474845,\"nanos\":683125749},\"status\":{}}}\n{\"data\":{\"key\":{\"app_key\":{\"developer_key\":{\"name\":\"demoorg\"},\"name\":\"vmtwo\",\"version\":\"1.0\"},\"cluster_inst_key\":{\"cluster_key\":{\"name\":\"DefaultVMCluster\"},\"cloudlet_key\":{\"operator_key\":{\"name\":\"TDG\"},\"name\":\"munich-main\"},\"developer\":\"demoorg\"}},\"cloudlet_loc\":{\"latitude\":48.1351,\"longitude\":11.582},\"uri\":\"demoorgvmtwo10.munich-main.tdg.mobiledgex.net\",\"liveness\":1,\"mapped_ports\":[{\"proto\":1,\"internal_port\":22,\"public_port\":22}],\"flavor\":{\"name\":\"m4.small\"},\"state\":5,\"runtime_info\":{},\"created_at\":{\"seconds\":1586902891,\"nanos\":716712765},\"status\":{},\"power_state\":3}}\n"
}
PLAY RECAP *****************************************************************************************************************************************************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0