Ansible Playbook Cheat Sheet



Create a user called ansible (example) on the server you intend to use Ansible to run playbooks from AND each of the Ansible nodes you intend to run playbooks on. Set the user as a sudo-capable user and include the NOPASSWD: ALL directive in /etc/sudoers. Create an SSH key with ssh-keygen on the Ansible server. Download a Printable PDF of This Cheat Sheet We, at Intellipaat, are keen to support our learners in all possible means; hence, we have created this handy reference, the Ansible cheat sheet. This cheat sheet is designed for you if you have already started learning Ansible but needs a quick and handy reference to recall what you have learned. ANSIBLE CHEAT SHEET FURTHERMORE. It uses a playbook to describe jobs and uses YAML which is human readable. It is designed for multi- tier deployment. It is agentless and works by connecting nodes through ssh. A n s i b l e. Connects nodes and pushes small programs called modules to them. Ansible-playbook playbooks/atmoplaybook.yml -e 'ATMOUSERNAME=atmouser' Limiting Playbook/Task Runs When writing Ansible, sometimes it is tedious to make a change in a playbook or task, then run the playbook It can sometimes be very helpful to run a module directly as shown above, but only against a single development host.

Linux

Ansible Playbook Cheat Sheet

Packages:

  • git
  • python
  • python-devel
  • python-pip
  • openssl
  • ansible

Linux

Be sure to install epel-release first and then update your caches (if CentOS). On Ubuntu/Debian distributions, you may install from the default repositories. Assuming CentOS, as in our course, do the following:

sudo yum install git python python-devel python-pip openssl ansible

User Accounts

Create a user called ansible (example) on the server you intend to use Ansible to run playbooks from AND each of the Ansible nodes you intend to run playbooks on. Set the user as a sudo-capable user and include the NOPASSWD: ALLdirective in /etc/sudoers.

Create an SSH key with ssh-keygen on the Ansible server. Exchange that key using ssh-copy-id on each of the nodes you are running playbooks on. This allows the playbook to run with escalated privileges as needed.

Configuration Files

  • /etc/ansible/ansible.cfg
    • Primary Ansible configuration file (agentless, daemon-less configuration, read on each ansible command run)
    • Uncomment “inventory” field
    • Uncomment “become user” field
  • /etc/ansible/hosts
    • Copy original to /etc/ansible/hosts.original
    • Create one or more sections with group names, sample below
2
4
6
8
10
12
14
16
18
20
22
- hosts: hostsToRunAgainst
become: yes
connection: ssh
vars:
var2: value
- name: Some description of what we are doing
name: httpd
notify:
handlers:
service:
state: restarted

ansible-playbook

  • Calling a playbook ansible-playbook /path/to/playbook.yaml

Inventory

Ansible Commands Cheat Sheet

  • /etc/ansible/hosts
    • Defines nodes/groups of nodes to operate against
  • $ANSIBLE_HOSTS
    • Shell variable containing one or more ansible hosts