Things about Ansible everybody should know

Posted By : Ankit Arora | 03-Sep-2017

What are things about Ansible everybody should know?

 

Hello everyone

Today we are going to discuss about things we should know about ansible.

Recently I have started working with Ansible and Ansible is a great & powerful configuration management .

Below are the points you would love to know about Ansible.

 

  • Running and checking tasks in dry mode

     This is one of the great feature of ansible I've loved. Using this mode,We can check what is going to happen after his getting the command.  this whole shows all the changes that have you made but nothing happens in the background.

 

$ ansible-playbook --check playbookname.yml

 

    Note: Please note that this mode might not work well in case we are using conditional steps in a playbooks.

 

  • Running task based on their tags

     For this mode we just have to add a tag and attribute in our playbooks. We can add one or more than one tags.

 

# playbook.yml
- hosts: all
 tags:
   - client
 tasks:
   - name: Download files
     tags:
       - wget
       - download
   - name: Install all the dependencies
     tags:
       - dependency



 

  • Running tasks step by step

 

    Yes we can run tasks step by step. Sometime when we don’t want to run all tasks, it’s the best option. This method offers an interactive mode & let user choose what to perform & what not. Using this feature requires --step flag.



 

# playbook.yml
- hosts: servername
 tasks:
   - name: Task one
     # ...
   - name: Task two
     # ...

$ ansible-playbook playbook.yml -i hosts --step

> Perform task: TASK: Task one (y/n/c): n
> Perform task: TASK: Task two (y/n/c): y

 

Here:-

y: yes(Run this task)
n: no    (Skip this task)

c: Continue without asking


 

  • Speed up things using pipeline, fact caching & disabling fact gathering.

 

    Enabling fact caching & disabling fact gathering speeds up the things. To disable fact gathering, simply enable gather_facts: False in playbooks.

 

- hosts: all
 gather_facts: False
 tasks:
   - name: ...
   # …

 

    To enable fact caching follow the documentation of ansible:-

    http://docs.ansible.com/ansible/latest/playbooks_variables.html#fact-caching

 

    

    Enabling pipeline is another great option as it reduces total no. of SSH connections which are required to execute modules on the remote server, in this it doesn’t copy scripts but pipes them to SSH session.

Pipelining only works if the requiretty is disabled in all the sudoers file on all remote machines & pipelining should be done carefully.

 

[ssh_connection]
pipelining = True

 

  • Handlers: Special types of tasks.

 

    They are tasks which have unique names & are executed only when notified by another task. They are executed only once & are called at the end of the playbooks. They are declared with handler clause & notify is used to trigger them.

 

Example:

 

- name: template configuration file
 template: src=template.j2 dest=/etc/foo.conf
 notify:
    - restart memcached
    - restart apache

 

Declaring handlers in end of playbook:

 

handlers:
   - name: restart
memcached
     # The service module was used, but you could use whatever module you wanted
     service: name=memcached state=restarted
   - name: restart
apache

     service: name=apache state=restarted


 

  • Running task only once.

 

    Sometimes we want to run multiple tasks but what if we want to run a task only once. Where we can use this feature? Suppose we have 7 application servers but we want to run a job only once. We have to use run_once parameter to tell Ansible to run command only once.

 

- name: run job
 command: node server.js &
 run_once: true

 

  • Use of with_items

 

With_items is used to create a variable called {{ item }} which contains the value of current iteration. Using this we can make our installation faster & easier.

 

Example:

 

# Installing all packages with one task (faster)
 - name: install required packages using the apt module
   apt: package={{ item }}  update_cache=yes
   sudo: True
   with_items:
     - git
     - memcached
     - nginx

 # Installing packages individually (slower)
 - name: install git
   apt: package=git update_cache=yes
   sudo: True

 - name: install
memcached
   apt: package=memcached update_cache=yes
   sudo: True

 - name: install
nginx

   apt: package=nginx update_cache=yes
   sudo: True


 

  • Listing all the tasks in a playbook.

 

Forgot what your playbook does & don’t want to check what’s in there because it’s too large? Don’t worry, use --list-tasks with ansible-playbook command.

 

$ ansible-playbook installing-jenkins.yml --list-tasks
PLAY: #1
 tasks:
   TASK: meta
   TASK: open-jdk : Install open jdk 1.8
   TASK: mount-partition : Creating the filesystem for the device {{ device }}  (if needed)
   TASK: mount-partition : Mounting the device {{ device }} on path {{ path }}
   TASK: jenkins : Ensure Jenkins repo is installed.
   TASK: jenkins : Add Jenkins repo GPG key.
   TASK: jenkins : Ensure Jenkins is present.
   TASK:
jenkins : Ensures that the home directory exists

About Author

Author Image
Ankit Arora

Ankit is a Redhat Certified Engineer and Cloud Engineer.

Request for Proposal

Name is required

Comment is required

Sending message..