Skip to main content
  1. Posts/

Ansible introduction

·623 words·3 mins
Ravi Singh
Author
Ravi Singh
Software engineer with 15+ years building backend systems and cloud platforms across fintech, automotive, and academia. I write about the things I build, debug, and learn — so I don’t forget them.

Ansible introduction
#

Modules
#

  • Command module: Takes the command and executes it
  • Shell module: Executes through a shell like /bin/sh
  • Script module: Runs a local script on a remote node after transferring it.
  • raw: executes a ssh command. useful for installation python3

Adhoc commands
#

Examples:

  • ansible all - ping
  • ansible web -m command -a “uptime”
  • ansible localhost -m setup

Static inventory
#

Sample inventory:

[control]
control ansible_host=10.42.0.2

[web]
node-1 ansible_host=10.42.0.6
node-2 ansible_host=10.42.0.7
node-3 ansible_host=10.42.8

[haproxy]
haproxy ansible_host=10.42.0.100

[all:vars]
ansible_user=vagrant
ansible_ssh_private_key_file=~/.vagrant.d/insecure_pvt_key

Test adhoc commands
#

  • ansible all -i hosts.ini -u vagrant -m ping: to test if systems are up
  • ansible all -i hosts.ini -u vagrant -m setup: to test if system is setup
  • ansible webservers -i hosts -u vagrant -m yum -a “name=python36 state=present” -b: installing packages python. -a is for args for yum module, -b become root
  • Ansible is idempotent
  • ansible webservers -i hosts -u vagrant -m yum -a “name=python36 state=absent” -b: to remove python

Variables
#

Ansible can work with metadata from various sources and manage their context in the form of variable. Can be facts, filepaths, package versions, etc.

Variable precedence (ansible 2.x):

  1. Extra vars
  2. Task vars
  3. Block vars
  4. Role and include vars
  5. Play vars_files
  6. Play vars_prompt
  7. Play vars
  8. Set_facts
  9. Registered vars
  10. Host facts
  11. Playbook host_vars
  12. Playbook group_vars
  13. Inventory host_vars
  14. Inventory group_vars
  15. Inventory vars
  16. Role defaults

get_url: download an archive git: clone a source code repo

Tasks
#

Example tasks in a play

tasks:
  - name: add cache dir
    file:
      path: /opt/cache
      state: directory

  - name: install nginx
    yum:
      name: nginx
      state: latest

  - name: restart nginx
    service:
      name: nginx
      state: restarted

Handler Tasks
#

Handlers are special tasks that run at the end of the play if notified by another task (caused by change of state). If a configuration file gets changed notify a service restart it needs to run.

tasks:
  - name: add cache dir
    file:
      path: /opt/cache
      state: directory

  - name: install nginx
    yum:
      name: nginx
      state: latest
    notify: restart nginx

handlers:
  - name: restart nginx
    service:
      name: nginx
      state: restarted

Plays and Playbooks
#

Plays are ordered sets of taks to execute against host selections from your inventory. A playbook is a file containing one or more plays

Playbook example:

---
- name: install and start apache
  hosts: web
  vars:
    http_port:80
    max_clients: 200
  remote_user: root

  tasks:
  - name: install httpd
    yum: pkg=httpd state=latest
  - name: write the apache config file
    template: src=/srv/httpd.j2 dest=/etc/httpd.conf
  - name: start httpd
    service: name=httpd state=started

Playbook files
#

site.yml(or main.yml)

---
- name: install and start apache
  hosts: webservers
  remote_user: vagrant
  become: yes

  tasks:
  - name: install epel repo
    yum: name=epel-release state=present

  - name: install python bindings for SELinux
    yum: name={{item}} state=present
    with_items:
    - libselinux-python
    - libsemanage-python

  - name: test to see if SeLinux is running
    command: getenforce
    register: sestatus
    changed_when: false

  - name: install apache
    yum: name=httpd state=present

  - name: start apache
    service: name=httpd state=started enabled=yes
  • ansible-playbook -i hosts site.yml: to run playbook (execute tasks from top to bottom)

Transition playbooks to roles
#

Roles are a packages of closely related ansible content that can be shared more easily than plays alone

Roles directory structure
#

  • defaults:
  • files:
  • handlers:
  • meta:
  • molecule:
  • tasks:
  • templates:
  • vars:

Creating a new role
#

  • ansible-galaxy init –help
  • ansible-galaxy init role_name
  • meta: flush_handler: runs the handler right now

Running multiple roles in a playbook
#

Example:

---
- name: apply the common configuration to all hosts
  hosts: all
  remote_user: vagrant
  become: yes

  roles:
  - common

- name: apply the database configuration
  hosts: dbservers
  remote_user: vagrant 
  become: yes

  roles:
  - mariadb

- name: apply the apache and the web roles
  hosts: webservers
  remote_user: vagrant
  become: yes

  roles:
  - apache
  - web

- name: apply the lb configuration
  hosts: lbservers
  remote_user: vagrant
  become: yes

  roles: 
  - haproxy

Discussion