-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding role to configure hostname (#249)
* Adding role to configure hostname * Updated README * Updated to have a default value for 'hostname' * Updating based on Tyler's feedback
- Loading branch information
Showing
8 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
|
||
hostname: "cool" | ||
dns_domain: "hostname.com" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
[my-host] | ||
192.168.10.12 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../roles |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
|
||
- hosts: my-host | ||
roles: | ||
- role: config-hostname |