AMP AMP

How to edit Windows host file using Ansible playbook on Oracle Linux 8.5

To Edit Windows Host File Using Ansible Playbook On Oracle Linux 8.5

Introduction:

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

Procedure:

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 below command

[root@linuxhelp ~]# cat /etc/os-release
NAME="Oracle Linux Server"
VERSION="8.5"
ID="ol"
ID_LIKE="fedora"

Step 2: Check the availability of Ansible package, python3-pip package by using the below command

[root@linuxhelp ~]# yum list ansible
ansible.noarch                        2.9.27-1.el8                         @ol8_developer_EPEL
Available Packages
ansible.src                           2.9.27-1.el8                         ol8_developer_EPEL 

[root@linuxhelp ~]# yum list python3
Last metadata expiration check: 1:28:13 ago on Sun 11 Sep 2022 03:22:41 PM IST.
Available Packages
python3.src                         3.6.8-45.0.1.el8                         ol8_baseos_latest
python3.src                         3.6.8-45.0.1.el8                         ol8_appstream    

[root@linuxhelp ~]# pip3 list | grep pywinrm
pywinrm (0.4.3)

Step 3: Create inventory for Windows node system by using the below command

[root@linuxhelp ~]# vim /etc/ansible/hosts
[windows]
192.168.6.104

[windows:vars]
ansible_user=Admin
ansible_password=Linuxc#4
ansible_port=5986
ansible_connection=winrm
ansible_winrm_server_cert_validation=ignore

Step 4: Create playbook for adding a host in windows host file by using the below command

root@linuxhelp:~# vi test1.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 5: Check the syntax of the test.yml ansible playbook by using the below command

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

Step6: Run the test.yml playbook by using the below command

[root@linuxhelp ~]# ansible-playbook test.yml 

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

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

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

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

Step 7: Check in Windows Host file for the added Host entries as shown in the below image

Step 8: Create playbook for removing a host in windows host file by using the below image

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 9: Check the syntax of the test.yml ansible playbook by using the below command

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

Step 10: Running the test1.yml playbook by using the below 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 11: Check the Windows Host file for the Removed Host entries as shown in the below image

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to Edit Windows Host file using Ansible Playbook on Oracle Linux 8.5. Your feedback is much welcome.

FAQ
Q
What does "state: absent" do in the Ansible playbook?
A
It removes the target file or directory
Q
What does "state: present" do 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
The Ansible Inventory file is located in /etc/ansible/hosts.
Q
What is inventory in Ansible?
A
Lists of managing nodes in the ansible host file are called Inventory.