-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Role: new:: hosts (completed & tested)
- Loading branch information
Showing
4 changed files
with
67 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,17 @@ | ||
hosts__role_enabled: false | ||
|
||
hosts__dir: /etc | ||
hosts__file: "{{ hosts__dir }}/hosts" | ||
hosts__template: etc/hosts.j2 | ||
hosts__backup: true | ||
|
||
# If enabled, the ansible inventory will be collected automatically | ||
# and will be appended to the list of hosts, when ansible_host (ip address) is defined. | ||
# ip: ansible_host, hostname: inventory_hostname_short, fqdn: inventory_hostname | ||
hosts__auto_discovery: false | ||
# Consult https://docs.ansible.com/ansible/latest/inventory_guide/intro_patterns.html for more information | ||
hosts__auto_discovery_inventories: 'webservers:&atlanta' | ||
|
||
hosts__default: [] | ||
hosts__group: [] | ||
hosts__host: [] |
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,11 @@ | ||
--- | ||
- name: Apply hosts configuration | ||
become: true | ||
ansible.builtin.template: | ||
src: "{{ hosts__template }}" | ||
dest: "{{ hosts__file }}" | ||
backup: "{{ hosts__backup | default(omit) | bool }}" | ||
when: | ||
- hosts__role_enabled | default(false) | bool | ||
- (hosts__all_ipv4 | default([]) | length > 0) or (hosts__all_ipv6 | default([]) | length > 0) | ||
register: hosts__apply |
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,15 @@ | ||
127.0.0.1 localhost.localdomain localhost | ||
{% for ipv4_host in hosts__all_ipv4 %} | ||
{{ ipv4_host.ip }} {{ ipv4_host.fqdn }} {{ ipv4_host.hostname }} | ||
{% endfor %} | ||
|
||
# The following lines are desirable for IPv6 capable hosts | ||
::1 ip6-localhost ip6-loopback | ||
fe00::0 ip6-localnet | ||
ff00::0 ip6-mcastprefix | ||
ff02::1 ip6-allnodes | ||
ff02::2 ip6-allrouters | ||
ff02::3 ip6-allhosts | ||
{% for ipv6_host in hosts__all_ipv6 %} | ||
{{ ipv6_host.ip }} {{ ipv6_host.fqdn }} {{ ipv6_host.hostname }} | ||
{% endfor %} |
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,24 @@ | ||
|
||
__hosts__auto_discovered: >- | ||
{%- if (hosts__auto_discovery | default(false) | bool) and hosts__auto_discovery_inventories is defined and hosts__auto_discovery_inventories | length > 0 -%} | ||
{%- set inventory_lookup = lookup('ansible.builtin.inventory_hostnames', hosts__auto_discovery_inventories) -%} | ||
{%- if inventory_lookup | length > 0 -%} | ||
{%- set inventory_hosts = inventory_lookup | split(',') -%} | ||
{%- set discovered_hosts = dict(hostvars) | aybarsm.helper.only_with(inventory_hosts) | dict2items | selectattr('value.ansible_host', 'defined') -%} | ||
{%- set ips = discovered_hosts | map(attribute='value.ansible_host') -%} | ||
{%- set hostnames = discovered_hosts | map(attribute='value.inventory_hostname_short') -%} | ||
{%- set fqdns = discovered_hosts | map(attribute='value.inventory_hostname') -%} | ||
{%- set auto_discovered = {'ip': ips, 'hostname': hostnames, 'fqdn': fqdns} | aybarsm.helper.to_list_of_dicts -%} | ||
{%- endif -%} | ||
{%- else -%} | ||
{%- set auto_discovered = [] -%} | ||
{%- endif -%} | ||
{{ auto_discovered }} | ||
hosts__all: "{{ (hosts__host + hosts__group + hosts__default + __hosts__auto_discovered) | | ||
aybarsm.helper.replace_aliases({'fqdn': ['hostname']}) | | ||
aybarsm.helper.selectattr([{'when': [['ip', 'defined'], ['hostname', 'defined'], ['fqdn', 'defined']]}]) | | ||
aybarsm.helper.unique_recursive(['ip', 'hostname', 'fqdn']) | default([]) }}" | ||
|
||
hosts__all_ipv4: "{{ hosts__all | selectattr('ip', 'ansible.utils.ipv4') }}" | ||
hosts__all_ipv6: "{{ hosts__all | selectattr('ip', 'ansible.utils.ipv6') }}" |