How to a save register variable to a file in Ansible
Ansible is an open-source automation tool that allows you to manage and configure IT infrastructure using a declarative approach. It simplifies the process of automating tasks such as software installations, configuration management, and application deployments across multiple servers or devices.
Saving a register variable
One of the key features of Ansible is its ability to execute tasks on remote hosts and collect data from those tasks. When Ansible runs a task, it can capture the output or result of that task and store it in a variable. This is known as registering a variable. Register variables can hold data like the output of a command, the content of a file, or any other information generated during the playbook execution.
A common use case in Ansible is to save the data stored in a registered variable to a file. Storing the output in a file is useful for documentation, reporting, or simply for future reference.
Using the copy module
The copy module in Ansible is used to copy files from the control node (the machine running Ansible) to remote hosts. You can use this module to save the content of the registered variable to a file on the remote host:
---
- hosts: localhost
connection: local
tasks:
- name: Get system uptime
ansible.builtin.shell: uptime
register: my_register_var
- name: Save system uptime to a file
copy:
content: "{{ my_register_var.stdout }}"
dest: "{{ lookup('env', 'PWD') }}/uptime.txt"
Code explanation
Line 1: The three dashes (
---) indicate the beginning of a YAML file. This Ansible playbook is starting, and it's written in YAML format.Line 2: The
hostssection specifies the target hosts for the playbook. In this case, it targets thelocalhost, which means the playbook will run on the local host.Line 3: The
connectionoption is set tolocal, which tells Ansible to execute tasks locally on the control node (the machine where Ansible is running) instead of using SSH to connect to remote hosts.Line 4: The
taskssection starts here, where you define a list of tasks to be executed.Line 5: This task is named
Get system uptime. Thenameparameter provides a description of what the task does for better readability in the output.Line 6: Ansible is using the
ansible.builtin.shellmodule to run a shell command on the local host. The command being run isuptime. Theuptimeis a common Unix/Linux command that displays the system's current uptime. It shows the time since the system was last booted and additional information, such as the current time, the number of logged-in users, and the system load average.Line 7: The
registerparameter is used to store the output of theuptimecommand in a variable namedmy_register_varfor later use.Line 9: This task is named
Save system uptime to a file.Line 10: The
copymodule is used to copy files from the control node to the target host. In this case, we are using it to save the system uptime to a file on the local host.Line 11: The
contentparameter specifies the content that should be saved to the file. We are using"{{ my_register_var.stdout }}"to access the output of theuptimecommand that we previously stored in themy_register_varvariable.Line 12: The
destparameter specifies the destination path where the file will be created."{{ lookup('env', 'PWD') }}/uptime.txt"will save the file in the current working directory of the control node (local host) with the nameuptime.txt.
Conclusion
The playbook above essentially executes the uptime command locally on the control node (localhost), captures its output, stores in register variable and then saves it to a file named uptime.txt in the current working directory of the control node.
Free Resources