Learn › Ansible

Ansible Inventories, group_vars and Variables for Windows Servers

Organise many Windows servers in Ansible: groups and child groups, group_vars and host_vars, variable precedence, and separate lab and production inventories.

Intermediate 11 min read
By Gopinath Karthikeyan

In Part 3 one server lived in one file. Real estates have hundreds of servers in many roles and environments. A well-organised inventory makes the difference between “run this on the web servers in lab” being one command – or an afternoon of editing.

Ansible control node with inventory, group_vars and playbook managing Windows and Linux hosts Control node (Linux) Control node (Linux) Inventory Hosts in groups Inventory Groups-ல் hosts group_vars Settings per group group_vars Group-க்கான settings Playbook Tasks in order, using idempotent modules Playbook வரிசையான tasks, idempotent modules WinRM or SSH WinRMஅல்லது SSH SSH SSH Windows servers win_* modules Windows servers win_* modules Linux servers SSH + Python Linux servers SSH + Python Recap: ok · changed · failed Per host, after every run Recap: ok · changed · failed ஒவ்வொரு run-க்கும், host வாரியாக
The control node reads the inventory and variables, then runs playbook tasks on each host – no agent needed on the servers.Control node inventory, variables-ஐ படித்து, ஒவ்வொரு host-லும் playbook tasks-ஐ இயக்குகிறது – servers-ல் agent தேவையில்லை.

Groups and child groups

inventories/lab/hosts.yml:

all:
  children:
    windows:
      children:
        web:
          hosts:
            WEB01: { ansible_host: 10.0.1.11 }
            WEB02: { ansible_host: 10.0.1.12 }
        app:
          hosts:
            APP01: { ansible_host: 10.0.1.21 }
        file:
          hosts:
            FS01:  { ansible_host: 10.0.1.31 }
    linux:
      hosts:
        MON01: { ansible_host: 10.0.1.41 }

A server can be in several groups (e.g. web and iis and site_east). Group by role, environment and location – whatever you’ll want to target.

See the structure:

ansible-inventory -i inventories/lab --graph

group_vars and host_vars

Keep variables out of playbooks and next to the inventory:

inventories/lab/
├── hosts.yml
├── group_vars/
│   ├── all.yml          # everything
│   ├── windows.yml      # connection settings for Windows
│   └── web.yml          # IIS settings for web servers
└── host_vars/
    └── WEB02.yml        # one-off exceptions

group_vars/windows.yml:

ansible_connection: ssh
ansible_shell_type: powershell
ansible_user: svc_ansible

group_vars/web.yml:

site_root: D:\Sites
app_pool_recycle_time: "03:00:00"

host_vars/WEB02.yml:

app_pool_recycle_time: "04:00:00"     # this server recycles an hour later

Variable precedence (simplified)

When the same variable is set in several places, the more specific one wins. From weakest to strongest, the ones you’ll use most:

  1. Role defaults (roles/x/defaults/main.yml)
  2. group_vars/all
  3. group_vars/<group> (child groups beat parent groups)
  4. host_vars/<host>
  5. Variables in the play (vars:)
  6. Extra vars on the command line (-e) – always win

Keep it simple: defaults in roles, environment values in group_vars, rare exceptions in host_vars, and -e only for one-off run options like target.

Separate lab and production

inventories/
├── lab/
└── prod/

Same playbooks, different inventories:

ansible-playbook -i inventories/lab  playbooks/iis-config.yml
ansible-playbook -i inventories/prod playbooks/iis-config.yml --limit WEB01 --check

Production inventories may hold real hostnames and IPs – keep them in a restricted repo or generate them dynamically, and never publish them.

Target precisely with –limit

ansible-playbook site.yml --limit web             # one group
ansible-playbook site.yml --limit 'web:&site_east' # web servers that are also in site_east
ansible-playbook site.yml --limit 'windows:!FS01'  # all Windows except FS01
ansible-playbook site.yml --limit WEB01 --list-hosts   # check before running!

--list-hosts shows exactly which servers will be touched. Make it a habit.

Dynamic inventories

For cloud or large estates, plugins build the inventory from the source of truth – e.g. amazon.aws.aws_ec2 or azure.azcollection.azure_rm – grouping servers by tags. Same playbooks; the server list stays current automatically.

FAQ

hosts.ini or hosts.yml? Both work. YAML is easier to read once you have nested groups and variables.

What’s next

Next: Ansible roles – reusable building blocks.

Practise this topic

Test yourself with free questions on this topic – instant answers and explanations.

Start practice questions

AnsibleInventoryVariablesWindows ServerIntermediate

Gopinath Karthikeyan

Gopinath Karthikeyan – Windows Site Reliability Engineer with about 12 years in enterprise Windows Server infrastructure, and founder of WinOps Academy. YouTube · LinkedIn

More in Ansible