mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Merge pull request #1058 from jkleint/devel
Support iteration over command output in with_items.
This commit is contained in:
commit
b25b9fd840
3 changed files with 38 additions and 0 deletions
|
@ -258,6 +258,8 @@ class PlayBook(object):
|
|||
facts = result.get('ansible_facts', {})
|
||||
self.SETUP_CACHE[host].update(facts)
|
||||
if task.register:
|
||||
if 'stdout' in result:
|
||||
result['stdout_lines'] = result['stdout'].splitlines()
|
||||
self.SETUP_CACHE[host][task.register] = result
|
||||
|
||||
# flag which notify handlers need to be run
|
||||
|
|
|
@ -181,3 +181,20 @@ class TestPlaybook(unittest.TestCase):
|
|||
)
|
||||
play = ansible.playbook.Play(playbook, playbook.playbook[0], os.getcwd())
|
||||
assert play.hosts == ';'.join(('host1', 'host2', 'host3'))
|
||||
|
||||
def test_results_list(self):
|
||||
# Test that we can iterate over the lines of a command's stdout in a register variable.
|
||||
test_callbacks = TestCallbacks()
|
||||
playbook = ansible.playbook.PlayBook(
|
||||
playbook=os.path.join(self.test_dir, 'results_list.yml'),
|
||||
host_list='test/ansible_hosts',
|
||||
stats=ans_callbacks.AggregateStats(),
|
||||
callbacks=test_callbacks,
|
||||
runner_callbacks=test_callbacks
|
||||
)
|
||||
result = playbook.run()
|
||||
self.assertIn('localhost', result)
|
||||
self.assertIn('ok', result['localhost'])
|
||||
self.assertEqual(result['localhost']['ok'], 6)
|
||||
self.assertIn('failures', result['localhost'])
|
||||
self.assertEqual(result['localhost']['failures'], 0)
|
||||
|
|
19
test/results_list.yml
Normal file
19
test/results_list.yml
Normal file
|
@ -0,0 +1,19 @@
|
|||
---
|
||||
# Test iterating over lines of stdout stored in a register.
|
||||
- hosts: localhost
|
||||
vars:
|
||||
small_file: /etc/resolv.conf
|
||||
temp_file: /tmp/ansible_result_list.tmp
|
||||
|
||||
tasks:
|
||||
- action: command cat $small_file
|
||||
register: result
|
||||
|
||||
- action: file dest=$temp_file state=absent
|
||||
|
||||
- action: shell echo '$item' >> $temp_file
|
||||
with_items: ${result.stdout_lines}
|
||||
|
||||
- action: command diff $small_file $temp_file
|
||||
|
||||
- action: file dest=$temp_file state=absent
|
Loading…
Reference in a new issue