Hi, I created this playbook:
---
- name: basic host configuration
hosts: ansibleclt00
become: True
tasks:
- name: get hostname
shell: hostname
register: nomemacchina
- name: change hostname
vars:
mio_ipaddr: 192.168.43.101
mio_prefix: 24
mio_gateway: 192.168.43.1
mio_dns: 8.8.8.8
mio_hostname: ansibleclt00
command: hostnamectl set-hostname {{ mio_hostname }}
when: nomemacchina != mio_hostname
register: nomehost
- name: reboot the server
shell: /sbin/shutdown -r 1
async: 0
poll: 0
ignore_errors: true
register: riavvio
when: nomehost.changed
When I run it:
PLAY [basic host configuration] ************************************************
TASK [setup] *******************************************************************
ok: [ansibleclt00]
TASK [get hostname] ************************************************************
changed: [ansibleclt00]
TASK [change hostname] *********************************************************
changed: [ansibleclt00]
TASK [reboot the server] *******************************************************
changed: [ansibleclt00]
PLAY RECAP *********************************************************************
ansibleclt00 : ok=4 changed=3 unreachable=0 failed=0
I'm experimenting with ansible, my idea is to verify the hostname of a server, if the name differs from the name in variable "mio_hostname", the hostname is changed (task "change hostname") to mio_hostname and then the server is rebooted.
But seems that the hostname differs everytime and I do not understand why.
Any idea?
ansible 2.1.1.0 on CentOS7
Thanks, Pietro.