Ansible

Ansible Cheat Sheet

Ansible is a suite of software tools that enables infrastructure as code.

Environment Setup

$ sudo apt-get update
$ sudo apt-get install software-properties-common
$ sudo apt-add-repository ppa:ansible/ansible $ sudo apt-get update
$ sudo apt-get install ansible

Ansible Terms

Before we get into the important Ansible commands, first, let’s understand its basic terminology.

  • Server: An entity that provides service for our Ansible
  • Machine: A physical machine, a VM, or a container
  • Target machine: An end machine to be configured by Ansible
  • Task: An action
  • Playbook: A location where YAML files are written and executed

Ad-hoc Commands

General syntax of an ad-hoc command:

Command hostgroup module/options [arguments]
FunctionCommand
To check the connectivity of hosts#ansible <group> -m ping
To reboot hosts#ansible <group> -a “/bin/reboot”
To check the host system’s info#ansible<group> -m setup | less
To transfer files#ansible <group> -m copy -a “src=home/ansible dest=/tmo/home”
To create a new user#ansible<group> -m user -a “name=ansible password= <encrypted password>”
To delete a user#ansible<group> -m user -a “name=ansible state- absent”
To check if a package is installed and to update it#ansible<group> -m yum -a “name=httpd state=latest”
To check if a package is installed but not to update it#ansible<group> -m yum -a “name=httpd state=present”
To check if a package is of a specific version#ansible<group> -m yum -a “name=httpd-1.8  state=latest”
To check if a package is not installed#ansible <group> -m yum -a “name= httpd state= absent
To start a service#ansible<group> -m service -a “name= httpd state=”started”
To stop a service#ansible<group> -m service -a “name= httpd state=”stopped”
To restart a service#ansible<group> -m service -a “name= httpd state=”restarted”

The general syntax of an ad-hoc command:

Command hostgroup module/options [arguments]
FunctionCommand
To check the connectivity of hosts#ansible <group> -m ping
To reboot hosts#ansible <group> -a “/bin/reboot”
To check the host system’s info#ansible<group> -m setup | less
To transfer files#ansible <group> -m copy -a “src=home/ansible dest=/tmo/home”
To create a new user#ansible<group> -m user -a “name=ansible password= <encrypted password>”
To delete a user#ansible<group> -m user -a “name=ansible state- absent”
To check if a package is installed and to update it#ansible<group> -m yum -a “name=httpd state=latest”
To check if a package is installed but not to update it#ansible<group> -m yum -a “name=httpd state=present”
To check if a package is of a specific version#ansible<group> -m yum -a “name=httpd-1.8  state=latest”
To check if a package is not installed#ansible <group> -m yum -a “name= httpd state= absent
To start a service#ansible<group> -m service -a “name= httpd state=”started”
To stop a service#ansible<group> -m service -a “name= httpd state=”stopped”
To restart a service#ansible<group> -m service -a “name= httpd state=”restarted”

Playbooks

Sample playbook/YAML file:

---
name: install and configure DB
hosts: testServer
become: yes
vars:
oracle_db_port_value: 1521
tasks:
-name: Install the Oracle DB
yum: <code to install the DB>
-name: Ensure the installed service is enabled and running
service:
name: <your service name>

Some general tags in YAML:

  • Name: Name of a playbook
  • Hosts: A mandatory field that specifies the list of hosts and the tasks that can be performed on the same machine or a different one
  • Vars: Defines the variables that we can use
  • Tasks: The list of actions that need to be performed contains the name of the task (a task is always linked to a module)

Variables

hosts : <your hosts>
vars:
tomcat_port : 8080

Important Keywords

  • Block: The Ansible syntax to execute a given block
  • Name: The name of the block
  • Action: The code to be executed and is next to the action tag
  • Register: Registers the output
  • Always: Executes no matter what the state is
  • Msg: Displays the message