Skip to content

Commit

Permalink
Merge pull request #29 from greg-hellings/add_flexible_hostname_ip
Browse files Browse the repository at this point in the history
Add optional IP address for hostnames
  • Loading branch information
greg-hellings authored Aug 12, 2020
2 parents 2e52bf0 + 4d2a83b commit c96bec0
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions roles/hostname/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
hostname: "{{ ansible_fqdn }}"
hostname_aliases: []
hostname_inject_hosts_files: true
hostname_inject_hosts_ip_address: "127.0.0.1"

# For host file inject task only
hostname_become: true
Expand Down
3 changes: 0 additions & 3 deletions roles/hostname/molecule/default/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,3 @@ driver:
name: openstack
platforms:
- name: hostname_default
provisioner:
playbooks:
converge: playbook.yml
9 changes: 9 additions & 0 deletions roles/hostname/molecule/set_ip/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
driver:
name: openstack
platforms:
- name: hostname_default
provisioner:
inventory:
group_vars:
all:
hostname_inject_hosts_ip_address: "1.2.3.4"
20 changes: 20 additions & 0 deletions roles/hostname/molecule/set_ip/tests/test_hostname.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def test_hostname_set(host):
assert host.run("hostname -f").stdout.strip() == "dev.example.com"


def test_hostname_resolves(host):
cmd = _call(host, 'dev.example.com')
assert cmd == '1.2.3.4'


def test_hostname_aliases_resolve(host):
cmd = _call(host, 'dev')
assert cmd == '1.2.3.4'


def _call(host, name):
cmd = host.run("ping -c 1 dev.example.com")
lines = cmd.stdout.split("\n")
line = lines[0]
line = line[line.find('(') + 1:line.find(')')]
return line
File renamed without changes.
2 changes: 1 addition & 1 deletion roles/hostname/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
- name: inject hostname to hosts file
blockinfile:
path: /etc/hosts
block: "127.0.0.1 {{ hostname }} {{ hostname_aliases | join(' ') }}"
block: "{{ hostname_inject_hosts_ip_address }} {{ hostname }} {{ hostname_aliases | join(' ') }}"
state: present
become: "{{ hostname_become }}"
become_user: "{{ hostname_become_user }}"
Expand Down

0 comments on commit c96bec0

Please sign in to comment.