Ansible Usage Guide
Basic Concepts

Common Modules
file
state: touch/directory/absent/link
template
src: /path/to/conf.j2 (local)
dest: /path/to/conf (remote)
user
name: xxx
state: present/absent
authorized_key
user: xxx
state: present/absent
key: /path/to/key.pub
blockinfile
path: /path/to/file
block: |
___. .__ __
\_ |__ | | ____ ____ | | __
| __ \| | / _ \_/ ___\| |/ /
| \_\ \ |_( <_> ) \___| <
|___ /____/\____/ \___ >__|_ \
\/ \/ \/
Variable Precedence
Common variable precedence (later ones have higher precedence)
- inventory file
- inventory groups_vars/all
- inventory host_vars/*
- include_vars
- passed in -e key=value
For more on precedence, refer to the documentation
Custom Filters
Ansible implements template replacement and data filters based on Jinja2.
Built-in filters may not always meet your needs, in which case you can create custom filters.
For filter writing, refer to the official filters.
You need to:
- Create a
filter_plugins
directory in your playbook directory. - Create a Python file in this directory and define a
FilterModule
class. - This class should have a
filters
method that returns a dictionary, where the key is the filter name and the value is the function implementing the filter. - Write your logic within the implementation function.
Note: Use ansible --version | grep "python version"
to check the Python version used by Ansible. If you want to use Python 3, refer to this link.
Summary
Why Ansible: automation, modularity, standardization (multi-machine execution, encapsulation abstraction, module reuse)
What Ansible missed: state management (refer to Terraform and K8s)