Points To Quickly Revise Ansible

Ansbible

  • Configuration management automation tool
  • Ansible needs SSH access to the systems you need to target.
  • Uses SSH to connect to remote machines desired to be controlled and configured
  • Gets context of machine on which action is to be performed.
  • Context means – System and state information
  • Do not make any change if context does not need it
  • We teel ansible the desired state and Ansible based on the context will decide if needs to make a change.

Ansible cfg file

/etc/ansible/ansible.cfg

Inventory

  • This is a collection/list of servers you want ansible to work on.
  • It is generally a file which contains the hostname or IP of the machine.
  • Default location is =/etc/ansible/hosts

Simple adhoc commands –

  • Command below will ping all the ansible hosts listed in the inventory file.
    ansible all  -m ping 
    
  • command with more flags
    ansible all -m ping -s -k -u vagrant 
    

    -m for module -s for sudo -k for ask password -u use vagrant as a username in remote machines

Playbooks

  • This is a script way of using Ansible, where tasks to be executed are written in a file in YAML format
  • Sample playbook is written below –
    ---
    - hosts: local 
      tasks:
        - name: Install Nginx 
          apt: pkg=nginx state=installed 
    
    
    

Handlers

  • Handlers are events which runs/executes if something happens or say an event occurs
  • Handlers are called by some other tasks. If task does not execute, handlers are skipped.
  • Sample Handler to start nginx service if nginx is installed
    ---
    - hosts: local
      tasks:
       - name: Install nginx 
         apt: pkg=nginx state=installed
         notify:
          -  Start Nginx 
    
      handlers:
       - name: Start Nginx 
         service: name=nginx state =installed 
    

When

  • This is to add condition in ansible task execution.
  • Register an event, check its success using when. If not, then that specific task is skipped.
  • Sample
    ---
    - hosts: local 
      vars:
       - docroot: /var/www/serversforhackers.com/public
      tasks:
       - name: Add Nginx repository 
         apt_repository: repo='ppa:nginx/stable' state=present
         register: ppastable 
    
       - name: Install Nginx 
         apt: pkg=installed state=installed update_cache=true
         when: ppastable|success
         register: nginxinstalled  
         notify: 
          - Start Nginx 
    

    In above playbook, there is a task to add nginx repo. If it succeeds, then it registers it to ppastable. When the next task is taken to install nginx, the success event of task with regitery ppastable is checked.

Roles

Note:: With multiple tasks inside a playbook, a playbook can become difficult to manage and there are no scope connects. Based on the learning above, we have following important sections in an ansible action –

  • files
  • handlers
  • meta information
  • templates
  • tasks
  • vars

Each of the above can beocme a sirectory haing a main.yml file in them. the complete structure then is referred to as a role. A role will have similiars tasks under its hood and is managed by seperating all the components used in the ansible playbook.

Handlers directory

Put all your handlers for nginx ansible in handlers/main.yml

---
- name: Start Nginx 
  service: name=nginx state=started

- name: Reload Nginx 
  service: name=nginx state=reloaded

The above can be referenced from a central main file when required.

Templates

See a sample templated file which is generally based on Python Jinja templates/nginx-config.conf

  server {
	listen 80 default_server;
       server_name *.{{ '{{' }} domain {{ '}}' }};
       return 301 https://{{ '{{' }} domain '}}' }}$request_uri;
  }
  server {
	listen 443 default_server ssl;
       root /var/www/{{ '{{' }} domain {{ '}}'/public;
       index index.html index.htm index.php
  

Variables

vars/main.yml

---
domain: something.com
ssl_key: /etc/ssl/sfh/sfh.key
server_name: someone.something.com

Running a role

  • Edit roles_path variable in ansible.cfg file say it to /home/user/ansible/roles
  • save your nginx role in /home/user/ansible/roles/nginx
  • Create a master yaml file
    --- 
    - hosts: all 
      roles:
       - nginx 
    

    where nginx is the name of your role and is a directory in roles_path

Install multiple packages using one apt

  • Using with_items, one can mention a list of pakacges to be installed.
- name: install default packages
   apt: pkg={{ item }} state=installed 
   with_items:
     - aptitude
     - vim 
     - supervisor
  • Simple modules usages in the below given sample playbooks
    ---
     - name: create directroy under /home/aashay
       file: path=/home/aashay/test-dir state=directory mode=755
    
     - name: Git checkout from github 
       git: repo=https://github.com/ashay-maheshwari/Linux-stuff.git 
            dest=/home/aashay/.test-dir
    
     - name: Drop config fro supervisord into conf.d directory
       copy: src=supervisor-test-.conf dest=/etc/supervisor/conf.d/supervisor-test.conf
    

Post A Comment

Your email address will not be published. Required fields are marked *