Understanding basics of Ansible

Inventory

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

Groups in Inventory

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

Alias

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

# 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:

hosts:

become:

become_user:

tasks:

    - 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

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

System

  - 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

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

Files

  - 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

  - 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

- 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

- 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

  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

--

--

--

Full stack developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to apply serverless in front-end cloud computing | WalkingTree Technologies

Assess, Test and Prepare for the (Un)Expected News Event

Low code development platform for enterprises

Internship experience: Improving point-of-sale with passive learning

Configuration of Apache WebServer On AWS EC2 Instances Using Ansible

Time-lapse Scatter Map-visualization using Cartopy

How to convert letters to numbers and commas to periods in Google Sheets

Know Your Google Drive

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
Rohan Aggarwal

Rohan Aggarwal

Full stack developer

More from Medium

How to install Jenkins without installing Java?(Using docker)

Forking, Cloing and Pushing on Git and Github

Jenkins: How to create a freestyle Job and Pipeline for beginners

File parameter in Jenkins Pipeline (creating file from input)