14. What is the problem?
• Can not be run on different OS
• Imperative style
• Possible issues with re-runs
• Not scalable
• Too many words
• Not human readable
14
15. What we need?
• Write once run anywhere
• Declarative style
• Idempotent operation
• Human readable
15
41. Inventory
File in format INI or YAML that contains:
• Lists of available host
• Group of hosts
• Groups of groups
• Host & Group variables(configuration)
41
42. Playbook
Is a point of entry in YAML format.
• Each playbook is composed of one or more
plays in a list.
42
43. Play
Mapps a group of hosts to some well defined
roles
• Each play starts with a name parameter,
declare its targeted hosts, tasks and handlers
to use.
43
44. Task
The goal of each task is to execute a modules,
with very specific arguments.
44
46. Handler
A Handler is exactly the same as a Task, but it
will run when called by another Task.
A Handler runs once end the end plays.
46
47. Playbook summary
• Playbook contains plays
• Play contains tasks
• Tasks run sequentially and execute modules
• Handlers are triggered by tasks and runs once
end the end plays
47
50. Demo Summary
• Inventory in differents formats
• Can run through ssh with
• Can retry with failed hosts
• Module is magic
• Handlers - running on change
50
54. Idempotent
Modules should be idempotent, that is, running
a module multiple times in a sequence should
have the same effect as running it just once.
54
55. But...
Some module can not be idempotent.
• shell - we are responsible for idempotency
• ec2 - is not idempotent without special parms
• etc.
55
57. Dynamic Inventory
We can create script to create inventory file
• --list - inventory file in json format
• --host - host variable in yaml format
We can use cli ansible-inventory
57
58. Host variable/Facts/Gether facts
Facts is result of setup module on remote
systems.
Facts starts with ansible_*
Information discovered from systems: Facts
58
59. Own variable
• Defined in Inventory or playbook per play
• Defined from included files
• Registered variables in tasks
59
61. How to use variable
• {{ variable }}
• YAML syntax requires that if you start a value
with {{ variable }} you quote
61
62. How to use variable
tasks:
- debug: msg={{variable}}
62
tasks:
- debug:
msg: '{{variable}}'
63. Filters
Filters in Ansible are from Jinja2, and are used
for transforming data inside a template
expression
Filters: mandatory, default(value), hash,
checksum, min, max, unique, intersect,
difference, random, etc
63
75. Error Handling
The tasks in the block would execute normally, if
there is any error the rescue section would get
executed.
The always section runs no matter what
previous error did or did not occur in the block
and rescue sections.
75
76. Error details
76
• ansible_failed_task - the task that returned
‘failed’ and triggered the rescue.
• ansible_failed_result - the captured return
result of the failed task that triggered the
rescue.