What Does Register File Do?
In Ansible, y'all can run whatever shell control on your Ansible hosts, the hosts you will exist configuring with Ansible. These trounce commands may accept outputs. Past default, the output is ignored. If you want to store the output in a variable and employ information technology later, then you can use the Ansible register module. This article will bear witness you how to use the Ansible register module to store the control output in a variable and access it later in your Ansible playbook.
Prerequisites
If y'all want to try out the examples in this article, you must:
1) Have Ansible installed on your estimator.
2) Have an Ubuntu host configured for Ansible automation.
There are many articles on LinuxHint dedicated to installing Ansible and configuring hosts for Ansible automation. You may check these manufactures out if necessary.
Setting Upwards a Project Directory
Before moving on whatsoever further, set up a new Ansible project directory, merely to keep things a bit organized.
To create the project directory register-demo/ and all the required subdirectories (in your electric current working directory), run the post-obit command:
$ mkdir -pv annals-demo/playbooks
Once the project directory is created, navigate to the project directory, every bit follows:
Create a hosts inventory file, equally follows:
Add the host IP or DNS name of your Ubuntu host in the inventory file (i host per line), as shown in the screenshot below.
Here, I have added my Ubuntu 20.04 LTS host vm3.nodekite.com in the ubuntu20 group.
Once you lot are done, save the file past pressing <Ctrl> + X, followed past Y and <Enter>.
Create an Ansible configuration file ansible.cfg in your project directory, as follows:
Next, blazon the post-obit lines in the ansible.cfg file:
[defaults]
inventory = hosts
host_key_checking = Imitation
Once you are done, save the ansible.cfg file by pressing <Ctrl> + X, followed by Y and <Enter>.
Now, effort to ping your Ubuntu host, as follows:
$ ansible ubuntu20 -u ansible -m ping
Equally you can meet, my Ubuntu 20.04 host vm3.nodekite.com is attainable.
Example 1: The Basics
In this example, I will show you some of the basics of the Ansible register module. I will utilize Ansible to generate a random countersign in my Ubuntu 20.04 host using the pwgen command, shop the password in a variable using the annals module, and impress the countersign on the screen.
Starting time, create the new playbook generate_pass.yaml in the playbooks/ directory, as follows:
$ nano playbooks/generate_pass.yaml
Type the post-obit lines in the generate_pass.yaml file:
- hosts : ubuntu20
user : ansible
get : True
tasks:
- name : Ensure pwgen is installed
apt:
name : pwgen
state : nowadays
update_cache : True
- proper noun : Generate password
shell : pwgen -N i -s thirty
annals : mypass
- name : Print the generated password
debug:
msg : "The countersign is {{ mypass }}"
Once you are done, press <Ctrl> + 10, followed by Y and <Enter>, to save the generate_pass.yaml file.
The following line tells Ansible to run the playbook generate_pass.yaml on every host in the ubuntu20 group. In my case, the playbook will be run on the host vm3.nodekite.com.
In this playbook, I will define three tasks.
The first task will enure that the pwgen package is installed.
The second task will generate a random, 30-character password using the pwgen command. I will use the register module to store the generated countersign in the mypass variable.
The tertiary task volition impress the mypass variable using the Ansible debug module.
Run the playbook generate_pass.yaml using the following command:
$ ansible-playbook playbooks/generate_pass.yaml
As you can see, the playbook ran successfully. A countersign has also been generated.
But, why did the variable mypass print and so many items?
Well, the variable mypass is an object that contains some important properties.
The near of import properties of each of the annals variables are as follows:
cmd – The command that ran to generate the output.
stdout – The output of the command.
stderr – The error output of the control.
start – The appointment and time when the control began executing.
end – The appointment and fourth dimension when the command finished executing.
delta – The fourth dimension taken to run the command. This is the divergence between the stop and the start properties.
stdout_lines – An array containing each output line of the command. Same as stdout, but stdout separates the lines using a newline (\n) characters instead of arrays.
stderr_lines – An assortment containing each fault output line of the command. Aforementioned every bit stderr, but stderr separates the lines using newlines (\n) characters instead of arrays.
If you just want to print/access the password cord (which is very likely), you may print/access the stdout belongings of the mypass variable in your playbook, as marked in the screenshot below.
$ nano playbooks/generate_pass.yaml
In one case you are done, run the playbook generate_pass.yaml again. Only the countersign string will be printed, as you tin see in the screenshot beneath.
That covers the basics of the Ansible annals module.
Case ii: Shop Directory Contents
In this instance, I will show you how to store the contents of a directory in a variable using the Ansible register module, as well equally how to iterate over them.
First, create the new playbook get_dir_contents.yaml in the playbooks/ directory.
$ nano playbooks/get_dir_contents.yaml
Side by side, type the post-obit lines in the get_dir_contents.yaml playbook:
- hosts : ubuntu20
user : ansible
become : True
tasks:
- name : List all files and directories in /domicile/ansible
trounce : ls /home/ansible
annals : dir_contents
- name : Print directory contents using loops
debug:
msg : "{{ particular }}"
loop : "{{ dir_contents.stdout_lines }}"
Once y'all are done, press <Ctrl> + X, followed by Y and <Enter>, to save the generate_pass.yaml file.
In this playbook, I will define two tasks.
The starting time task lists all the contents of the /home/ansible directory and stores them in the dir_contents variable.
The 2d task prints the dir_contents variable.
Run the get_dir_contents.yaml playbook, equally follows.
$ ansible-playbook playbooks/get_dir_contents.yaml
As you can see, the stdout_lines property stored the directory contents as an array. The stdout belongings is also stored in the directory contents. These properties are separated past newline (\n) characters. In this example, the stdout_lines belongings is easy to work with.
Next, iterate over the directory contents using a loop.
To exercise this, open the get_dir_contents.yaml playbook and change the second job, every bit marked in the screenshot below.
$ nano playbooks/get_dir_contents.yaml
Here, I am iterating over the dir_contents.stdout_lines assortment using a loop and printing the array items using the Ansible debug module. In this chore, the item variable is a loop variable used to iterate over the array elements.
Run the get_dir_contents.yaml playbook, as follows:
$ ansible-playbook playbooks/get_dir_contents.yaml
As you can see, the contents of the /home/ansible directory are printed on the screen.
Example 3: Back up Directory
In this example, I will testify yous how to back upward a directory using the Ansible annals, file, and copy modules.
Starting time, create the new playbook backup_home_dir.yaml in the playbooks/ directory, as follows:
$ nano playbooks/backup_home_dir.yaml
Next, type the following lines in the backup_home_dir.yaml file.
- hosts : ubuntu20
user : ansible
become : True
tasks:
- name : Get abode directory /home/ansible contents
crush : ls /dwelling house/ansible
register : dir_contents
- proper name : Create a new directory /tmp/ansible
file:
path : /tmp/ansible
land : directory
- proper name : Backup domicile directory /domicile/ansible to /tmp/ansible
copy:
src : /abode/ansible/{ { particular } }
dest : /tmp/ansible/
remote_src : True
loop : "{{ dir_contents.stdout_lines }}
Once you lot are washed, press <Ctrl> + X, followed by Y and <Enter>, to salvage the backup_home_dir.yaml file.
In this playbook, I will define 3 tasks.
The first job stores the contents of the /home/ansible directory (the directory I will be bankroll up) in the dir_contents variable using the Ansible register module.
The second task creates a new directory /tmp/ansible using the Ansible file module. This is the directory where the backup volition be stored.
The tertiary task loops through the dir_contents.stdout_lines array and uses the Ansible copy module to copy each directory to the /tmp/ansible/ directory.
Run the backup_home_dir.yaml playbook, as follows:
$ ansible-playbook playbooks/backup_home_dir.yaml
As you can see, on my Ubuntu twenty.04 LTS host, the fill-in was successful.
Example four: Run or Skip Tasks
In this example, I will show you how to run or skip tasks, depending on the variable you have registered, using the annals module.
Beginning, create the new playbook register_conditions.yaml in the playbooks/ directory as follows:
$ nano playbooks/register_conditions.yaml
Next, blazon the following lines in the register_conditions.yaml file.
- hosts : ubuntu20
user : ansible
become : Truthful
tasks:
- name : List directory contents
trounce : ls /home/ansible/test3
register : dir_contents
- name : Check if directory is empty
debug:
msg : "Directory is empty."
when : dir_contents.stdout == ""
In one case you lot are done, press <Ctrl> + Ten, followed past Y and <Enter>, to save the register_conditions.yaml file.
In this playbook, I have divers 2 tasks.
The start job stores the contents of the /home/ansible/test3 directory in the dir_contents variable.
The second task checks if dir_contents.stdout is an empty string, or whether the directory /abode/ansible/test3 is empty. If the directory is empty, the message Directory is empty will print.
Run the register_conditions.yaml playbook, equally follows:
$ ansible-playbook playbooks/register_conditions.yaml
As you can see, the playbook ran successfully.
Since the directory /home/ansible/test3 is empty, the playbook printed the message Directory is empty.
Side by side, create a new file in the /home/ansible/test3 directory.
Since the /domicile/ansible/test3 directory is no longer empty, the job Check if directory is empty is skipped, every bit you tin can see in the screenshot below.
$ ansible-playbook playbooks/register_conditions.yaml
Conclusion
The Ansible register module is very useful for server automation. This article showed you the basics of the register module, including examples of using the Ansible register module for directory storage and backup, and for running directory tasks.
What Does Register File Do?,
Source: https://linuxhint.com/ansible_register_module/
Posted by: kingagge1986.blogspot.com
0 Response to "What Does Register File Do?"
Post a Comment