
Ansible registers module used to capture / store the output of a task/command.it is similar to variable declarations we can define the value in different scenarios like a conditional, logging etc.
for example we echo uptime and hostname, Following Syntax :
register: uptime
Here uptime is value of register variable, we can define any suitable value. At the last we use debug module with variable name.
To create a playbook with suitable name, we create a register.yml file under ansible configuration directory
cd /etc/ansiblevi register.yml
---
- hosts: all
tasks:
- name: System Uptime
command: uptime
register: uptime
- debug: var=uptime
-name: System Uptime is a task name,
command: uptime , command is module name and uptime is linux system command
register: uptime , uptime is register variable value, we can use any suitable value
To run playbook using ansible-playbook command, for help of ansible-playbook you can use -h option
ansible-playbook -h
To run ansible playbook
ansible-playbook registe.yml
To run ansible playbook on single hosts of inventory using -l option,
172.17.20.98 is hosts IP of our Ansible Inventery
ansible-playbook -l 172.17.20.98 register.yml
We can add stderr, stderr_lines, stdout, stdout_lines for filter
We will use stdout for filter the result, something like this example
Video Tutorial :
---
- hosts: all
tasks:
- name: System Uptime
command: uptime
register: uptime
- debug: var=uptime.stdout
Multiple register variable :
---
- hosts: 127.0.0.1
tasks:
- name: System Uptime
command: uptime
register: uptime
- name: System Hostname
command: hostname
register: host
- debug: var={{ item }}
loop:
- uptime.stdout
- host.stdout
ansible-playbook registe.yml
Thanks
End of this ansible register variables tutorial, we need your support so i request you to please comment if something missed by me, and share and like this post aslo
www.linuxtopic.com
Very good..
ReplyDeleteNice!
ReplyDeletecan you plz tell me how we can use a variable globally so that i can put some condition among the host std_out
ReplyDelete