10 Things you should start using in your Ansible Playbook

1. with_items:

# Installing packages individually ( Slower Process )- name: Installing Git
apt:
name: git
update_cache: yes
- name: Installing nginx
apt:
name: nginx
update_cache: yes
- name: Installing memcached
apt:
name: memcached
update_cache: yes
# Installing Packages with one Task ( Faster Process )- name: Installing Packages
apt:
name: "{{ item }}"
update_cache: yes
with_items:
- git
- nginx
- memcached

2. Facts Gathering

192.168.56.7 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"172.17.0.1",
"10.0.2.15",
"192.168.56.7"
],
( many more facts)...
---- hosts: web
gather_facts: False
- hosts: web
gather_facts: True
gather_subset: network
- hosts: web
gather_facts: True
gather_subset: network,virtual
[general]
sample_value=1
sample_fact=normal
ansible all -i local -m setup -a "filter=ansible_local"
192.168.56.7 | SUCCESS => {
"ansible_facts": {
"ansible_local": {
"prefrences": {
"general": {
"sample_fact": "normal",
"sample_value": "1"
}
}
}
},
"changed": false,
"failed": false
}

3. any_errors_fatal

---
- hosts: web
any_errors_fatal: true

4. max_fail_percentage

---
- hosts: web
max_fail_percentage: 30
serial: 30

5. run_once

---
- hosts: web
tasks:
- name: Initiating Database
script: initalize_wp_database.sh
run_once: true

6. Ansible Vault

ansible-vault create encryptme.yml
ansible-vault encrypt filename.yml
ansible-vault --new-vault-id dev --new-vault-password-file=development encrypt dev_config.yml
ansible-vault --new-vault-id prod --new-vault-password-file=production encrypt prod_config.yml
ansible-vault --vault-id prod@prompt decrypt prod_config.yml

7. No_logs

Playbook ran with verbose mode to show how encrypted data can be seen.
---
- hosts: web
tasks:
- include_vars: group_vars/encrypted_data.yml
no_log: true
- name: Printing encrypted variable
debug:
var: vault_db_password
no_log: true

8. tags

---
- hosts: web

tasks:
- name: Installing git package
apt:
package: git
tags:
- package
- name: Running db setup command
command: setupdb.sh
tags:
- dbsetup
ansible-playbook -i local site.yml --tags package
ansible-playbook -i local site.yml --skip-tags package
ansible-playbook -i local site.yml --skip-tags always

9. command module idempotent (optional)

tasks:- name: Running command if file not present
command: setup_db.sh
args:
creates: /opt/database

10. Debugging Playbook on Run

---
- hosts: web
strategy: debug
tasks: ...
---
- hosts: web
strategy: debug
vars:
package: "nginx"
tasks:
- name: Install Git Package
apt:
name: "{{ new_package }}"
Video showing demo of how you can use debug for above playbook

Conclusion:

--

--

--

DevOps Consultant, ALM Consultant, CloudOps Consultant

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

NFTY Labs Technical Update

How to Make a Simple, Python Slack Bot

Your ‘Hello World’ in Decarbonization

Your ‘Hello World’ in Decarbonization

What to do if every website update causes user complaints

Git || GitHub || VS Code

Retrieve Public Holidays for a Country in Java

Spring Boot : Integration tests with environment variables

JupyterCon NYC, August 2017

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhijeet Kamble

Abhijeet Kamble

DevOps Consultant, ALM Consultant, CloudOps Consultant

More from Medium

Task 6:-How to create a Docker image using Docker File and push it on Docker Hub.

How to Harden your Nginx or Apache Server Configuration

Use Jenkins to Automate Project Versioning

Jenkins Logo