Ansible Windows Playbook Example - 1

Content of create-user.yml


---
- name: Add a user
  hosts: all
  gather_facts: false
  tasks:
    - name: Add User
      win_user:
        name: ansible
        password: "@ns1bl3"
        state: present

Ansible Windows Playbook Example - 2

Content of ping.yml


---
# This playbook uses the win_ping module to test connectivity to Windows hosts
- name: Ping 
  hosts: all 

  tasks:
  - name: ping
    win_ping:

Ansible Windows Playbook Example - 3

Content of run-powershell.yml


---
# This playbook tests the script module on Windows hosts

- name: Run powershell script
  hosts: all 
  gather_facts: false
  tasks:
    - name: Run powershell script
      script: files/helloworld.ps1
	  
# Content of helloworld.ps1
# Filename: helloworld.ps1
Write-Host
Write-Host 'Hello World!'
Write-Host "Good-bye World! `n"
# end of script

Ansible Windows Playbook Example - 4

Content of install-msi.yml


---
- name: Install Apache from an MSI 
  hosts: all 
 
  tasks:
    - name: Download the Apache installer
      win_get_url:
        url: 'http://mirror.cc.columbia.edu/pub/software/apache//httpd/binaries/win32/httpd-2.2.25-win32-x86-no_ssl.msi'
        dest: 'C:\Users\Administrator\Downloads\httpd-2.2.25-win32-x86-no_ssl.msi'

    - name: Install MSI
      win_msi: 
        path: 'C:\Users\Administrator\Downloads\httpd-2.2.25-win32-x86-no_ssl.msi'
        state: present
        

Ansible Windows Playbook Example - 5

Content of enable-iis.yml


---
# This playbook installs and enables IIS on Windows hosts

- name: Install IIS
  hosts: all
  gather_facts: false
  tasks:
    - name: Install IIS
      win_feature:
        name: "Web-Server"
        state: present
        restart: yes
        include_sub_features: yes
        include_management_tools: yes

Ansible Windows Playbook Example - 6

Content of deploy-site.yml


---
# This playbook uses the win_get_url module to download a simple HTML file for IIS
- name: Download simple web site 
  hosts: all 

  gather_facts: false
  tasks:
    - name: Download simple web site to 'C:\inetpub\wwwroot\ansible.html'
      win_get_url:
        url: 'https://raw.githubusercontent.com/thisdavejohnson/mywebapp/master/index.html'
        dest: 'C:\inetpub\wwwroot\ansible.html'

Ansible Windows Playbook Example - 7

Content of test.yml


---
- name: test raw module
  hosts: all 
  tasks:
    - name: run ipconfig
      raw: ipconfig
      register: ipconfig
    - debug: var=ipconfig

- name: test stat module
  hosts: windows
  tasks:
    - name: test stat module on file
      win_stat: path="C:/Windows/win.ini"
      register: stat_file

    - debug: var=stat_file

    - name: check stat_file result
      assert:
          that:
             - "stat_file.stat.exists"
             - "not stat_file.stat.isdir"
             - "stat_file.stat.size > 0"
             - "stat_file.stat.md5"

Ansible Windows Playbook Example - 7

Content of test.yml


---
- name: test raw module
  hosts: all 
  tasks:
    - name: run ipconfig
      raw: ipconfig
      register: ipconfig
    - debug: var=ipconfig

- name: test stat module
  hosts: windows
  tasks:
    - name: test stat module on file
      win_stat: path="C:/Windows/win.ini"
      register: stat_file

    - debug: var=stat_file

    - name: check stat_file result
      assert:
          that:
             - "stat_file.stat.exists"
             - "not stat_file.stat.isdir"
             - "stat_file.stat.size > 0"
             - "stat_file.stat.md5"