Variables to Retrieve the Results of Running Commands:
Register
Handlers
Using loops:
Testing when conditional and loops using dictionary key-value
Ignore errors on certain hosts:
key:
ingore_errors: yes
Block-rescue-always
Note: always is not mandatory
similar to try-except
Note: that module names after block keyword should have -, which is not common
–
- name: install httpd
hosts: servers
become: yes
tasks:
- name: install httpd
yum:
name: httpd
state: latest
- name: create index file
copy:
dest: /var/www/html/index.html
content: “Hello World”
- name: block-rescue-always
hosts: localhost
tasks:
- name: check if index file exists
block:
- get_url:
url: “http://{{ item.server }}/index.html”
dest: /tmp/{{ item.file }}
with_items:
- { server: “10.0.1.42”, file: ‘server1’ }
- { server: “10.0.1.176”, file: ‘server2’ }
rescue:
- debug
msg: “Service is unreacheable”
always:
- debug:
msg: “ Testing block-rescue module”
…
tags:
should be set on the level of module
ansible-playbook -i inventory tags.yaml –skip-tags “install,start” is the same as ansible-playbook -i inventory tags.yaml –tags “users,password”
Force_handlers
Forces previously notifies handler to run
If multiple tasks are notifying the same handler and one of those tasks fail, then the handler will not run and this can leave a host in an unexpected state.
For example, a configuration file being updated, which will then notify the handler that a service needs to be restarted but then another task in the same play has a failure, which causes the handler not to run. So to force that to always run, set force handlers equal to true
When
Failed_when
Defines failure conditions