• Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial News Comments FAQ Related Articles

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

  • 00:33 cat /etc/os-release
  • 00:50 yum list ansible
  • 01:03 yum list python3
  • 01:08 pip3 list | grep pywinrm
  • 01:42 vim /etc/ansible/hosts
  • 02:10 vi test1.yml
  • 02:26 ansible-playbook test.yml --syntax-check
  • 02:52 ansible-playbook test.yml
  • 03:45 vi test1.yml
  • 04:04 ansible-playbook test.yml --syntax-check
  • 04:30 ansible-playbook test1.yml
{{postValue.id}}

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 snap 1

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 snap 2

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.

Tags:
keeljohnston
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

Q

What is inventory in Ansible?

A

Lists of managing nodes in the ansible host file are called Inventory.

Q

Where is the Ansible Inventory file Located?

A

The Ansible Inventory file is located in /etc/ansible/hosts.

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

What does "state: present" do in the Ansible playbook?

A

It creates or edits the target file or directory.

Q

What does "state: absent" do in the Ansible playbook?

A

It removes the target file or directory

Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help Sebastian ?
How to change non required to required field in SuiteCRM Custom/Default Modules

How to change not required to the required field in SuiteCRM Custom/Default Modules?

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.