mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Add ability to store and access module results later on in the play. See examples/playbooks/register_logic for details.
This commit is contained in:
parent
6c5761a79e
commit
05a128c2be
4 changed files with 35 additions and 4 deletions
|
@ -27,7 +27,7 @@ Ansible Changes By Release
|
|||
* allow variables in parameterized task include parameters (regression)
|
||||
* make remote_md5 internal function work with non-bash shells
|
||||
* allow user to be passed in via --extra-vars (regression)
|
||||
* ${last_result} variable stores the last result for each host
|
||||
* add ability to store the result of any command in a register (see examples/playbooks/register_logic.yml)
|
||||
|
||||
0.6 "Cabo" -- August 6, 2012
|
||||
|
||||
|
|
29
examples/playbooks/register_logic.yml
Normal file
29
examples/playbooks/register_logic.yml
Normal file
|
@ -0,0 +1,29 @@
|
|||
# here's a cool advanced topic about how to perform conditional logic in ansible without resorting
|
||||
# to writing your own module that defines facts. You can do that too, and it's easy to do, but
|
||||
# often you just want to run a command and then decide whether to run some steps or not. That's
|
||||
# easy to do, and here we'll show you how.
|
||||
|
||||
- name: test playbook
|
||||
user: root
|
||||
hosts: all
|
||||
|
||||
tasks:
|
||||
|
||||
# it is possible to save the result of any command in a named register. This variable will be made
|
||||
# available to tasks and templates made further down in the execution flow. Here we save the result
|
||||
# of a simple 'cat' command in a variable called 'motd_contents'
|
||||
|
||||
- action: shell cat /etc/motd
|
||||
register: motd_contents
|
||||
|
||||
# and here we access the register. Note that motd_contents as a variable is structured data because
|
||||
# it is a return from the command module. The shell module makes available variables such as
|
||||
# as 'stdout', 'stderr', and 'rc'. Here's a rather trivial example that runs an arbitrary step
|
||||
# if and only if the motd file contained the word 'hi'. Remember that only_if statements are
|
||||
# Python expressions. This is as complicated as Ansible syntax is going to get, and the only
|
||||
# time python really seeps into ansible's language.
|
||||
|
||||
- action: shell echo "motd contains the word hi"
|
||||
only_if: "'${motd_contents.stdout}'.find('hi') != -1"
|
||||
|
||||
|
|
@ -224,7 +224,8 @@ class PlayBook(object):
|
|||
for host, result in results['contacted'].iteritems():
|
||||
facts = result.get('ansible_facts', {})
|
||||
self.SETUP_CACHE[host].update(facts)
|
||||
self.SETUP_CACHE[host]['last_result'] = result
|
||||
if task.register:
|
||||
self.SETUP_CACHE[host][task.register] = result
|
||||
|
||||
# flag which notify handlers need to be run
|
||||
if len(task.notify) > 0:
|
||||
|
|
|
@ -23,13 +23,13 @@ class Task(object):
|
|||
__slots__ = [
|
||||
'name', 'action', 'only_if', 'async_seconds', 'async_poll_interval',
|
||||
'notify', 'module_name', 'module_args', 'module_vars',
|
||||
'play', 'notified_by', 'tags', 'with_items', 'first_available_file', 'ignore_errors'
|
||||
'play', 'notified_by', 'tags', 'register', 'with_items', 'first_available_file', 'ignore_errors'
|
||||
]
|
||||
|
||||
# to prevent typos and such
|
||||
VALID_KEYS = [
|
||||
'name', 'action', 'only_if', 'async', 'poll', 'notify', 'with_items', 'first_available_file',
|
||||
'include', 'tags', 'ignore_errors'
|
||||
'include', 'tags', 'register', 'ignore_errors'
|
||||
]
|
||||
|
||||
def __init__(self, play, ds, module_vars=None):
|
||||
|
@ -46,6 +46,7 @@ class Task(object):
|
|||
self.name = ds.get('name', None)
|
||||
self.action = ds.get('action', '')
|
||||
self.tags = [ 'all' ]
|
||||
self.register = ds.get('register', None)
|
||||
|
||||
# notified by is used by Playbook code to flag which hosts
|
||||
# need to run a notifier
|
||||
|
|
Loading…
Reference in a new issue