• 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

  • 03:02 lsb_release -a
  • 04:06 apt update
  • 04:18 apt install ansible
  • 05:26 pip install pywinrm
  • 09:18 ansible windows -m win_ping
  • 11:55 ansible-playbook test.yml --syntax-check
  • 12:23 ansible-playbook test.yml
{{postValue.id}}

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

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

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

Tags:
landon
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 ansible host file is called as Inventory.

Q

Where is the Ansible Inventory file Located?

A

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" does in the Ansible playbook?

A

It creates or edits the target file or directory.

Q

What does "state: absent" does 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 Gibbson ?
How do i run both nginx and apache in same instance on centos

Hi...,

my server is based centos operating system and my webserver is already running on Apache.... i need to run both apache and nginx on same instance ... please help me to implement this concept...

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.