Ansible: Run a Playboook for the first time – Targets are Windows Server

This post shows the procedures to run a simple playbook for Windows Server targets on Ansible.

Preparations

On Targets (Windows Server)

Set up the followings as the necessary bare minimum of settings on Windows Server targets.

  • Set a Administrator’s password.
  • Set a IP address to be used by Ansible.

On the Ansible Controller

On the Ansible Controller, configure directory structure and define variables to connect to Windows Server targets.
For those, refer to the following post.
>> Recommended directory structure and using variables of Ansible

Playbook sample

test.yml

---
- name: Test Playbook
  hosts: 192.168.1.1
  
  tasks:
  - name: Install Windows Feature
    win_feature:
      name: SNMP-Service
      state: present

Comments for playbook

---

The 1st line is a declaration of YAML file.

- name: Test Playbook

Define the name of the playbook の名前を定義します。

  hosts: 192.168.1.1

Specify targets to be run the playbook.
Manners of specification are the following:

  • For some targets.
    hosts: 192.168.1.1,192.168.1.2
    or
    hosts: 192.168.1.1:192.168.1.2
  • For using wild-card.
    hosts: 192.168.1.*
  • For using group name.
    hosts: group01
  tasks:

After this declaration, write tasks in the order of execution.
In this example, one task is run.

  - name: Install Windows Feature

Specify the name of the task.

    win_feature:
      name: SNMP-Service
      state: present

Make targets installed (present) SNMP-Service by the win_feature module that install or uninstall Windows Server roles and features.
The win_feature’s name is Name that is gotten by Get-WindowsFeature cmdlet.

Run the Playbook

Run the playbook.

ansible-playbook test.yml

If SNMP-Service is not installed, the result of execution is the following.

PLAY [Test Playbook] *****************************************************************************

TASK [Install Windows Feature] *******************************************************************
changed: [192.168.1.1]

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

changed=1 because the state is changed from not-installed to installed.

If SNMP-Service is already installed, the result of execution is the following.

PLAY [Test Playbook] *****************************************************************************

TASK [Install Windows Feature] *******************************************************************
ok: [192.168.1.1]

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

changed=0 because the state is not changed.

That’s about it.