HOstname always change

15 views
Skip to first unread message

Pietro Leone

unread,
Aug 29, 2016, 10:24:57 AM8/29/16
to Ansible Project
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.

Dick Davies

unread,
Aug 30, 2016, 3:59:56 AM8/30/16
to ansible list
nomemacchina isn't a simple string.

add a debug: task to print it, it's a data structure. start with
nomemacchina.stdout and take it from there.
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/2815f040-e382-436f-a09c-5714de9b94e8%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages