How to edit Windows host file using Ansible playbook

To Edit Windows Host File Using Ansible playbook

Introduction:

Ansible is an IT automation tool that manages both Unix-like and Windows-based systems. It is a simple text file in which IP addresses are matched up with host names.

Master Server Requirements:

ansible

python3-pip

pywinrm (python package)

Windows Requirements:

powershell 3+

Dot net 4

Installation Procedure:

Step 1: Check the OS version by using the following command

root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 21.04
Release:	21.04
Codename:	hirsute

Step 2: Check the availability of Ansible package

root@linuxhelp:~# apt list -a ansible
Listing... Done
ansible/hirsute,hirsute,now 4.8.0-1ppa~hirsute all [installed]
ansible/hirsute,hirsute 2.10.7-1 all

Step 3: Check the availability of python3-pip package

root@linuxhelp:~# apt list -a python3-pip
Listing... Done
python3-pip/hirsute,hirsute,now 20.3.4-1ubuntu2 all [installed]

Step 4: Check the availability of python package pywinrm

root@linuxhelp:~# pip list | grep pywinrm
pywinrm                0.4.2

Step 5: Create inventory for Windows node system

root@linuxhelp:~# vi /etc/ansible/hosts 

[windows]
192.168.6.104

[windows:vars]
ansible_user=Admin
ansible_password=Admin@123
ansible_port=5986
ansible_connection=winrm
ansible_winrm_server_cert_validation=ignore

Step 6: Create playbook for adding a hosts in windows host file

root@linuxhelp:~# vi test.yml
- hosts: windows
  gather_facts: true
  tasks:
  - name: Adding 192.168.6.104 mapping for linuxhelp.com
    win_hosts:
       state: present
       canonical_name: linuxhelp.com
       ip_address: 192.168.6.104

Step 7: Check the syntax of the test.yml ansible playbook by using the following command

root@linuxhelp:~# ansible-playbook test.yml --syntax-check

playbook: test.yml

Step 8: Run the test.yml playbook by using the following command

root@linuxhelp:~# ansible-playbook test.yml 

PLAY [windows] ***********************************************************************************

TASK [Gathering Facts] ***************************************************************************
ok: [192.168.6.104]

TASK [Adding 192.168.6.104 mapping for linuxhelp.com] ********************************************
changed: [192.168.6.104]

PLAY RECAP ***************************************************************************************
192.168.6.104              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Step 9: Check in Windows Host file for the added Host entries

Step 10: Create playbook for removing a hosts in windows host file

root@linuxhelp:~# vi test1.yml
- hosts: windows
  gather_facts: true
  tasks:
  - name: Removing the 192.168.6.104 mapping for linuxhelp.com
    win_hosts:
       state: absent
       canonical_name: linuxhelp.com
       ip_address: 192.168.6.104

Step 11: Check the syntax of the test1.yml ansible playbook by using the following command

root@linuxhelp:~# ansible-playbook test1.yml --syntax-check

playbook: test1.yml

Step 12: Running the test1.yml playbook by using the following command

root@linuxhelp:~# ansible-playbook test1.yml 

PLAY [windows] ***********************************************************************************

TASK [Gathering Facts] ***************************************************************************
ok: [192.168.6.104]

TASK [Removing the 192.168.6.104 mapping for linuxhelp.com] **************************************
changed: [192.168.6.104]

PLAY RECAP ***************************************************************************************
192.168.6.104              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

Step 13: Check the Windows Host file for the Removed Host entries

With this Changing of Windows host file using Ansible comes to end

FAQ
Q
What does "state: absent" does in the Ansible playbook?
A
It removes the target file or directory
Q
What does "state: present" does in the Ansible playbook?
A
It creates or edits the target file or directory.
Q
What is a module used for Managing Windows host file entries?
A
"win_hosts:" is the module used for Managing Windows host file entries.
Q
Where is the Ansible Inventory file Located?
A
Ansible Inventory file is located in /etc/ansible/hosts.
Q
What is inventory in Ansible?
A
Lists of managing nodes in ansible host file is called as Inventory.