🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Ansible Module Sample Code to Work using github API – Example 2

#!/usr/bin/python
DOCUMENTATION = '''
---
module: github_repo
short_description: Manage your repos on Github
'''
EXAMPLES = '''
- name: Create a github Repo
github_repo:
github_auth_key: "..."
name: "Hello-World"
description: "This is your first repository"
private: yes
has_issues: no
has_wiki: no
has_downloads: no
register: result
- name: Delete that repo
github_repo:
github_auth_key: "..."
name: "Hello-World"
state: absent
register: result
'''
from ansible.module_utils.basic import *
import requests
api_url = "https://api.github.com"
def github_repo_present(data):
api_key = data['github_auth_key']
del data['state']
del data['github_auth_key']
headers = {
"Authorization": "token {}" . format(api_key)
}
url = "{}{}" . format(api_url, '/user/repos')
result = requests.post(url, json.dumps(data), headers=headers)
if result.status_code == 201:
return False, True, result.json()
if result.status_code == 422:
return False, False, result.json()
# default: something went wrong
meta = {"status": result.status_code, 'response': result.json()}
return True, False, meta
def github_repo_absent(data=None):
headers = {
"Authorization": "token {}" . format(data['github_auth_key'])
}
url = "{}/repos/{}/{}" . format(api_url, "toast38coza", data['name'])
result = requests.delete(url, headers=headers)
if result.status_code == 204:
return False, True, {"status": "SUCCESS"}
if result.status_code == 404:
result = {"status": result.status_code, "data": result.json()}
return False, False, result
else:
result = {"status": result.status_code, "data": result.json()}
return True, False, result
def main():
fields = {
"github_auth_key": {"required": True, "type": "str"},
"name": {"required": True, "type": "str"},
"description": {"required": False, "type": "str"},
"private": {"default": False, "type": "bool"},
"has_issues": {"default": True, "type": "bool"},
"has_wiki": {"default": True, "type": "bool"},
"has_downloads": {"default": True, "type": "bool"},
"state": {
"default": "present",
"choices": ['present', 'absent'],
"type": 'str'
},
}
choice_map = {
"present": github_repo_present,
"absent": github_repo_absent,
}
module = AnsibleModule(argument_spec=fields)
is_error, has_changed, result = choice_map.get(
module.params['state'])(module.params)
if not is_error:
module.exit_json(changed=has_changed, meta=result)
else:
module.fail_json(msg="Error deleting repo", meta=result)
if __name__ == '__main__':
main()

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.