```yaml | |
--- | |
- name: This sets up an httpd webserver | |
hosts: localhost | |
tasks: | |
- debug: | |
msg: My Name is Rajesh | |
- weather: | |
appkey: fe8f2030bd325e2f9c85a96ab20aaa9f | |
treshold: 2 | |
register: output | |
- debug: | |
var: output.decision | |
``` |
#!/usr/bin/python | |
import requests | |
import json | |
def get_temperature(appkey, city, module, units='metric'): | |
session = requests.Session() | |
headers = {'Content-Type': 'application/json'} | |
url = 'http://api.openweathermap.org/data/2.5/weather' | |
params = {'q': city, 'APPID': appkey, 'units': units} | |
try: | |
response = session.request('GET', url, headers=headers, params=params) | |
except requests.exceptions.RequestException as e: | |
module.fail_json(msg=e) | |
if response.status_code not in [200]: | |
module.fail_json(msg=(response.status_code, response.content)) | |
response_content = json.loads(response.content) | |
return response_content['main']['temp'] | |
def main(): | |
module = AnsibleModule( | |
argument_spec=dict( | |
appkey=dict(required=True), | |
city=dict(default='munich,de'), | |
treshold=dict(required=True, type='float') | |
), | |
) | |
city = module.params['city'] | |
treshold = module.params['treshold'] | |
appkey = module.params['appkey'] | |
temperature = get_temperature(appkey, city, module) | |
if temperature > treshold: | |
module.exit_json(changed=True, decision='Run, Yves, Run, it is {} in {}'.format(temperature, city)) | |
if temperature < treshold: | |
module.exit_json(changed=False, decision='Stop, Yves, Stop, it is {} in {}'.format(temperature, city)) | |
from ansible.module_utils.basic import * | |
main() |

















I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND