ansible/examples/playbooks/register_logic.yml

29 lines
1.1 KiB
YAML
Raw Normal View History

# 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
2012-09-29 14:10:45 +02:00
# available to tasks and templates made further down in the execution flow.
2012-09-29 14:10:45 +02:00
- action: shell grep hi /etc/motd
ignore_errors: yes
2012-09-29 14:10:45 +02:00
register: motd_result
2012-09-29 14:10:45 +02:00
# 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
2012-09-29 14:10:45 +02:00
# as 'stdout', 'stderr', and 'rc'.
# here we run the next action only if the previous grep returned true
- action: shell echo "motd contains the word hi"
2012-09-29 14:10:45 +02:00
only_if: "${motd_result.rc} == 0"