mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-02 14:40:19 -07:00
Introduce 'changed_when' keyword to override a task's changed status with the evaluation of a Jinja2 expression
This commit is contained in:
parent
81940c8b11
commit
eb45f07ae3
5 changed files with 75 additions and 6 deletions
|
@ -382,6 +382,34 @@ class TestPlaybook(unittest.TestCase):
|
|||
|
||||
assert utils.jsonify(expected, format=True) == utils.jsonify(actual,format=True)
|
||||
|
||||
def test_playbook_changed_when(self):
|
||||
test_callbacks = TestCallbacks()
|
||||
playbook = ansible.playbook.PlayBook(
|
||||
playbook=os.path.join(self.test_dir, 'playbook-changed_when.yml'),
|
||||
host_list='test/ansible_hosts',
|
||||
stats=ans_callbacks.AggregateStats(),
|
||||
callbacks=test_callbacks,
|
||||
runner_callbacks=test_callbacks
|
||||
)
|
||||
actual = playbook.run()
|
||||
|
||||
# if different, this will output to screen
|
||||
print "**ACTUAL**"
|
||||
print utils.jsonify(actual, format=True)
|
||||
expected = {
|
||||
"localhost": {
|
||||
"changed": 3,
|
||||
"failures": 0,
|
||||
"ok": 6,
|
||||
"skipped": 0,
|
||||
"unreachable": 0
|
||||
}
|
||||
}
|
||||
print "**EXPECTED**"
|
||||
print utils.jsonify(expected, format=True)
|
||||
|
||||
assert utils.jsonify(expected, format=True) == utils.jsonify(actual,format=True)
|
||||
|
||||
def _compare_file_output(self, filename, expected_lines):
|
||||
actual_lines = []
|
||||
with open(filename) as f:
|
||||
|
|
25
test/playbook-changed_when.yml
Normal file
25
test/playbook-changed_when.yml
Normal file
|
@ -0,0 +1,25 @@
|
|||
---
|
||||
- hosts: all
|
||||
connection: local
|
||||
gather_facts: False
|
||||
|
||||
tasks:
|
||||
- action: command echo first action
|
||||
- action: command echo second action
|
||||
register: var
|
||||
changed_when: "'X' in var.stdout"
|
||||
- action: shell exit 2
|
||||
register: exit
|
||||
ignore_errors: yes
|
||||
changed_when: "exit.rc < 1"
|
||||
- action: command echo third action
|
||||
changed_when: false
|
||||
- action: file path=/ state=directory
|
||||
changed_when: true
|
||||
- action: command echo {{item}}
|
||||
register: out
|
||||
changed_when: "'e' in out.stdout"
|
||||
with_items:
|
||||
- hello
|
||||
- foo
|
||||
- bye
|
Loading…
Add table
Add a link
Reference in a new issue