Creating an Ansible module for removing a computer from a Windows System Center Configuration Manager (SCCM) Collection involves several steps. Below is a guided walkthrough to help you achieve this. Please note that this process assumes you have basic familiarity with Ansible, Python, and access to a Windows environment with SCCM.
Prerequisites
Ansible: Installed on your control node (the machine from which you'll run Ansible playbooks).
Windows SCCM Environment: Access to an SCCM server with the necessary permissions to manage collections.
PyWMI or Similar Library: For interacting with WMI (Windows Management Instrumentation) on the Windows system. PyWMI is a Python WMI library.
Python: Installed on the machine where you'll develop and potentially run the module (if targeting local development).
Step-by-Step Guide to Creating the Ansible Module
1. Setup Your Development Environment
Ensure Python is installed.
Install pywmi using pip if you decide to use it for WMI interactions:
bash
pip install pywmi
2. Choose an Approach for SCCM Interaction
Using PyWMI or a similar library: Interact with SCCM's WMI namespace directly. This approach requires working with WMI queries and may involve more complexity, especially for those unfamiliar with WMI.
SCCM PowerShell Module: Utilize the official SCCM PowerShell module for more straightforward interaction. For this, your Ansible module would need to invoke PowerShell scripts on the SCCM server. The Remove-CMResource cmdlet is specifically used to remove resources (like computers) from a collection.
For simplicity, let's outline the PowerShell approach, as it's more intuitive for SCCM management. The PowerShell cmdlet Remove-CMResource is used to remove a computer from the specified collection. This cmdlet is part of the SCCM PowerShell module and simplifies the interaction with SCCM collections.
3. Create the Ansible Module
Directory Setup: Within your Ansible project or a test directory, create a structure like so:
This example uses the PowerShell approach. The module will execute a PowerShell script on the SCCM server.
python
#!/usr/bin/python
# -*- coding: utf-8 -*-
DOCUMENTATION = '''
module: sccm_remove_from_collection
short_description: Remove a computer from an SCCM Collection
description:
- Remove a computer from a specified SCCM Collection.
options:
sccm_server:
description:
- The SCCM Server to connect to.
required: true
collection_name:
description:
- Name of the SCCM Collection.
required: true
computer_name:
description:
- Name of the computer to remove.
required: true
author:
- Your Name
'''
from ansible.module_utils.basic import AnsibleModule
import subprocess
def execute_ps_script(sccm_server, collection_name, computer_name):
# Construct the PowerShell command
# The Remove-CMResource cmdlet removes one or more resources from a specified collection.
# The -Force parameter suppresses confirmation prompts.
ps_command = f"Invoke-Command -ComputerName {sccm_server} -ScriptBlock {{ Remove-CMResource -CollectionName '{collection_name}' -ResourceName '{computer_name}' -Force }} -ErrorAction Stop"
try:
# Execute the PowerShell command
subprocess.check_output(["powershell.exe", ps_command], shell=True)
return True, f"Successfully removed {computer_name} from {collection_name}"
except subprocess.CalledProcessError as e:
# Handle errors from PowerShell execution
return False, f"Failed to remove {computer_name} from {collection_name}: {e}"
except Exception as e:
# Catch any other unexpected errors
return False, f"An unexpected error occurred: {e}"
def main():
argument_spec = dict(
sccm_server=dict(required=True),
collection_name=dict(required=True),
computer_name=dict(required=True)
)
module = AnsibleModule(argument_spec=argument_spec)
sccm_server = module.params.get('sccm_server')
collection_name = module.params.get('collection_name')
computer_name = module.params.get('computer_name')
changed, msg = execute_ps_script(sccm_server, collection_name, computer_name)
module.exit_json(changed=changed, msg=msg)
if __name__ == '__main__':
main()
4. Testing Your Module
Create a Test Playbook (test_sccm_module.yml):
yaml
- name: Test SCCM Module
hosts: localhost
gather_facts: no
tasks:
- name: Remove Computer from SCCM Collection
sccm_remove_from_collection:
sccm_server: "your_sccm_server_name"
collection_name: "Your Collection Name"
computer_name: "ComputerNameToRemove"
register: result
ignore_errors: yes
- debug:
var: result
Run Your Test Playbook:
bash
ansible-playbook -vv test_sccm_module.yml
5. Considerations and Next Steps
Security and Permissions: Ensure the account running the Ansible playbook has the necessary permissions on the SCCM server.
Error Handling: Enhance error handling in your module for more robustness. For example, you can add checks for:
Connection Errors: Verify that the SCCM server is reachable.
Permission Issues: Handle cases where the account lacks permissions to modify the collection.
Resource Not Found: Gracefully handle scenarios where the computer or collection does not exist.
Sharing Your Module: If beneficial to others, consider sharing your module through Ansible Galaxy.
This guide should give you a solid starting point. Depending on your environment's specifics, you might need to adjust the module, especially if you decide to use a different approach (e.g., direct WMI interaction).