Skip to content

Instantly share code, notes, and snippets.

@devops-school
Last active April 25, 2024 11:27
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save devops-school/52c8de8924555f91c50e3a016556060f to your computer and use it in GitHub Desktop.
Save devops-school/52c8de8924555f91c50e3a016556060f to your computer and use it in GitHub Desktop.
15 Ansible Playbooks Example for Windows Administration

1. Copying Files

---
- hosts: win

  tasks:
  - name: Copy File
    win_copy:
      src: C:\output.pdf
      dest: C:\ansible_examples\
      remote_src: yes

2. Install/UnInstall MSI

---
- name: Installing Apache MSI
  hosts: win

  tasks:
    - name: Download the Apache installer
      win_get_url:
        url: https://archive.apache.org/dist/httpd/binaries/win32/httpd-2.2.25-win32-x86-no_ssl.msi
        dest: C:\ansible_examples\httpd-2.2.25-win32-x86-no_ssl.msi

    - name: Install MSI
      win_package:
        path: C:\ansible_examples\httpd-2.2.25-win32-x86-no_ssl.msi
        state: present

install applications using MSI with arguments

---
- name: Installing Apache MSI
  hosts: win
  tasks:
    - name: Download the Apache installer
      win_get_url:
        url: https://archive.apache.org/dist/httpd/binaries/win32/httpd-2.2.25-win32-x86-no_ssl.msi
        dest: C:\ansible_examples\httpd-2.2.25-win32-x86-no_ssl.msi

    - name: Install MSI
      win_package:
        path: C:\ansible_examples\httpd-2.2.25-win32-x86-no_ssl.msi
        arguments:
          - /install
          - /passive
          - /norestart

** Uninstalling Apache.**

---
- name: UnInstalling Apache MSI
  hosts: win
  tasks:
    - name: UnInstall MSI
      win_package:
        path: C:\ansible_examples\httpd-2.2.25-win32-x86-no_ssl.msi
        state: absent

3. Uninstall Software (.EXE)

---
- hosts: win
  tasks:
   - name: Uninstall 7-Zip from the exe
     win_package:
       path: C:\Program Files\7-Zip\Uninstall.exe
       product_id: 7-Zip
       arguments: /S
       state: absent

4. Stop/Start/Restart Windows Services

---
- hosts: win
  tasks:
   - name: Stop service Tomcat
     win_service:
       name: Tomcat8
       state: stopped

5. Gathering Facts

---
- hosts: win
  tasks:
  - name: Get disk facts
    win_disk_facts:

  - name: Output first disk size
    debug:
      var: ansible_facts.disks[0].size

  - name: Convert first system disk into various formats
    debug:
      msg: '{{ disksize_gib }} vs {{ disksize_gib_human }}'
    vars:
      # Get first system disk
      disk: '{{ ansible_facts.disks|selectattr("system_disk")|first }}'

      # Show disk size in Gibibytes
      disksize_gib_human: '{{ disk.size|filesizeformat(true) }}'
      disksize_gib: '{{ (disk.size/1024|pow(3))|round|int }} GiB'

```yaml
---
- hosts: win
  tasks:
   - name: Get disk facts
     win_command: wmic cpu get caption, deviceid, name, numberofcores, maxclockspeed, status
     register: usage

   - debug: msg="{{ usage.stdout }}"

6. Running Commands

---

- hosts: win

  tasks:
   - name: run an executable using win_command
     win_command: whoami.exe
   - name: run a cmd command
     win_command: cmd.exe /c mkdir C:\test

7. Environment Variables

---
- hosts: win
  tasks:
   - name: Set an environment variable for all users
     win_environment:
       state: present
       name: NewVariable
       value: New Value
       level: machine

8. Add/Edit Registry

---

- hosts: win
  tasks:
   - name: Creating a registry
     win_regedit:
      path: HKLM:\SOFTWARE\GeekFlare
   - name: Modifying a registry, adding name and data
     win_regedit:
      path: HKLM:\SOFTWARE\GeekFlare
      name: Geek
      data: Flare

9. Delete Log

---
- hosts: win
  tasks:
   - name: Remove Internet Explorer Logs
     win_eventlog:
      name: Internet Explorer
      state: absent

10. Install

---
- 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_package: 
        path: 'C:\Users\Administrator\Downloads\httpd-2.2.25-win32-x86-no_ssl.msi'
        state: present
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment