Ansible Register

Ansible Register
Techiio-author
Written by Nilima PaulFebruary 1, 2022
11 min read
Ansible
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
Techiio-author
Nilima Paul

Technology Security Analyst

We will know in this article, what's the Ansible Register.

Introduction to Ansible Register

Ansible register is a method for catching the result from task execution and storing it in a variable. This is a significant component, as this result is different for every remote host, and the premise on that we can utilize conditions circles to do a few different errands. Likewise, each register esteem is substantial all through the playbook execution. In this way, we can utilize set_fact to control the information and give a contribution to different errands likewise. In this article, we will learn and see instances of involving register in various situations. We will likewise perceive how just a snippet of data in the enrolled variable can be utilized.

Ansible Register

For any undertaking yield, which we register in a variable, it is put away in a pre-characterized design in JSON. For various assignments, we will get different results and it is put away in the manner it is characterized in ansible documentation. Whenever we see the result we will consider the qualities to be per ansible documentation and a few fields which will be displayed in each result like changed.

The following is a result of an undertaking where we attempted to duplicate a record from the control hub to the distant hub. We enrolled it and showed the enlisted variable like underneath:

blogpost

How does Ansible Register work?

These are the factors wherein the result of your undertaking will be put away on the Ansible Control Server. In straightforward words, when you need to run an order on a far off PC and afterward store the result in a variable and utilize a snippet of data from the result, later in your plays. This sort of utilization is conceivable by enlisted factors. It is some way or another like the framework Facts which are found and brought by arrangement module. Here anything order you run its result will be saved in JSON configuration and afterward you utilize that data equivalent to you utilized realities.

For another reference for a playbook with content like below:

  • hosts: host-one tasks: name: check HTTP status command: service httpd status register: output_var
  • name: display the output of the above execution debug: var: output_var

In which we are checking the status of httpd service on a remote server named host-one. You will see that; in the below output we got every small detail about the command executed on the remote host. Similarly when we run any other module and register its output in a variable. We will notice all details about the task execution and related information will be seen in JSON fields.

you will see most of the fields in output, we try to explore some of those.

changed – this will be true or false based on the state of remote hosts if the state changed then it will contain true, else it will contain

cmd – This is a command which ran on the remote host

delta – the time taken to execute the command

end – end date and time when the task completed

failed – if a task failed or not, it has true or false values

RC – return code

start – start date and time of a task

stderr – the standard error message in a single line

stderr_lines – the standard error message in separate lines

stdout – the output in a single line

stdout_lines – the output in separate lines

blogpost

Examples to Implement Ansible Register

Presently by utilizing models, we will attempt to find out with regards to the ansible register, which you may need to use in everyday tasks. We will take a few models, yet prior to going there, we initially comprehend our lab utilized for the purpose of testing.

Here we have an Ansible control server named ansible-regulator and two controllers has named have one and host-two. We will make playbooks and run Ansible orders on the ansible-regulator hub and deal with the data from remote hosts for executed undertakings

Example #1

In the below example, we try to find the .txt files on remote nodes and capture the output in a variable, then will display that variable find_output. For this first see the contents of the playbook like

Code:

- hosts: all tasks:
name: find all txt files in /tmp shell: "find /tmp -name *.txt" register: find_output
debug:
var: find_output

After executing it, we will see below output, in this output under stdout_lines we will see the name of files that are found.

Output:

ansible-playbook register_variable_task_find.yaml

Example #2

If you want to see the files only, then update the playbook and make the debug task like below:

Code:

- debug:
var: find_output.stdout_lines

Output:

ansible-playbook register_variable_task_find.yaml

Example #3

In this example, we are checking the free memory and uptime on the remote host. We create a playbook like below. Here we used stdout_lines to display only the output of

Code:

---
hosts: host-two tasks:
name: To check the uptime of remote host command: uptime
register: uptime_var
name: To check free memory on remote host shell: free -m
register: free_var
debug:
var: "{{ item }}" loop:
stdout_lines
stdout_lines

Output:

ansible-playbook register_multiple_variable.yaml

Example #4

In the beneath model, we utilize a condition by when and check whether a document exists or nor on remote hosts, on the off chance that the record doesn't exist, then, at that point, make it, else skip creation. We really look at the record's presence, basically by ls, and check the bring code back. In Linux, is any order is effective, then, at that point, return code is zero, else it is non-zero. We make an example playbook like underneath:

Code:

---
hosts: all tasks:
name: check whether /tmp/test1.txt exists or not command: ls /tmp/test1.txt
register: check_rc_var ignore_errors: yes
debug:
var: check_rc_var.rc
name: create file /tmp/test1.txt when it doesn't exists already file:
path: /tmp/test1.txt state: touch
when: check_rc_var.rc != 0

While sending this playbook, we will consider the result to be beneath. Here you can see, on the remote host where the document doesn't exist, playbook showed a blunder message and return code will be non-zero and on the remote host where the record exists, it showed task state as different as ls order ran effectively, additionally for this situation return code will be zero. Along these lines, on this premise, the document was made on have two and avoided on have one.

Output:

ansible-playbook register_condition.yaml

Conclusion

In Ansible, utilizing register is extremely useful particularly in the creation climate. The plausibility to catch yield contrastingly for various distant hubs is extremely helpful. Likewise, as the result is put away in JSON, perusing and parsing it simple for different instruments as info. So learn it and use it carefully.

Ansible
Ansible Register
Linux
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Nilima Paul
Technology Security Analyst
Techiio-followerTechiio-followerTechiio-follower
201 Blog Posts
0 Discussion Threads
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance