mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-08-03 12:44:22 -07:00
Simplify register example
This commit is contained in:
parent
e108d1758c
commit
57c9534abb
1 changed files with 10 additions and 11 deletions
|
@ -10,20 +10,19 @@
|
||||||
tasks:
|
tasks:
|
||||||
|
|
||||||
# it is possible to save the result of any command in a named register. This variable will be made
|
# 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
|
# available to tasks and templates made further down in the execution flow.
|
||||||
# of a simple 'cat' command in a variable called 'motd_contents'
|
|
||||||
|
|
||||||
- action: shell cat /etc/motd
|
- action: shell grep hi /etc/motd
|
||||||
register: motd_contents
|
ignore_errors: True
|
||||||
|
register: motd_result
|
||||||
|
|
||||||
# and here we access the register. Note that motd_contents as a variable is structured data because
|
# and here we access the register. Note that variable is structured data because
|
||||||
# it is a return from the command module. The shell module makes available variables such as
|
# 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
|
# as 'stdout', 'stderr', and 'rc'.
|
||||||
# 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
|
# here we run the next action only if the previous grep returned true
|
||||||
# time python really seeps into ansible's language.
|
|
||||||
|
|
||||||
- action: shell echo "motd contains the word hi"
|
- action: shell echo "motd contains the word hi"
|
||||||
only_if: "'${motd_contents.stdout}'.find('hi') != -1"
|
only_if: "${motd_result.rc} == 0"
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue