Mode « registre » dans operationBook
The [‘register’] mode (/en/operationbook#register), in “operationBook”, allows you to store the standard output of an operation in an environment variable and communicate this to subsequent operations (execution environment administered node).
Prerequisites
Follow the global prerequisite.
Basic case with data in JSON format
I’d like to perform two separate operations, displaying the value of one of the attributes of a data structure in “JSON” format, obtained from some REST API service. For this stage, I’ve created a link which returns this content : {"x1": "1", "x2": "2"}
, and simulates a REST API call.
The complexity induced in the following example is deliberate. This helps us to better understand the challenge of automation.
1st option
operation
As the operations are performed separately, the first idea would be to create a single operation, with a variable that would vary the name of the JSON attribute:
-
operation “getAndDisplayJsonAttr”
-
The script “run.sh” :
#!/usr/bin/env bash# Calling API REST service to get datadata=$(curl -s "https://automation-doc.mytinydc.com/get/jsonformation.json")valueAttr=$(echo "${data}" | jq -r ".${FIELDNAME}")echo "Value of attribut ${FIELDNAME}: ${valueAttr}" -
The manifest :
comment: "get data from API REST and display attribut value"scripts:- "run.sh"dependencies:- "jq"- "curl"parameters:required:FIELDNAME:type: stringcomment: "name of attribute to display"
-
Run :
automation-cli run -op "getAndDisplayJsonAttr" -h "localhost" -e FIELDNAME="x1"
The operation displays the value of the “x1” attribute of the data structure obtained by calling the REST API.
operationBook
By creating an “operationBook” (“displayJson2Calls”), successively calling the same operation with a different environment, the job is done:
operations: - operation: "getAndDisplayJsonAttr" environment: FIELDNAME: "x1" - operation: "getAndDisplayJsonAttr" environment: FIELDNAME: "x2"
Run :
automation-cli run -ob "displayJson2Calls" -h "localhost"
Disadvantage
With this first option, each operation calls the API Rest service, implying two network calls for an identical response.
2nd option
To save a call, let’s create a new operation that will be responsible for calling the API REST service.
-
Operation “callApiRest” :
-
The script “run.sh” :
#!/usr/bin/env bashcurl -s "https://automation-doc.mytinydc.com/get/jsonformation.json" -
The manifest :
comment: "call API REST"scripts:- "run.sh"dependencies:- "curl"
-
-
The operation “displayJsonAttr” :
-
The script “run.sh” :
The “register” process has captured the standard output (stdout) of the operation. In our case, this is a JSON structure.
#!/usr/bin/env bash# JSON is required and comes from register (operationBook)# Consequently, the operation launched alone will request a value for JSON# The attribute to be read will be '.x?'valueAttr=$(echo "${JSON}" | jq -r ".${FIELDNAME}")echo "Value of attribut ${FIELDNAME}: ${valueAttr}" -
The manifest :
comment: "display attribut value"scripts:- "run.sh"dependencies:- "jq"parameters:required:JSON:type: stringcomment: "JSON format dataset"FIELDNAME:type: stringcomment: "Name of attribute to display" -
operationBook : “displayJson1Calls”:
operations:- operation: "callApiRest"register: "JSON"- operation: "displayJsonAttr"environment:FIELDNAME: "x1"- operation: "displayJsonAttr"environment:FIELDNAME: "x2"
-
By performing this operation, the work is also done, and you save a network call:
automation-cli run -ob "displayJson1Calls" -h "localhost"
Basic case with a string
I’d like to perform an operation that displays the value of any identifier (e.g. an EC2 AWS instance id) and use this value in the next operation.
-
File “registerString.yaml”
operations:- command: "echo '-i951133254xx'"register: EC2INSTANCEID- command: "echo ${EC2INSTANCEID}"Fenêtre de terminal automation-cli run -ob "./registerString.yaml" -h "localhost"