Cyber Deals - Get up to 65% off on CKA, CKAD, CKS, KCNA, KCSA exams and courses!

Ansible Cheatsheet

Ansible Cheat Sheet

Ansible is an open-source IT automation engine which can help you to automate most of your repetitive tasks in your work life. Ansible can also improve the consistency, scalability, reliability and easiness of your IT environment.

Ansible Cheat Sheet for Quick Reference and understanding

Visit techbeatly.com for more articles.

Variables

ItemDescription
host_varsdirectory for host variable files
group_varsdirectory for group variable files
factscollecting the host specific data
registerregistered variables
varsin playbook
vars_filesin playbook
include_varsmodule
include_tasks: stuff.ymlinclude a sub task file

Task Control & Loops

ItemDescription
with_itemsthen “item” inside action
with_nestedfor nested loops
with_file
with_fileglob
with_sequence
with_random_choice
whenmeet a condition

Modules

ItemDescription
copycopy file or content
get_urldownload file
filemanage file/directories
yummanage package
servicemanage services
firewalldfirewall service
lineinfileadd a line to dest file
templateto template file with variables
debugto debug and display
add_hostadd host to inventory while play
wait_foruse for flow control
aptmanage apt-packages
shellexecute shell commands on targets

Playbooks

ItemDescription
ansible-playbook <YAML>Run on all hosts defined
ansible-playbook <YAML> -f 10Fork - Run 10 hosts parallel
ansible-playbook <YAML> --verboseVerbose on successful tasks
ansible-playbook <YAML> -CTest run
ansible-playbook <YAML> -C -DDry run
ansible-playbook <YAML> -l <host>Limit to run on single host

Handlers

ItemDescription
notifyto notify the handler
handlersdefine handler

Tags

ItemDescription
tagsadd tags to the tasks
--tags ‘<tag>’during playbook execution
--skip-tagsfor skipping those tags
taggedrun any tagged tasks
untaggedany untagged items
allall items

Handling Errors

ItemDescription
ignore_errorsproceed or not if any error on current task
force_handlerscall handler even the play failed
failed_whenmark the task as failed if a condition met
changed_whenset “ok” or “failed” for a task
blocklogical grouping of tasks (can use with when)
rescueto run if block clause fails
alwaysalways run even block success or fails

Jinja2 Templates

To be added later with examples

Roles

main file in sub-directories should be main.yml Role variable can define under roles directive

Role Directories

ItemDescription
defaultsdefault value of role variables
filesstatic files referenced by role tasks
handlersrole’s handlers
metarole info like Author, Licence, Platform etc
tasksrole’s task defenition
templatesjinja2 templates
teststest inventory and test.yml
varsrole’s variable values
pre_taskstasks before role
post_taskstasks after role

Ansible Galaxy

https://galaxy.ansible.com

ItemDescription
ansible-galaxy search ‘install git’ --platform elsearch for a role
ansible-galaxy info <role-name>display role information
ansible-galaxy install <role-name> -p <directory>install role from galaxy
ansible-galaxy listto list local roles
ansible-galaxy remove <role-name>remove role
ansible-galaxy init --offline <role-name>initiate a role directory

Delegation

ItemDescription
delegate_to: localhostrun the task on localhost instead of inventory item
delegate_factsassign the gathered facts from the tasks to the delegated host instead of current host

Parallelism

ItemDescription
forksnumber of forks or parallel machines
--forkswhen using ansible-playbook
serialcontrol number parallel machines
async: 3600wait 3600 seconds to complete the task
poll: 10check every 10 seconds if task completed
wait_formodule to wait and check if specific condition met
async_statusmodule to check an async task status

Ansible Vault

ItemDescription
ansible-vault create newfilecreate a new vault file
ansible-vault view newfileview file which is already ansible vaulted
ansible-vault edit newfileEdit file
ansible-vault view --vault-password-file .secret newfileProvide vault password as file
ansible-vault decrypt newfileRemove encryption or vault
ansible-vault rekey newfilechange vault password
--ask-vault-pass or
--vault-password-file <secret-password-file>
ask for vault password for ansible-playbook

Troubleshooting

ItemDescription
log_pathwhere logs are saved
debugmodule for debugging
--syntax-checksyntax checking for playbooks before they run
--steprun playbook step by step
--start-at-taskrun a playbook but start at specific task
--checkcheck mode
--diffwill show the expected changes if you run the playbook, but will not do any changes (kind of dry run)
urimodule for testing url
scriptmodule for running script and return success code
statmodule to check the status of files/dir
assertcheck file exist

if you find any mistakes, please feel free to update or let me know

Thanks

LinkedIn | techbeatly.com