Skip to content

Commit

Permalink
Adding role to configure hostname (#249)
Browse files Browse the repository at this point in the history
* Adding role to configure hostname

* Updated README

* Updated to have a default value for 'hostname'

* Updating based on Tyler's feedback
  • Loading branch information
oybed authored and sabre1041 committed Aug 22, 2018
1 parent 7bab6cd commit f95f7ae
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 0 deletions.
63 changes: 63 additions & 0 deletions roles/config-hostname/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
Role Description
================

This ansible role updates a hosts' hostname or FQDN

Requirements
------------

No specific system requirements.

Role Variables
--------------

| Variable | Description | Required | Defaults |
|:--------:|:-----------:|:--------:|:--------:|
| hostname | The (short) hostname to use for the host | no | the 'inventory_hostname_short' fact |
| dns_domain | DNS domain to append to the hostname to make a FQDN | no | |


Dependencies
------------

N/A

Example Playbooks
----------------

```
- hosts: my-host
roles:
- config-hostname
```

Example Inventory
----------------

**inventory/hosts:**
```
[my-host]
192.168.1.10
```

**inventory/group_vars/my-host.yml:**

```
---
hostname: "my-cool-host"
dns_domain: "my-cool-domain.com"
```



License
-------

Apache License 2.0


Author Information
------------------

Red Hat Community of Practice & staff of the Red Hat Open Innovation Labs.
7 changes: 7 additions & 0 deletions roles/config-hostname/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---

- block:
- import_tasks: prep.yml
- import_tasks: set-hostname.yml
become: True

8 changes: 8 additions & 0 deletions roles/config-hostname/tasks/prep.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---

- name: 'Install required packages'
package:
name: '{{ item }}'
state: installed
with_items:
- libselinux-python
33 changes: 33 additions & 0 deletions roles/config-hostname/tasks/set-hostname.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---

- name: Setting Default Hostname if new one is not supplied
set_fact:
hostname: "{{ hostname | default(inventory_hostname_short) }}"

- name: Setting Hostname Related Facts
set_fact:
hostname: "{{ hostname }}.{{ dns_domain }}"
when:
- dns_domain is defined
- dns_domain|trim != ''

- name: Setting hostname (FQDN)
hostname:
name: "{{ hostname }}"

- name: Check for cloud.cfg
stat:
path: "/etc/cloud/cloud.cfg"
register: cloud_cfg

- name: Prevent cloud-init updates of hostname/fqdn (if applicable)
lineinfile:
dest: /etc/cloud/cloud.cfg
state: present
regexp: "{{ item.regexp }}"
line: "{{ item.line }}"
with_items:
- { regexp: '^ - set_hostname', line: '# - set_hostname' }
- { regexp: '^ - update_hostname', line: '# - update_hostname' }
when:
- cloud_cfg.stat.exists
5 changes: 5 additions & 0 deletions roles/config-hostname/tests/inventory/group_vars/my-host.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---

hostname: "cool"
dns_domain: "hostname.com"

3 changes: 3 additions & 0 deletions roles/config-hostname/tests/inventory/hosts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

[my-host]
192.168.10.12
1 change: 1 addition & 0 deletions roles/config-hostname/tests/roles
5 changes: 5 additions & 0 deletions roles/config-hostname/tests/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---

- hosts: my-host
roles:
- role: config-hostname

0 comments on commit f95f7ae

Please sign in to comment.