From 5b3a00b86097d4b01ddaffe957f0c66f4d0ceba7 Mon Sep 17 00:00:00 2001 From: medusa Date: Sat, 1 Jun 2024 05:10:39 +0000 Subject: [PATCH] Update work/tbx/meraki/meraki_api.md --- work/tbx/meraki/meraki_api.md | 192 +++++++++++++++++++++++++++++++++- 1 file changed, 191 insertions(+), 1 deletion(-) diff --git a/work/tbx/meraki/meraki_api.md b/work/tbx/meraki/meraki_api.md index 46de059..2ce8f90 100644 --- a/work/tbx/meraki/meraki_api.md +++ b/work/tbx/meraki/meraki_api.md @@ -1296,4 +1296,194 @@ graph TD; ``` -By adding this Mermaid chart to your GitHub site, you'll have a clear visual representation of the hierarchical structure within Meraki's organization, network, and device model. If you need further customization or have additional questions, feel free to ask! \ No newline at end of file +By adding this Mermaid chart to your GitHub site, you'll have a clear visual representation of the hierarchical structure within Meraki's organization, network, and device model. If you need further customization or have additional questions, feel free to ask! + +--- + +### Guide: Converting Python Scripts to Ansible Playbooks and Vice Versa + +This guide provides a step-by-step approach for converting Python scripts to Ansible playbooks and Ansible playbooks to Python scripts. It also outlines when to choose Bash/Python scripts versus Ansible playbooks based on your use case. + +--- + +### Converting Python to Ansible + +**1. Identify each API call in the Python script.** + +**2. Use the Ansible `uri` module for HTTP requests.** + +**3. Use the `set_fact` module to store variables.** + +**4. Use the `copy` module to save content to files.** + +**5. Use `ansible.builtin.debug` for debugging output.** + +--- + +**Example Python Script: Fetching Organizations and Networks** + +```python +import requests +import json + +API_KEY = 'your_api_key' +BASE_URL = 'https://api.meraki.com/api/v1' + +# Fetch organizations +response = requests.get(f'{BASE_URL}/organizations', headers={'Authorization': f'Bearer {API_KEY}'}) +organizations = response.json() +with open('organizations.json', 'w') as f: + json.dump(organizations, f, indent=4) +print("Organizations data saved to organizations.json") + +# Extract organization ID +org_id = organizations[0]['id'] + +# Fetch networks +response = requests.get(f'{BASE_URL}/organizations/{org_id}/networks', headers={'Authorization': f'Bearer {API_KEY}'}) +networks = response.json() +with open('networks.json', 'w') as f: + json.dump(networks, f, indent=4) +print("Networks data saved to networks.json") +``` + +**Converted Ansible Playbook (myplaybook.yml):** + +```yaml +--- +- hosts: localhost + gather_facts: false + tasks: + - name: Get all Organizations + uri: + url: "https://api.meraki.com/api/v1/organizations" + method: GET + headers: + Authorization: "Bearer {{ lookup('env', 'MERAKI_DASHBOARD_API_KEY') }}" + return_content: yes + register: organizations + + - name: Save organizations to file + copy: + content: "{{ organizations.json | to_nice_json }}" + dest: ./organizations.json + + - name: Extract organization ID + set_fact: + organization_id: "{{ organizations.json[0].id }}" + + - name: Get all Networks for the first Organization + uri: + url: "https://api.meraki.com/api/v1/organizations/{{ organization_id }}/networks" + method: GET + headers: + Authorization: "Bearer {{ lookup('env', 'MERAKI_DASHBOARD_API_KEY') }}" + return_content: yes + register: networks + + - name: Save networks to file + copy: + content: "{{ networks.json | to_nice_json }}" + dest: ./networks.json +``` + +**Hosts File (hosts):** + +```ini +[local] +localhost +``` + +**Run the Playbook:** + +```sh +ansible-playbook -i hosts myplaybook.yml +``` + +--- + +### Converting Ansible to Python + +**1. Identify each task in the Ansible playbook.** + +**2. Use the `requests` library for HTTP requests.** + +**3. Use Python variables to store and manipulate data.** + +**4. Use `print` or `json.dumps` for output.** + +--- + +**Example Ansible Playbook:** + +```yaml +--- +- hosts: localhost + gather_facts: false + tasks: + - name: Get all Organizations + cisco.meraki.organizations_info: + meraki_api_key: "{{ lookup('env', 'MERAKI_DASHBOARD_API_KEY') }}" + register: result + + - name: Show Organizations List + ansible.builtin.debug: + msg: "{{ result.meraki_response | json_query('[*].{id: id, name: name}') }}" + + - name: Get all Networks for the first Organization + cisco.meraki.networks_info: + meraki_api_key: "{{ lookup('env', 'MERAKI_DASHBOARD_API_KEY') }}" + organization_id: "{{ result.meraki_response[0].id }}" + register: networks + + - name: Show Networks List + ansible.builtin.debug: + msg: "{{ networks.meraki_response | json_query('[*].{id: id, name: name}') }}" +``` + +**Converted Python Script:** + +```python +import requests +import json +import os + +API_KEY = os.getenv('MERAKI_DASHBOARD_API_KEY') +BASE_URL = 'https://api.meraki.com/api/v1' + +# Get all Organizations +response = requests.get(f'{BASE_URL}/organizations', headers={'Authorization': f'Bearer {API_KEY}'}) +organizations = response.json() +print("Organizations List:") +print(json.dumps([{ 'id': org['id'], 'name': org['name'] } for org in organizations], indent=4)) + +# Extract organization ID +organization_id = organizations[0]['id'] + +# Get all Networks for the first Organization +response = requests.get(f'{BASE_URL}/organizations/{organization_id}/networks', headers={'Authorization': f'Bearer {API_KEY}'}) +networks = response.json() +print("Networks List:") +print(json.dumps([{ 'id': net['id'], 'name': net['name'] } for net in networks], indent=4)) +``` + +--- + +### Choosing Between Bash/Python and Ansible + +**Bash/Python:** +- **Use for quick scripts**: Ideal for simple, one-off tasks. +- **Fine-grained control**: Offers detailed control over each step and can handle dynamic programming logic easily. +- **Flexibility**: Easily switch between different tasks and workflows. + +**Ansible:** +- **Use for automation**: Best for automating routine tasks and ensuring consistency across environments. +- **Scalability**: Designed to scale across many devices and networks. +- **Readability and simplicity**: Playbooks are written in YAML, which is easy to read and understand. +- **Error handling and idempotence**: Handles errors gracefully and ensures tasks run once with the same effect. + +--- + +By following this guide, you can convert Python scripts to Ansible playbooks and vice versa, allowing you to choose the best tool for your specific needs. Whether you need fine-grained control with Bash/Python or scalable automation with Ansible, this guide provides the necessary steps for effective conversions. + +If you have any further questions or need additional examples, feel free to ask! \ No newline at end of file