mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
[PR #6602/59db302d backport][stable-7] VarDict module utils: add as_dict()
method (#6610)
VarDict module utils: add `as_dict()` method (#6602)
* VarDict module utils: add `as_dict()` method
* add changelog frag
(cherry picked from commit 59db302deb
)
Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
This commit is contained in:
parent
816d4e8f49
commit
7ee0389c98
3 changed files with 14 additions and 0 deletions
2
changelogs/fragments/6602-vardict-as-dict.yml
Normal file
2
changelogs/fragments/6602-vardict-as-dict.yml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
minor_changes:
|
||||||
|
- VarDict module utils - add method ``VarDict.as_dict()`` to convert to a plain ``dict`` object (https://github.com/ansible-collections/community.general/pull/6602).
|
|
@ -173,3 +173,6 @@ class VarDict(object):
|
||||||
@property
|
@property
|
||||||
def has_changed(self):
|
def has_changed(self):
|
||||||
return any(True for var in self.__vars__.values() if var.has_changed)
|
return any(True for var in self.__vars__.values() if var.has_changed)
|
||||||
|
|
||||||
|
def as_dict(self):
|
||||||
|
return dict((name, var.value) for name, var in self.__vars__.items())
|
||||||
|
|
|
@ -122,3 +122,12 @@ def test_vardict_change():
|
||||||
assert vd.has_changed is False
|
assert vd.has_changed is False
|
||||||
vd.xx = 12345
|
vd.xx = 12345
|
||||||
assert vd.has_changed is True
|
assert vd.has_changed is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_vardict_dict():
|
||||||
|
vd = VarDict()
|
||||||
|
vd.set("xx", 123)
|
||||||
|
vd.set("yy", 456)
|
||||||
|
vd.set("zz", 789)
|
||||||
|
|
||||||
|
assert vd.as_dict() == {"xx": 123, "yy": 456, "zz": 789}
|
||||||
|
|
Loading…
Reference in a new issue