Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 29 ยท Everyday Modules

Windows, Network Devices and Cloud Modules

Automate Windows over WinRM or SSH, network devices with network_cli and resource modules, and cloud APIs from localhost. Learn connection types and collections.

So far every example assumed a Linux host reached over SSH. Ansible can manage much more: Windows servers, switches and routers, and cloud services that have no operating system you can log in to at all. The playbook syntax stays the same. What changes is how Ansible connects, which collection supplies the modules, and where the work really runs.

This lesson explains connection types, Windows automation with win_* modules, network automation with network_cli and resource modules, and cloud modules that run on the control node. These setups need real Windows hosts, devices or cloud accounts, so their code is read-only. Two JavaScript stand-ins show the logic: a table that maps host variables to a connection plugin, and a resource module that turns a desired list into configuration commands.

Connection types

A connection plugin decides how Ansible reaches a host and runs a module. You choose it per host or group with ansible_connection. The ones you will meet most:

Connection Used for Typical settings
ssh (default) Linux and Unix hosts, also Windows with OpenSSH ansible_user, ansible_port
local The control node itself, no SSH ansible_connection=local
winrm Windows over WinRM ansible_winrm_transport, port 5985 or 5986
psrp Windows over PowerShell Remoting needs the pypsrp library
ansible.netcommon.network_cli Switches and routers with a CLI ansible_network_os
ansible.netcommon.httpapi Devices with an HTTP API ansible_network_os, ansible_httpapi_use_ssl
ansible.netcommon.netconf Devices speaking NETCONF ansible_network_os
community.docker.docker Containers, without SSH inside them the container name is the host

The stand-in below reads a host's variables and says which plugin it would use and what extra thing must be present. It is a teaching table, not the real plugin loader.

JS Which connection plugin?

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Managing Windows

Ansible's control node must run on Linux or macOS (on Windows, use WSL). Windows machines are managed nodes only. There are two ways to reach them.

WinRM is the traditional route. Enable the WinRM service on the host, install pywinrm on the control node, and set the connection variables:

INI
[windows]
win1.example.com

[windows:vars]
ansible_connection=winrm
ansible_winrm_transport=ntlm
ansible_port=5986
ansible_winrm_server_cert_validation=validate
ansible_user=Administrator
ansible_password={{ vault_windows_password }}

Port 5985 is plain HTTP and 5986 is HTTPS. Use HTTPS with real certificate validation outside a lab. The transport is the authentication method: ntlm and kerberos are common for servers, credssp is needed for some double-hop cases, and basic should be avoided on anything but a lab. Keep the password in Ansible Vault.

OpenSSH is the newer route: current Windows Server versions can run an OpenSSH server, and then Ansible can use ansible_connection=ssh with ansible_shell_type=powershell (or cmd). You then use the same key-based habits as on Linux.

Windows modules start with win_ and live mainly in the ansible.windows collection, with more in community.windows. Modules from the Linux world do not work, because they assume Python and POSIX tools on the target. Windows modules are PowerShell scripts that Ansible sends and runs.

YAML
- name: Configure a Windows web server
  hosts: windows
  tasks:
    - name: Check the connection
      ansible.windows.win_ping:

    - name: Install the IIS role
      ansible.windows.win_feature:
        name: Web-Server
        state: present
        include_management_tools: true
      register: iis

    - name: Make sure a folder exists
      ansible.windows.win_file:
        path: C:\inetpub\myapp
        state: directory

    - name: Copy the site config
      ansible.windows.win_copy:
        src: files/web.config
        dest: C:\inetpub\myapp\web.config

    - name: Keep the IIS service running at startup
      ansible.windows.win_service:
        name: W3SVC
        state: started
        start_mode: auto

    - name: Reboot when the feature install asked for it
      ansible.windows.win_reboot:
      when: iis.reboot_required

Three habits help. First, write Windows paths in single quotes or use {{ ... }} carefully, because a backslash inside a double-quoted YAML string is an escape character. Second, prefer a specific module (win_feature, win_service, win_regedit) over win_shell, for the same idempotence reasons as on Linux. Third, packages come from ansible.windows.win_package for installers and, with the chocolatey.chocolatey collection, from win_chocolatey.

Managing network devices

Switches and routers usually have no Python and no shell you can run Ansible modules in. So for network gear, Ansible does the work on the control node: it logs in to the device's CLI (or API), sends commands, and reads the replies. The connection is ansible.netcommon.network_cli, and ansible_network_os tells Ansible which vendor language to speak.

INI
[switches]
sw1.example.com
sw2.example.com

[switches:vars]
ansible_connection=ansible.netcommon.network_cli
ansible_network_os=cisco.ios.ios
ansible_user=admin
ansible_password={{ vault_switch_password }}
ansible_become=true
ansible_become_method=enable

Here become_method: enable is the network version of sudo: it enters the device's privileged mode. Each vendor has a collection (cisco.ios, arista.eos, junipernetworks.junos and many more), and each collection has two styles of module.

Command-style modules, such as cisco.ios.ios_command and ios_config, send text:

YAML
- name: Read the version
  cisco.ios.ios_command:
    commands: show version
  register: version

- name: Set a banner with raw config lines
  cisco.ios.ios_config:
    lines:
      - banner motd ^Managed by Ansible^

Resource modules describe a piece of configuration as data, such as ios_vlans, ios_interfaces or ios_l3_interfaces, and compare it with the device. This is the same "desired state" idea as everything else in Ansible, and it is the preferred style:

YAML
- name: Make sure these VLANs exist
  cisco.ios.ios_vlans:
    config:
      - vlan_id: 10
        name: users
      - vlan_id: 20
        name: servers
    state: merged

The state values have a shared meaning across resource modules: merged adds and updates what you list and leaves the rest alone, replaced makes each listed item match exactly, overridden makes the whole device section match your list (removing everything else), deleted removes what you list, and gathered, rendered and parsed read, preview or parse configuration without changing the device. Use overridden and deleted with care, and run them with --check --diff first.

This stand-in shows the difference for a list of VLANs. It is a simplified model (real modules work with many more fields per item), but the four states behave as described above.

JS Merged, replaced, overridden, deleted

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice that merged keeps VLAN 30, overridden removes it, and a second merged run changes nothing.

Cloud modules and localhost

A cloud API is not a server you log in to, so there is nothing to connect to over SSH. The module runs on the control node (or on a runner such as AWX) and calls the provider's API with a library like boto3. In a playbook you target localhost with the local connection:

YAML
- name: Create cloud resources
  hosts: localhost
  connection: local
  gather_facts: false
  tasks:
    - name: Launch an EC2 instance
      amazon.aws.ec2_instance:
        name: web-1
        instance_type: t3.micro
        image_id: ami-0123456789abcdef0
        region: eu-central-1
        state: running
        wait: true
      register: web

    - name: Show the new address
      ansible.builtin.debug:
        msg: "{{ web.instances[0].public_ip_address | default('no public IP') }}"

Cloud modules ship in collections: amazon.aws, azure.azcollection, google.cloud, community.vmware, community.digitalocean and others (see Collections, Galaxy and Reusable Content). Credentials come from environment variables or the provider's own profile files, not from your playbook text. The same idea works for any web API with the uri module and delegate_to: localhost:

YAML
- name: Tell the chat room we deployed
  ansible.builtin.uri:
    url: https://chat.example.com/hooks/deploy
    method: POST
    body_format: json
    body: { text: "Release 2.4.0 is live" }
  delegate_to: localhost
  run_once: true

When you create servers in the cloud and then configure them, you often use a dynamic inventory so the new machines appear in your groups automatically (covered in Performance, Dynamic Inventory and AWX).

flowchart LR
  C["Control node"] -- "ssh" --> L["Linux host<br/>runs a Python module"]
  C -- "winrm or ssh" --> W["Windows host<br/>runs a PowerShell module"]
  C -- "network_cli" --> N["Switch or router<br/>CLI only"]
  C -- "HTTPS API call<br/>module runs on the control node" --> A["Cloud API"]

The connection plugin decides where the module code runs and how it reaches the target.

What is real and what is the stand-in

The commands, inventory files and playbooks above are real Ansible, shown read-only because the site cannot reach a Windows host, a switch or a cloud account. The two JavaScript programs are teaching models: the first only prints a table, and the second only imitates the merge, replace, override and delete rules on a tiny VLAN list. They do not speak WinRM, SSH or any vendor's CLI.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which variable tells Ansible to reach a Windows host with WinRM?

    ansible_connection selects the connection plugin. WinRM also needs the pywinrm library on the control node.
  2. 2. Why do network device modules run on the control node instead of the device?

    network_cli sends CLI commands and parses the replies from the control node, because the device offers no place to run a Python module.
  3. 3. A resource module is run with state: overridden. What happens to items on the device that are not in your list?

    overridden makes the whole configuration section match your data. merged is the state that leaves extra items alone.
  4. 4. Where does amazon.aws.ec2_instance normally run?

    A cloud module talks to the provider's API, so you target localhost with the local connection. There is no server to log in to yet.

Summary

  • The connection plugin (ansible_connection) controls how Ansible reaches a target; ssh is the default.
  • Windows hosts are managed over WinRM (port 5985 or 5986) or OpenSSH, use win_* modules from ansible.windows, and need quoting care for backslash paths.
  • Network devices use network_cli, ansible_network_os, become_method: enable and vendor collections; resource modules describe desired data with merged, replaced, overridden and deleted.
  • Cloud and API modules run on the control node against localhost; credentials come from the environment or provider profiles.
  • Prefer specific modules to win_shell and ios_command, and test risky states with --check --diff.

Next: Ansible Vault and Secrets.