Setup Salt Minion in a Python3 Virtual Environment on Windows

This blog is a third in series in continuation to my previous blogs viz Ansible Playbook to automate Salt Minion deployment and Setup Salt Minion in a Python3 Virtual Environment on a Redhat or Centos. In this blog, get to know how to implement Salt Minion in python 3 virtual environment running on windows servers using Ansible 🙂

To communicate between windows manage nodes andAnsible control nodes, WinRM is used to establish connection and execute playbooks or commands using powershell. To do the required configuration, please follow the steps provided here.

If you have followed the above steps you should be able to reach windows hosts using the ansible command ansible windows -m win_ping. If the response succeeds then proceed with the below steps.

Ansible playbook to install python3.7, salt and setup salt-minion to run in virtual environment on windows

# vi: set shiftwidth=2 tabstop=2 softtabstop=-1 expandtab:
# Pre-requisites:
# - All target hosts are added to the domain
# - User with domain admin rights required to run the playbooks
# - Existing Salt minion and service will be removed
---
- name: Ansible Playbook to install Salt Minion in a Python Virtual Environment
  hosts: windows
  tasks:
  # Step 1 : Remove standard salt minion service
  - name: Remove standardsalt-minion service 
    win_service:
      name: salt-minion
      state: absent
    ignore_errors: true  

  # Step 2 : Check if salt directory exists
  - name: Check if salt directory exists
    win_stat: path='C:\salt\scripts'
    register: saltstatus
  - debug:
      var: saltstatus

  # Step 3 : Uninstall salt minion if exists
  - name: Uninstall standard salt-minion
    win_package:
      path: C:\salt\uninst.exe
      product_id: salt
      state: absent
      arugments: /S
    when: not saltstatus.stat.exists

  # Step 4 : Remove standard salt minion's salt directory
  - name: Remove c:\salt directory 
    win_file:
      path: C:\salt
      state: absent
    when: not saltstatus.stat.exists

  # Step 5 : Create directory structure to deploy salt-minion
  - name: Create directory structure
    win_file:
      path: C:\salt-deploy
      state: directory

  # Step 6 : Copy python 3.7, salt repo and virtual env files
  - name: Copy python installer, salt repo and virtual env files
    win_copy:
      src: /salt-deploy/files/win
      dest: C:\salt-deploy\

  # Step 7 : Check if python already installed by this playbook previously
  - name: Check to see if Python is installed
    win_stat: path='C:\python37'
    register: pythonstatus
  - debug: 
      var: pythonstatus

  # Step 8 : Install python silently using the options provided in the 
  # unttend.xml file. Python is installed to c:\python37 directory
  - name: Install python 
    raw: 'C:\salt-deploy\win\python-3.7.6-amd64.exe unattend.xml /quiet'
    when: not pythonstatus.stat.exists
    register: pythonsetup
  - debug:
      var: pythonsetup
  
  
  # Step 9 : Extract salt from the zip archive to c:\salt directory
  - name: Extract salt to c:\salt
    win_unzip:
      src: 'C:\salt-deploy\win\salt.zip'
      dest: 'C:\'
    when: not saltstatus.stat.exists
  
 
  # Step 10 : Check if python virtual environment exists
  - name: Check salt-minion Venv exists
    win_stat: path='C:\min_venv'
    register: minionstatus
  - debug:
      var: minionstatus

  # Step 11 : Extract python virtual environment to c:\min_venv directory
  - name: Setup salt-minion Venv
    win_unzip:
      src: 'C:\salt-deploy\win\min_venv.zip'
      dest: 'C:\'
    when: not minionstatus.stat.exists
  
  # Step 12 : Update the hosts file with salt master ip address and hostname
  - name: Add salt-master IP and Hostname to hosts files 
    win_lineinfile:
      path: C:\Windows\System32\drivers\etc\hosts
      regex: '# 127\.0\.0\.1'
      insertafter: '^127\.0\.0\.1'
      line: 10.100.249.87   salt

  # Step 13 : Check if Salt Minion Service is already installed
  - name: Check Salt Minion service installed
    win_service:
      name: Minion-Venv
    register: service_info
  - debug:
      var: service_info
    
  # Step 14 : Install Salt Minion as Service from start.bat batch file
  # using nssm utility https://nssm.cc/ if not installed already
  - name: Install Minion Service using NSSM
    raw: 'C:\salt-deploy\win\nssm.exe install Minion-Venv C:\salt-deploy\win\start.bat'
    register: minionsvc
    when: not service_info.exists == true
  - debug:
      var: minionsvc

  # Step 15 : Start Salt Minion Service if not running already
  - name: Start Minion Service 
    raw: 'C:\salt-deploy\win\nssm.exe start Minion-Venv'
    register: startminion
    when: not service_info.state == 'running'
  - debug:
      var: startminion

Hope you’ve followed all the steps and able to configure Salt Minion in a Python virtual environment on windows successfully.

What am I missing here? Let me know in the comments and I’ll add it in!

One thought on “Setup Salt Minion in a Python3 Virtual Environment on Windows

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s