https://github.com/francomile/ansible-role-set-hostname
Ansible Role Set Hostname
https://github.com/francomile/ansible-role-set-hostname
ansible-role hostname hosts-file
Last synced: 4 months ago
JSON representation
Ansible Role Set Hostname
- Host: GitHub
- URL: https://github.com/francomile/ansible-role-set-hostname
- Owner: francomile
- License: gpl-2.0
- Created: 2024-06-15T22:34:59.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-16T23:25:01.000Z (almost 2 years ago)
- Last Synced: 2025-09-11T01:52:39.709Z (9 months ago)
- Topics: ansible-role, hostname, hosts-file
- Language: Jinja
- Homepage:
- Size: 13.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# Ansible Set-Hostname Role
[](https://github.com/francomile/ansible-role-set-hostname/actions/workflows/ansible_lint.yml)
[](https://github.com/francomile/ansible-role-set-hostname/actions/workflows/push_to_galaxy.yml)
## Actions of the Role
* Set hostname via systemd
* Copy /etc/hostname template in place
* Copy /etc/hosts template in place
## Common Usage
```yaml
roles:
- { role: francomile.set-hostname,
# Set default instance hostname, by default it will pick the inventory hostname.
set_hostname_default_hostname: "{{ inventory_hostname }}",
set_hostname_default_ip: "{{ ansible_default_ipv4.address|default(ansible_all_ipv4_addresses[0]) }}",
set_hostname_set_hosts_file: false, # boolean to define if you whish to replace /etc/hosts with your template. Default is false
# If you set your set_hostname_set_hosts_file to 'true', add your desired entries for /etc/hosts file here in this dictionary below.
# The entry will be added to /etc/hosts in format below:
#
set_hostname_hosts_list: {
entry0: {
ip_address: "{{ default_ip }}",
host_name: "{{ default_hostname }}"
},
entry1: {
ip_address: "172.23.132.17",
host_name: "www.myhost.tld"
}
},
tags: ["set-hostname"]
}
```
## Run the playbook
Run without setting /etc/hosts file:
```shell
ansible-playbook -i inventory playbook.yaml --tags "set-hostname"
```
If you need to copy your /etc/hosts template, you can enable it on runtime by defining an extra variable for setting `set_hostname_set_hosts_file` to `true`:
```shell
ansible-playbook -i inventory playbook.yaml --tags "set-hostname" -e '{"set_hostname_set_hosts_file": true}'
```