You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.7 KiB
48 lines
1.7 KiB
# 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
|
|
remote_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.
|
|
|
|
- shell: grep hi /etc/motd
|
|
ignore_errors: yes
|
|
register: motd_result
|
|
|
|
# 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
|
|
# as 'stdout', 'stderr', and 'rc'.
|
|
|
|
# here we run the next action only if the previous grep returned true
|
|
|
|
- shell: echo "motd contains the word hi"
|
|
when: motd_result.rc == 0
|
|
|
|
# alternatively:
|
|
|
|
- shell: echo "motd contains the word hi"
|
|
when: motd_result.stdout.find('hi') != -1
|
|
|
|
# or also:
|
|
|
|
- shell: echo "motd contains word hi"
|
|
when: "'hi' in motd_result.stdout"
|
|
|
|
# you can use 'stdout_lines' to loop over the registered output lines
|
|
- name: motd lines matching 'hi'
|
|
shell: echo "{{ item }}"
|
|
with_items: motd_result.stdout_lines
|
|
|
|
# you can also split 'stdout' yourself
|
|
- name: motd lines matching 'hi'
|
|
shell: echo "{{ item }}"
|
|
with_items: motd_result.stdout.split('\n')
|
|
|
|
|
|
|