Ansible can be used for both configuration management and deployment

What is configuration management?
In configuration management, we describe the desired ‘state of the server’

  • the right packages should be installed
  • configuration files should contain the expected values and have the expected permissions
  • the right services are running, and so on

What is deployment?

  • generating binaries or static assets (if necessary)
  • copying the required files to the server(s)
  • and then starting up the services.

Hello World
Lets try Hello World of ansible

1
2
# Create a hello_ansible/ directory and create file named inventory with following content.
127.0.0.1

1
2
3
4
5
6
7
8
 # Create main.yml file with following contents
---
- name: Hello Ansible
hosts: all

tasks:
- name: Hello World
shell: echo "Hello World" >> hello.txt

Finally run it with following command :
ansible-playbook hello.yml -i inventory --connection=local


Working of ansible

working
State of Task

  • state = ok
    If the state of the host matches the arguments of the module, then Ansible takes no action on the host and responds with a state of ok.
  • state = changed
    If there is a difference between the state of the host and the arguments to the module, then Ansible will change the state of the host and return changed.

Ansible common terms :

ansible-terms

PLAYBOOK

  • A playbook can have multiple plays. A play can have multiple tasks. A task has exactly one module like git, file, copy, command etc

INVENTORY FILE

  • Contains list of hosts
  • hosts can be grouped
  • groups of groups can also be created using keyword :children

VARIABLES

  • variables for each group can be placed in group_vars directory with file name that of group

WHEN

  • ‘when’ is a conditional that should evaluate to true for the task to be executed.

REGISTER

  • stdout of a command can be copied to a variable using register. this variable can be used in a conditional in another task.

ANSIBLE GALAXY

Advantages :

  • Agentless - needs python pre installed in all hosts
  • Parallel operations : No repeats


  • Readymade roles written by community at ansible galaxy
  • Readability of scripts

More notes about Ansible :

  • Ansible default shell type is bourne shell located at /bin/sh. It also supports csh, fish and windows powershell
  • Ansible uses python located at /usr/bin/python