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
Recently I have started working with Ansible and Ansible is a great & powerful configuration
Below are the points you would love to know about Ansible.
-
Running and checking tasks in dry mode
This is one of the great
$ ansible-playbook --check
Note: Please note that this mode might not work well in case we are using conditional steps in
-
Running task based on their tags
For this
# 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
# 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
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
Pipelining only works if the
[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
handlers:
- name: restart
# The service module was used, but you could use whatever module you wanted
service: name=memcached state=restarted
- name: restart
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
- 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
apt: package=memcached update_cache=yes
sudo: True
- name: install
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:
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Ankit Arora
Ankit is a Redhat Certified Engineer and Cloud Engineer.