mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-09-29 21:13:23 -07:00
Merge pull request #4914 from resmo/fix/module-host
host: bug fixes and improvments
This commit is contained in:
commit
f90df824c3
2 changed files with 108 additions and 15 deletions
|
@ -75,7 +75,6 @@ class Host(object):
|
||||||
self._ip_matches = False
|
self._ip_matches = False
|
||||||
self._hostname_matches = False
|
self._hostname_matches = False
|
||||||
self._aliases_matches = False
|
self._aliases_matches = False
|
||||||
self._has_aliases = False
|
|
||||||
self._found_on_line = -1
|
self._found_on_line = -1
|
||||||
|
|
||||||
def validate_has_hostname_on_present(self):
|
def validate_has_hostname_on_present(self):
|
||||||
|
@ -100,7 +99,7 @@ class Host(object):
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
for lineno, line in enumerate(self._hostsfile_lines):
|
for lineno, line in enumerate(self._hostsfile_lines):
|
||||||
if line.startswith("#"):
|
if line.startswith("#") or line.startswith("\n"):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
ip = line.split()[0:1]
|
ip = line.split()[0:1]
|
||||||
|
@ -117,16 +116,12 @@ class Host(object):
|
||||||
|
|
||||||
# only look at aliases if we found hostname or ip
|
# only look at aliases if we found hostname or ip
|
||||||
if self._hostname_matches or self._ip_matches:
|
if self._hostname_matches or self._ip_matches:
|
||||||
if aliases:
|
if self.aliases == aliases:
|
||||||
self._has_aliases = True
|
|
||||||
if self.aliases and self.aliases == aliases:
|
|
||||||
self._aliases_matches = True
|
self._aliases_matches = True
|
||||||
break
|
break
|
||||||
|
|
||||||
def full_entry_exists(self):
|
def full_entry_exists(self):
|
||||||
if self.aliases and not self._aliases_matches:
|
return self._ip_matches and self._hostname_matches and self._aliases_matches
|
||||||
return False
|
|
||||||
return self._ip_matches and self._hostname_matches
|
|
||||||
|
|
||||||
def entry_exists(self):
|
def entry_exists(self):
|
||||||
return self._ip_matches or self._hostname_matches
|
return self._ip_matches or self._hostname_matches
|
||||||
|
@ -157,7 +152,7 @@ def main():
|
||||||
state=dict(default='present', choices=['present', 'absent'], type='str'),
|
state=dict(default='present', choices=['present', 'absent'], type='str'),
|
||||||
ip=dict(default=None, type='str'),
|
ip=dict(default=None, type='str'),
|
||||||
hostname=dict(default=None, type='str'),
|
hostname=dict(default=None, type='str'),
|
||||||
aliases=dict(default=None, type='str'),
|
aliases=dict(default='', type='str'),
|
||||||
),
|
),
|
||||||
supports_check_mode=True
|
supports_check_mode=True
|
||||||
)
|
)
|
||||||
|
@ -167,13 +162,13 @@ def main():
|
||||||
result['state'] = host.state
|
result['state'] = host.state
|
||||||
result['changed'] = False
|
result['changed'] = False
|
||||||
|
|
||||||
err = host.validate_has_hostname_on_present()
|
result['msg'] = host.validate_has_hostname_on_present()
|
||||||
if err:
|
if result['msg']:
|
||||||
module.fail_json(msg=err)
|
module.fail_json(**result)
|
||||||
|
|
||||||
err = host.validate_has_ip_or_hostname_on_absent()
|
result['msg'] = host.validate_has_ip_or_hostname_on_absent()
|
||||||
if err:
|
if result['msg']:
|
||||||
module.fail_json(msg=err)
|
module.fail_json(**result)
|
||||||
|
|
||||||
host.proceed_hosts_entries()
|
host.proceed_hosts_entries()
|
||||||
if host.state == 'present':
|
if host.state == 'present':
|
||||||
|
|
98
test/playbook-module-host.yml
Normal file
98
test/playbook-module-host.yml
Normal file
|
@ -0,0 +1,98 @@
|
||||||
|
---
|
||||||
|
# run with option -i localhost
|
||||||
|
# need root permissions
|
||||||
|
|
||||||
|
- name: host module testing
|
||||||
|
hosts: localhost
|
||||||
|
connection: local
|
||||||
|
gather_facts: no
|
||||||
|
sudo: yes
|
||||||
|
|
||||||
|
pre_tasks:
|
||||||
|
- name: backup /etc/hosts
|
||||||
|
command: cp /etc/hosts /etc/hosts.origin
|
||||||
|
|
||||||
|
post_tasks:
|
||||||
|
- name: restore /etc/hosts
|
||||||
|
command: cp /etc/hosts.origin /etc/hosts
|
||||||
|
|
||||||
|
tasks:
|
||||||
|
- name: test add a record
|
||||||
|
host: hostname=foobar ip=192.168.123.1
|
||||||
|
register: result
|
||||||
|
failed_when: not result.changed
|
||||||
|
|
||||||
|
- name: test error handling only hostname given on present
|
||||||
|
host: hostname=foobar
|
||||||
|
register: result
|
||||||
|
failed_when: result.changed
|
||||||
|
|
||||||
|
- name: test error handling only ip given on present
|
||||||
|
host: ip=192.168.123.1
|
||||||
|
register: result
|
||||||
|
failed_when: result.changed
|
||||||
|
|
||||||
|
- name: test record exists
|
||||||
|
host: hostname=foobar ip=192.168.123.1
|
||||||
|
register: result
|
||||||
|
failed_when: result.changed
|
||||||
|
|
||||||
|
- name: test remove record using hostname
|
||||||
|
host: hostname=foobar state=absent
|
||||||
|
register: result
|
||||||
|
failed_when: not result.changed
|
||||||
|
|
||||||
|
- name: test remove not existing record using hostname
|
||||||
|
host: hostname=foobar state=absent
|
||||||
|
register: result
|
||||||
|
failed_when: result.changed
|
||||||
|
|
||||||
|
- name: test add a record again
|
||||||
|
host: hostname=foobar ip=192.168.123.1
|
||||||
|
register: result
|
||||||
|
failed_when: not result.changed
|
||||||
|
|
||||||
|
- name: test remove record using ip
|
||||||
|
host: ip=192.168.123.1 state=absent
|
||||||
|
register: result
|
||||||
|
failed_when: not result.changed
|
||||||
|
|
||||||
|
- name: test remove not existing record using ip
|
||||||
|
host: ip=192.168.123.1 state=absent
|
||||||
|
register: result
|
||||||
|
failed_when: result.changed
|
||||||
|
|
||||||
|
- name: test add a record with alias
|
||||||
|
host: hostname=foobar ip=192.168.123.1 aliases=foobar.com,foobar.net
|
||||||
|
register: result
|
||||||
|
failed_when: not result.changed
|
||||||
|
|
||||||
|
- name: test add an existing record with alias
|
||||||
|
host: hostname=foobar ip=192.168.123.1 aliases=foobar.com,foobar.net
|
||||||
|
register: result
|
||||||
|
failed_when: result.changed
|
||||||
|
|
||||||
|
- name: test add an existing record with changed alias
|
||||||
|
host: hostname=foobar ip=192.168.123.1 aliases=foobar.net,foobar.com
|
||||||
|
register: result
|
||||||
|
failed_when: not result.changed
|
||||||
|
|
||||||
|
- name: test remove aliases from existing record
|
||||||
|
host: hostname=foobar ip=192.168.123.1
|
||||||
|
register: result
|
||||||
|
failed_when: not result.changed
|
||||||
|
|
||||||
|
- name: test add aliases for existing record
|
||||||
|
host: hostname=foobar ip=192.168.123.1 aliases=foobar.net,foobar.com
|
||||||
|
register: result
|
||||||
|
failed_when: not result.changed
|
||||||
|
|
||||||
|
- name: test change ip on existing record
|
||||||
|
host: hostname=foobar ip=192.168.123.2
|
||||||
|
register: result
|
||||||
|
failed_when: not result.changed
|
||||||
|
|
||||||
|
- name: test change hostname on existing record
|
||||||
|
host: hostname=barfoo ip=192.168.123.2
|
||||||
|
register: result
|
||||||
|
failed_when: not result.changed
|
Loading…
Add table
Add a link
Reference in a new issue