set_hostname.yml 614 Bytes
Newer Older
1 2 3 4 5
# This is a utility play to set a hostname
# on a server

- name: Set hostname
  hosts: all
6
  become: True
7 8
  gather_facts: False
  tasks:
9 10
    - name: "Ensure we have a hostname"
      fail: msg="you must pass a hostname_fqdn var into this play"
11 12 13 14
      when: hostname_fqdn is not defined
    - name: Set hostname
      hostname: name={{ hostname_fqdn.split('.')[0] }}
    - name: Update /etc/hosts
15 16
      lineinfile:
        dest: /etc/hosts
17
        regexp: "^127\\.0\\.1\\.1"
18
        line: "127.0.1.1{{ '\t' }}{{ hostname_fqdn.split('.')[0] }}{{ '\t' }}{{ hostname_fqdn }}{{ '\t' }}localhost"
19
        state: present