Ansible Register Variable-Ansible Register Module Examples

Ansible Register Module With Examples

Ansible register variable or ansible register module is used to capture or store the output of the command or task. By using the register module, you can store that output into any variable.

In the following example, I will show you how to find the status of httpd. I will start the httpd and I will see the status and I will store the status into a variable called httpd_status by using ansible register module. With below playbook, you can check whether httpd started with errors or without errors. The use of register module is you can verify your task is executed or not by checking task output in the log.

ansible register module with an example:

[root@localhost ~]# cat register.yml 
---
- hosts: localhost
  gather_facts: no
  tasks:
  - name: starting httpd
    service: name=httpd state=started enabled=yes

  - name: httpd status
    command: service httpd status
    register: httpd_status  
  
  - name: httpd status output
    debug:
      var: httpd_status

Here httpd_status is the variable so the output of the command "service httpd status" will be stored into variable httpd_status. So by using the register module, you can store the output of the task. To print that output we use debug module.

OUTPUT LOG:

[root@localhost ~]# ansible-playbook register.yml 

PLAY [localhost] ***********************************************************************************************

TASK [starting httpd] ******************************************************************************************
ok: [localhost]

TASK [httpd status] ********************************************************************************************
 [WARNING]: Consider using the service module rather than running service.  If you need to use command because
service is insufficient you can add warn=False to this command task or set command_warnings=False in
ansible.cfg to get rid of this message.

changed: [localhost]

TASK [httpd status output] *************************************************************************************
ok: [localhost] => {
    "httpd_status": {
        "changed": true, 
        "cmd": [
            "service", 
            "httpd", 
            "status"
        ], 
        "delta": "0:00:00.031653", 
        "end": "2018-12-15 05:11:50.199260", 
        "failed": false, 
        "rc": 0, 
        "start": "2018-12-15 05:11:50.167607", 
        "stderr": "Redirecting to /bin/systemctl status httpd.service", 
        "stderr_lines": [
            "Redirecting to /bin/systemctl status httpd.service"
        ], 
        "stdout": "● httpd.service - The Apache HTTP Server\n   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)\n   Active: active (running) since Sat 2018-12-15 05:11:08 EST; 41s ago\n     Docs: man:httpd(8)\n           man:apachectl(8)\n  Process: 5960 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)\n Main PID: 6018 (httpd)\n   Status: \"Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec\"\n    Tasks: 6\n   CGroup: /system.slice/httpd.service\n           ├─6018 /usr/sbin/httpd -DFOREGROUND\n           ├─6019 /usr/sbin/httpd -DFOREGROUND\n           ├─6020 /usr/sbin/httpd -DFOREGROUND\n           ├─6021 /usr/sbin/httpd -DFOREGROUND\n           ├─6022 /usr/sbin/httpd -DFOREGROUND\n           └─6023 /usr/sbin/httpd -DFOREGROUND\n\nDec 15 05:11:08 localhost.localdomain systemd[1]: Starting The Apache HTTP Server...\nDec 15 05:11:08 localhost.localdomain httpd[6018]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message\nDec 15 05:11:08 localhost.localdomain systemd[1]: Started The Apache HTTP Server.", 
        "stdout_lines": [
            "● httpd.service - The Apache HTTP Server", 
            "   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)", 
            "   Active: active (running) since Sat 2018-12-15 05:11:08 EST; 41s ago", 
            "     Docs: man:httpd(8)", 
            "           man:apachectl(8)", 
            "  Process: 5960 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)", 
            " Main PID: 6018 (httpd)", 
            "   Status: \"Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec\"", 
            "    Tasks: 6", 
            "   CGroup: /system.slice/httpd.service", 
            "           ├─6018 /usr/sbin/httpd -DFOREGROUND", 
            "           ├─6019 /usr/sbin/httpd -DFOREGROUND", 
            "           ├─6020 /usr/sbin/httpd -DFOREGROUND", 
            "           ├─6021 /usr/sbin/httpd -DFOREGROUND", 
            "           ├─6022 /usr/sbin/httpd -DFOREGROUND", 
            "           └─6023 /usr/sbin/httpd -DFOREGROUND", 
            "", 
            "Dec 15 05:11:08 localhost.localdomain systemd[1]: Starting The Apache HTTP Server...", 
            "Dec 15 05:11:08 localhost.localdomain httpd[6018]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message", 
            "Dec 15 05:11:08 localhost.localdomain systemd[1]: Started The Apache HTTP Server."
        ], 
        "warnings": [
            "Consider using the service module rather than running service.  If you need to use command because service is insufficient you can add warn=False to this command task or set command_warnings=False in ansible.cfg to get rid of this message."
        ]
    }
}

PLAY RECAP *****************************************************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=0   

Whenever you executed any task or commands ansible will give you some return values like.

  • changed
  • cmd
  • delta
  • end
  • failed
  • rc
  • start
  • stderr
  • stderr_lines
  • stdout
  • stdout_lines

These return values depend on the task you executed if you want to know more about ansible return value you can check here.

  • ansible return values or ansible return codes

In the above return values our output will be stored in stdout, So to print exact output without any extra return values we can use like variablename.stdout or you can use stdout_lines. Stdout_lines will split the output into line by line.

playbook with stdout_lines:

[root@localhost ~]# cat register.yml 
---
- hosts: localhost
  gather_facts: no
  tasks:
  - name: starting httpd
    service: name=httpd state=started enabled=yes

  - name: httpd status
    command: service httpd status
    register: httpd_status  
  
  - name: httpd status output with stdout
    debug:
      var: httpd_status.stdout_lines

output with stdout_lines:

[root@localhost ~]# ansible-playbook register.yml

PLAY [localhost] ***********************************************************************************************

TASK [starting httpd] ******************************************************************************************
changed: [localhost]

TASK [httpd status] ********************************************************************************************
[WARNING]: Consider using the service module rather than running service. If you need to use command because
service is insufficient you can add warn=False to this command task or set command_warnings=False in
ansible.cfg to get rid of this message.

changed: [localhost]

TASK [httpd status output] *************************************************************************************
ok: [localhost] => {
"httpd_status.stdout_lines": [
"● httpd.service - The Apache HTTP Server", 
" Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)", 
" Active: active (running) since Sat 2018-12-15 04:45:06 EST; 418ms ago", 
" Docs: man:httpd(8)", 
" man:apachectl(8)", 
" Process: 5503 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)", 
" Main PID: 5568 (httpd)", 
" Status: \"Processing requests...\"", 
" Tasks: 6", 
" CGroup: /system.slice/httpd.service", 
" ├─5568 /usr/sbin/httpd -DFOREGROUND", 
" ├─5569 /usr/sbin/httpd -DFOREGROUND", 
" ├─5570 /usr/sbin/httpd -DFOREGROUND", 
" ├─5571 /usr/sbin/httpd -DFOREGROUND", 
" ├─5572 /usr/sbin/httpd -DFOREGROUND", 
" └─5573 /usr/sbin/httpd -DFOREGROUND", 
"", 
"Dec 15 04:45:06 localhost.localdomain systemd[1]: Starting The Apache HTTP Server...", 
"Dec 15 04:45:06 localhost.localdomain httpd[5568]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message", 
"Dec 15 04:45:06 localhost.localdomain systemd[1]: Started The Apache HTTP Server."
]
}

PLAY RECAP *****************************************************************************************************
localhost : ok=3 changed=2 unreachable=0 failed=0

Here you can see we got only stdout_lines return code output, Depends on our requirement we can mention any return value. But if you want the only output of the command we will mention variblename.stdout  or variblename.stdout_lines.

 

  • ansible register variable
  • ansible register example
  • register varible ansible
  • ansible register module with example
  • register variable ansible
  • ansible register variable example

 

2 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *