Skip to content

Commit f621dd4

Browse files
betanummericfhamme
andauthored
postgresql_pg_hba: fix false "changed" when called with "overwrite: true" (#378)
* postgresql_pg_hba: fix whitespace trimming when parsing rules with inline comments * postgresql_pg_hba: fix checking whether something changed * add changelog fragment for pg_hba "changed" fix * postgresql_pg_hba: test that nothing changes when pg_hba is called again with the same arguments (having overwrite=true) Co-authored-by: Felix Hamme <felix.hamme@ionos.com>
1 parent 7c22a90 commit f621dd4

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bugfixes:
2+
- postgresql_pg_hba - fix ``changed`` return value for when ``overwrite`` is enabled (https://github.com/ansible-collections/community.postgresql/pull/378).

plugins/modules/postgresql_pg_hba.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ def __init__(self, pg_hba_file=None, order="sdu", backup=False, create=False, ke
337337
# users, this might be totally off, but at least it is some info...
338338
self.users = set(['postgres'])
339339

340+
self.preexisting_rules = None
340341
self.read()
341342

342343
def clear_rules(self):
@@ -366,7 +367,7 @@ def read(self):
366367
line, comment = line.split('#', 1)
367368
if comment == '':
368369
comment = None
369-
370+
line = line.rstrip()
370371
# if there is just a comment, save it
371372
if line == '':
372373
if comment is not None:
@@ -381,6 +382,7 @@ def read(self):
381382
except PgHbaRuleError:
382383
pass
383384
self.unchanged()
385+
self.preexisting_rules = dict(self.rules)
384386
except IOError:
385387
pass
386388

@@ -490,7 +492,9 @@ def changed(self):
490492
'''
491493
This method can be called to detect if the PgHba file has been changed.
492494
'''
493-
return bool(self.diff['before']['pg_hba'] or self.diff['after']['pg_hba'])
495+
if not self.preexisting_rules and not self.rules:
496+
return False
497+
return self.preexisting_rules != self.rules
494498

495499

496500
class PgHbaRule(dict):

tests/integration/targets/postgresql_pg_hba/tasks/postgresql_pg_hba_bulk_rules.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@
4040
- "result.pg_hba[0].src == test_rule1.address"
4141
- "result.pg_hba[0].usr == test_rule1.users"
4242
- "result.pg_hba[0].type == test_rule1.contype"
43+
- name: 'test the same again (overwrite: true, one normal rule) to ensure nothing changed'
44+
community.postgresql.postgresql_pg_hba:
45+
<<: *pghba_defaults
46+
overwrite: true
47+
<<: *test_rule1
48+
register: result
49+
- assert:
50+
that:
51+
- "result.changed == false"
52+
4353
- name: overwrite with one bulk rule
4454
community.postgresql.postgresql_pg_hba:
4555
<<: *pghba_defaults
@@ -54,6 +64,16 @@
5464
- "result.pg_hba[0].src == test_rule2.address"
5565
- "result.pg_hba[0].usr == test_rule2.users"
5666
- "result.pg_hba[0].type == test_rule2.contype"
67+
- name: 'test the same again (overwrite: true, one bulk rule) to ensure nothing changes'
68+
community.postgresql.postgresql_pg_hba:
69+
<<: *pghba_defaults
70+
overwrite: true
71+
rules:
72+
- "{{ test_rule2 }}"
73+
register: result
74+
- assert:
75+
that:
76+
- "result.changed == false"
5777

5878
- name: test rules_behavior conflict
5979
community.postgresql.postgresql_pg_hba: "{{ pghba_defaults|combine(item)|combine({'rules': [test_rule2]}) }}"

0 commit comments

Comments
 (0)