0%

Ansible part11: cron module to power off remote, Wake-on-LAN module to power on remote

power off using cron

I am going to power off one of my node using cron module.

1
2
3
4
5
6
7
8
9
10
11
12
- name: cron to poweroff
hosts: web2
become: yes

tasks:
- name: power off
cron:
name: "shutdown"
user: root
minute: "52"
hour: "16"
job: "sync; /sbin/shutdown"

after running playbook, the remote will be shutdown on 16:42 everyday.

Wake-on-LAN

Compare to shutdown, power on remote is a little bit hard. Ansible has a Wake-on-LAN module can do it, but remote needs has Wake-on-LAN option is enabled.
Wake-on-LAN (WOL) is an Ethernet networking standard that allows a server to be turned on by a network message.

If you don’t see this option in your BIOS or UEFI, check the computer or motherboard’s manual to see if it supports Wake-on-LAN.

To verify if remote supports Wake-on-LAN, use ethtool -s eth0 wol g

1
2
3
[anna@cube4200 ~]$ sudo ethtool enp3s0 |grep Wake
Supports Wake-on: pumbg
Wake-on: g

It shows remote node cube4200 supports Wake-on-LAN

Let’s write playbook

1
2
3
4
5
6
7
8
9
10
11
12
13
14
- name: power on
hosts: web2
gather_facts: no

tasks:
- name: Send a magic Wake-on-LAN packet to turn on individual systems via gateway
wakeonlan:
mac: '00:f0:d2:d0:a0:0e'
broadcast: 255.255.255.255
delegate_to: localhost
- wakeonlan:
mac: '00:f0:d2:d0:a0:0e'
port: 9
delegate_to: localhost

after running

1
2
3
4
5
6
7
8
9
10
11
12
anna@ansible-controller:/tmp$ ansible-playbook wakeonlan.yml 

PLAY [power on] ********************************************************************************************************

TASK [Send a magic Wake-on-LAN packet to 00:00:5E:00:53:66] ************************************************************
changed: [cube4200 -> localhost]

TASK [wakeonlan] *******************************************************************************************************
changed: [cube4200 -> localhost]

PLAY RECAP *************************************************************************************************************
cube4200 : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

waitting few seconds, I can ssh to cube4200

1
2
anna@ansible-controller:/tmp$ ssh anna@cube4200
Last login: Thu Jun 18 00:56:29 2020 from 192.168.0.31

Notice: gather_facts: no must be configured. Because gather_facts default is yes, so when playbook is excuted, it will gather facts from remote, which is not power on yet, so ansible will output error message as following:

1
2
3
4
5
6
7
8
9
anna@ansible-controller:/tmp$ ansible-playbook wakeonlan.yml 

PLAY [power on] ********************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************
fatal: [cube4200]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: System is going down.\n\nConnection closed by 192.168.0.30 port 22", "unreachable": true}

PLAY RECAP *************************************************************************************************************
cube4200 : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0