Ansible is a simple configuration management tool. Open source community keeps trying to make the code much simpler on the newer version. Prior to Ansible engine 2.7, To reboot the target hosts, we need to define a block of code to reboot the server and wait until the hosts to come back. Most of the time. making the configuration changes or installing the OS patches which require a reboot. Post reboot, we might need to get few commands output to validate those changes. This article will walk through how Ansible 2.7 engine codes reduce the block of code.
Reboot the node/server and wait to come back : (Prior to 2.7)
Here is the block of code that we use to reboot the target hosts and perform post checks. (Highlighted the reboot block)
--- - hosts: all become: yes tasks: - name: Check the uptime prior reboot shell: uptime register: UPTIME_PRE_REBOOT - debug: msg={{UPTIME_PRE_REBOOT.stdout}} - name: Reboot node and stop polling. shell: reboot async: 10 # Do not care for 10 sec poll: 0 # Fire & Forget - name: wait for host to finish reb00t wait_for: port: "{{ (ansible_port|default(ansible_ ssh_port))|default(22) }}" host: '{{ (ansible_ssh_host|default( ansible_host))|default( inventory_hostname) }}' search_regex: OpenSSH delay: 10 # Do not check for at least 10 sec connection: local - name: Check the uptime post reboot shell: uptime register: UPTIME_POST_REBOOT - debug: msg={{UPTIME_POST_REBOOT. stdout}}
Run the playbook and Check Results: (Prior to 2.7)
[root@ansible-server ~]# ansible-playbook -i hosts_lists reboot_wait_to_come_back_2.6.yml -k SSH password: PLAY [all] ****************************** ****************************** ******************* TASK [Gathering Facts] ****************************** ****************************** ******** ok: [192.168.3.20] TASK [Check the uptime prior reboot] ****************************** ****************************** ****************************** ** changed: [192.168.3.20] TASK [debug] ****************************** ****************************** ******************* ok: [192.168.3.20] => { "msg": " 01:41:53 up 7 min, 2 users, load average: 0.00, 0.04, 0.05" } TASK [Reboot node and stop polling.] ****************************** ****************************** ****************************** ** changed: [192.168.3.20] TASK [wait for host to finish reb00t] ****************************** ****************************** ****************************** * ok: [192.168.3.20] TASK [Check the uptime post reboot] ****************************** ****************************** ****************************** * changed: [192.168.3.20] TASK [debug] ****************************** ****************************** ****************** ok: [192.168.3.20] => { "msg": " 01:42:33 up 0 min, 1 user, load average: 0.62, 0.14, 0.05" } PLAY RECAP ****************************** ****************************** ******************* 192.168.3.20 : ok=7 changed=3 unreachable=0 failed=0 [root@ansible-server ~]#
Reboot block in Ansible 2.7 :
In Ansible 2.7, reboot block of code looks very simple. Please see the below code to reboot the server and wait to come back.
--- - hosts: all become: yes tasks: - name: Check the uptime shell: uptime register: UPTIME_PRE_REBOOT - debug: msg={{UPTIME_PRE_REBOOT.stdout}} - name: Unconditionally reboot the machine with all defaults reboot: - name: Check the uptime after reboot shell: uptime register: UPTIME_POST_REBOOT - debug: msg={{UPTIME_POST_REBOOT. stdout}}
Let’s test the playbook.
[root@ansible-server ~]# ansible-playbook -i hosts_lists reboot_wait_to_come_back.yml -k SSH password: PLAY [all] ************************************************************ ************************ TASK [Gathering Facts] ****************************** ****************************** ****************************** ***** ok: [192.168.3.20] TASK [Check the uptime] ****************************** ****************************** ****************************** ******* changed: [192.168.3.20] TASK [debug] ****************************** ****************************** ************************ ok: [192.168.3.20] => { "msg": " 01:15:38 up 12 min, 2 users, load average: 0.16, 0.06, 0.06" } TASK [Unconditionally reboot the machine with all defaults] ****************************** ****************************** ***************************** changed: [192.168.3.20] TASK [Check the uptime after reboot] ****************************** ****************************** ****************************** ******* changed: [192.168.3.20] TASK [debug] ****************************** ****************************** ************************* ok: [192.168.3.20] => { "msg": " 01:17:28 up 1 min, 2 users, load average: 1.19, 0.57, 0.22" } PLAY RECAP ****************************** ****************************** ************************** 192.168.3.20 : ok=6 changed=3 unreachable=0 failed=0 [root@ansible-server ~]#
If the target nodes are very slow to reboot, you can increase the reboot timeout using additional option.
- name: Reboot a slow machine that might have lots of updates to apply reboot: reboot_timeout: 3600
Refer the ansible reboot module page to know more about additional parameters. Hope this article is informative to you. Share the knowledge with your colleagues.
Leave a Reply