Understanding basics of Ansible

Inventory

Ansible can work with one or multiple systems in your infrastructure. To work with these systems/servers, it needs to create a connection with these servers. This is done using SSH for Linux and power shell remoting for windows that makes Ansible agentless. It means we don't need to install any additional software on the target machine.

server1.company.com
server2.company.com
192.168.1.201
192.168.1.202

Groups in Inventory

We can create different groups of servers inside the Inventory like

[database]
192.168.1.201
192.168.1.202
[node]
192.168.1.201
[master]
92.168.1.202

Alias

We can define the alias of the servers to make it more readable using “ansible_host”.

db ansible_host=192.168.1.202
node ansible_host=192.168.1.201
ansible_connection - ssh/winrm/localhost
ansible_port - 22/5986
ansible_user - root/administrator
ansible_ssh_pass - Password
db ansible_host=192.168.1.202 ansible_connection=ssh
node ansible_host=192.168.1.201 ansible_connection=winrm
db ansible_host=192.168.1.202 ansible_connection=ssh  ansible_port=9090
node ansible_host=192.168.1.201 ansible_connection=winrm
db ansible_host=192.168.1.202 ansible_connection=ssh  ansible_port=9090
node ansible_host=192.168.1.201 ansible_connection=winrm ansible_user="xyz"
db ansible_host=192.168.1.202 ansible_connection=ssh  ansible_port=9090
node ansible_host=192.168.1.201 ansible_connection=winrm ansible_user="xyz" ansible_ssh_pass=TR@57!
[database]
192.168.1.201
192.168.1.202
[node]
192.168.1.201
[master]
92.168.1.202

[code:children]
database
node

Ansible Playbook

Ansible Playbooks are Ansible orchestration language. Here, we define what we want to do with the servers. We can define a different variety of commands that include copying some stuff on the servers, installing some software on them to deploying virtual machines on servers. Ansible playbook gives a variety of commands to work with.

# Play 1  
- name: update web servers
hosts: webservers
become: yes
become_user: root

tasks:
- name: ensure apache is at the latest version
yum:
name: httpd
state: latest
- name: write the apache config file
template:
src: /srv/httpd.j2
dest: /etc/httpd.conf
# Play 2
- name: update db servers
hosts: databases
remote_user: root
tasks:
- name: ensure postgresql is at the latest version
yum:
name: postgresql
state: latest
- name: ensure that postgresql is started
service:
name: postgresql
state: started

name:

Name of the play, a single ansible-playbook can have multiple plays and each must have a unique name.

hosts:

Name of host group defined in an Inventory file

become:

Executes the play using a user with higher privileges

become_user:

Changes the user to the given user while executing the tasks.

tasks:

Set of tasks to perform on the defined hosts.

    - name: Install the httpd apps
yum: name=httpd

- name: Deploy configuration File
template: src=templates/index.j2 dest=/var/www/html/index.html

- name: start the httpd service
service: name=httpd state=started

- name: Install common software requirements
yum: pkg={{ item }} state=installed
with_items:
- git
- ntp
- vim

Modules

A module is a reusable, standalone script that Ansible runs on your behalf, either locally or remotely. Modules interact with your local machine, an API, or a remote system to perform specific tasks like changing a database password or spinning up a cloud instance.

ansible group1 -m ping -i inventories.txt
  - name: Test connectivity
hosts: all
tasks:
- name: Ping test
ping:

System

It is used to modify the system like modifying the user, IP tables, firewall settings, etc.

  - name: System module category Example
hosts: all
tasks:
- name: Add the user 'johnd' with a specific uid and a primary group of 'admin'
user:
name: johnd
comment: John Doe
uid: 1040
group: admin

Commands

It is used to run a command or scripts on host servers

  - name: Command module category example
hosts: all
tasks:
- name: Execute the UNAME command
register: unameout
command: "uname -a"

Files

It is used to work with files like copying a file in all the defined servers, searching a file in all the defined servers, etc.

  - name: Files module category example
hosts: all
tasks:
- name: Change file ownership, group and permissions
ansible.builtin.file:
path: /etc/foo.conf
owner: foo
group: foo
mode: '0644'

Database

Used to work with databases like MySQL, PostgreSQL, MongoDB, etc.

  - name: Database module category example
hosts: all
tasks:
- mssql_db:
name: jackdata
state: present
- copy:
src: dump.sql
dest: /tmp
- mssql_db:
name: my_db
state: import
target: /tmp/dump.sql

Cloud

Used to work with cloud service providers like AWS, Azure, Docker, etc.

- name: Cloud module category example
hosts: all
tasks:
- name: Create a new direct connect gateway attached to virtual private gateway
dxgw:
state: present
name: my-dx-gateway
amazon_asn: 7224
virtual_gateway_id: vpg-12345
register: created_dxgw

Windows

Helps to use ansible in Windows operating system, it includes modules like Win_copy, Win_command, Win_domain, Win_use, etc.

- name: Windows module category example
hosts: all
tasks:
- name: Download the 7-Zip package
win_get_url:
url: https://www.7-zip.org/a/7z1701-x64.msi
dest: C:\temp\7z.msi
ansible-doc -l

Running ansible-playbook

There are two ways to run an Ansible playbook

  1. Using ansible command
  2. Using ansible-playbook command
ansible <host-group> -a <command> -i <inventory file path with name>

ansible <host-group> -m <module> -i <inventory file path with name>
ansible group1 -a "/sbin/reboot" -i inventory.txtansible group1 -m ping -i inventories/inventory1.txt
ansible 192.168.1.201 -a "/sbin/reboot"
ansible-playbook <anisble playbook name> -i <inventory-file>
  - name: Test connectivity
hosts: all
tasks:
- name: Ping test
ping:
node ansible_host=192.168.1.201
target ansible_host=192.168.100.21
ansible-playbook Sample-playbook.yaml -i inventory.txt

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store