1. What is Ansible ?
Ansible is an IT automation tool. It can configure
systems, deploy software, and orchestrate more
advanced IT tasks such as continuous deployments or
zero downtime rolling updates.
Ansible Architecture
Install Ansible
To install Ansible in Debian based Linux, you can follow
the following steps:
#Add Ansible repository
$ sudo apt-add-repository ppa:ansible/ansible
#Run the update command
$ sudo apt-get update
#Install Ansible package
$ sudo apt-get install ansible
Control Node and Manage Node Configuration
Manage Node configuration
For Ansible to communicate to a Windows host and use Windows
modules, the Windows host must meet the following requirements:
•Ansible’s supported Windows versions generally match those under
current and extended support from Microsoft. Supported desktop OSs
include Windows 7, 8.1, and 10, and supported server OSs are Windows
Server 2008, 2008 R2, 2012, 2012 R2, 2016, and 2019.
•Ansible requires PowerShell 3.0 or newer and at least .NET 4.0 to be
installed on the Windows host.
•A WinRM listener should be created and activated. More details for this
can be found below.
# Download the below power shell script.
ConfigureRemotingForAnsible.ps1
#Run the power shell script
$ ./ConfigureremotingForAnsible.ps1
# Ensure the Power shell remote login is enabled or not
$ Enable-PSRemoting -Force
# Get version
$ ansible --version
Control Node configuration
For Ansible to communicate to a Windows host and use Windows
modules, the Control host must meet the following requirements:
• A WinRM must be installed and activated .
• pywinrm with support for basic, certificate, and NTLM auth, simply
activated.
#To install pywinrm with support for basic, certificate, and NTLM auth, simply
$ Pip install pywinrm
# for Debian/Ubuntu/etc:
$ sudo apt-get install python-dev libkrb5-dev
$ pip install pywinrm
# for RHEL/CentOS/etc:
$ sudo yum install gcc krb5-devel krb5-workstation
$ pip install pywinrm
Ansible Control Node to communicate with Windows nodes must meet the below requirements
Configuration in Hosts file
Inventory Files & Hosts Pattern
The inventory file of Ansible lists all the platforms you want to automate across. Ansible at
a single instance can work on multiple hosts in the infrastructure. It is also possible to have
multiple inventory files at the same time.
•The host inventory file can contain hostnames either individually or in groups
•Host Groups can be created by giving a group name within square brackets
•Group members can then be listed under, till there is a line break
#Set up hosts by editing the hosts' file in the Ansible
directory
$ sudo vim /etc/ansible/hosts
#To check the connection to hosts #Change your
directory to /etc/Ansible
$ cd /etc/ansible
#Ansible’s win_ping module allows you to check
whether Ansible is connecting to hosts
$ ansible –m win_ping <hosts>
#To check on servers individually
$ ansible –m win_ping server name
#To check a particular server group
$ ansible -m win_ping servergroupname
Follow the below steps to set hosts and
then check their connection:
Set & Check win Hosts Connection Inventory Example
Example inventory file, which you can refer to
understand the various parameters.
Ad-hoc Commands
Ad-Hoc commands are quick commands which are used to perform the actions, that won’t be saved for later.
Frequently used commands
# To check ping status.
$ ansible [host_group_name_in_inventory_file] -i hosts -m win_ping
# Gathering Hostnames
$ ansible [host_group_name_in_inventory_file] -i hosts -m setup | grep
ansible_Hostname
# Start the service
$ ansible [host_group_name_in_inventory_file] -m win_service –a
"name=ser _name“
# check specific file is exist or not
$ ansible [host_group_name] -m win_file –a “path=c:filename” ,
“state=touch”
Service commands
# Start the service in all the servers
$ ansible [host_group] -m win_service –a "name=ser _name
state=started“
# Retart the service in the servers
$ ansible [host_group] -m win_service –a "name=ser _name
state=restarted“
# Stop the service in servers
$ ansible [host_group] -m win_service –a "name=ser _name
state=stopped“
# Pause the service in servers
$ ansible [host_group] -m win_service –a "name=ser _name
state=pause“
File commands
# Create a Directory
$ ansible [host_group] -i hosts -m win_file –a “path=c:directory name
state=directory”
# deleting a directory
$ ansible [host_group] -i hosts -m win_file –a “path=c:directory name
state=absent”
# download a file or image using url
$ ansible [host_group] -m win_get_url -a
"url=https://www.somewebsite.com/ansible.jpg dest=c:fileansible.jpg"
Updating Windows
# Install only security updates as a scheduled task for Server
win_updates:
category_names: SecurityUpdates
use_scheduled_task: yes
# Install only particular updates based on the KB numbers
win_updates:
category_name: - SecurityUpdates
whitelist:
- KB*******
- KB*******
Sample play book
---
- name: Getting started First play book
hosts: win
tasks:
- name: Create directory structure
win_file:
path: C:Tempshellscripts
state: directory
- name: copy a shell script
win_copy:
src: /etc/ansible/localuser.ps1
dest: c:Tempshellscripts
- name: run the powershell script
win_shell: c:Tempshellscriptslocaluser.ps1 >> c:script.txt
- name: create a user using ansible module
win_user:
name: Level3
password: Pass@123
state: present
groups:
- administrators
- users
- name: DNS updation
win_dns_client:
adapter_names: "*"
ipv4_addresses:
- 192.168.3.16
- 192.168.3.19
log_path: c:dna_log.txt
Writing & Running play book
# writing a play book
$ vim play_book name.yaml
(or)
$ vim play_book.yml
# Run a play book
$ ansible-playbook –m Module name –a arguments –i inventory
# Run a play book interactively
$ ansible –playbook_name –step
# Run a play book at a particular task
$ ansible-playbook playbook-name.yml --start-at-task=“task_name”
Ansible windows cheat sheet
Anil Kumar .K
Note: please download the ConfigureRemotingforansible.ps1 from https://raw.githubusercontent.com/ANILVM/powershell/master/ConfigureRemotingForAnsible.ps1