add or update dns with API OVH

This commit is contained in:
2022-12-18 18:42:14 +01:00
parent 2a63aed0e3
commit 67c2a0ed3e
4 changed files with 143 additions and 54 deletions

View File

@@ -0,0 +1 @@
ovh

View File

@@ -45,22 +45,54 @@
ansible.builtin.pause:
minutes: 2
- name: Fetch services
set_fact:
service: "{{ query('kubernetes.core.k8s', kind='Service', namespace='testing') }}"
register: result
tags: [ "services"]
- name: Get IP Cluster
block:
- name: Fetch services
set_fact:
service: "{{ query('kubernetes.core.k8s', kind='Service', namespace='testing') }}"
register: result
- name: debug services
debug:
msg: "{{ result.ansible_facts.service[0].status.loadBalancer.ingress[0].ip }}"
tags: [ "services"]
- name: Test API Backend
ansible.builtin.uri:
url: "http://{{ result.ansible_facts.service[0].status.loadBalancer.ingress[0].ip }}:{{ port }}/api/hello"
status_code: 200
method: GET
tags: [ "services" ]
- name: debug services
debug:
msg: "{{ result.ansible_facts.service[0].status.loadBalancer.ingress[0].ip }}"
- name: Test API Backend
ansible.builtin.uri:
url: "http://{{ result.ansible_facts.service[0].status.loadBalancer.ingress[0].ip }}:{{ port }}/api/hello"
status_code: 200
method: GET
tags: ["services", "dns"]
- name: Add DNS for IP Cluster
block:
- name: Copy script add or update DNS
template:
src: "dns_add_update.py.j2"
dest: "{{ project_backend }}/dns_add_update.py"
- name: Copy requierements python
copy:
src: "requierements.txt"
dest: "{{ project_backend }}"
- name: Pip install requierements
shell: "pip3.10 install -r {{ project_backend }}/requierements.txt"
- name: En attente de l'installation des packages
ansible.builtin.pause:
minutes: 2
- name: Add or update DNS
shell: "python3.10 {{ project_backend }}/dns_add_update.py --ip {{ result.ansible_facts.service[0].status.loadBalancer.ingress[0].ip }}"
register: result
- name: Display result script
debug:
msg: "{{ result }}"
- name: Test API Backend with DNS
ansible.builtin.uri:
url: "http://api.valczeryba.ovh:{{ port }}/api/hello"
status_code: 200
method: GET
tags: [ "dns" ]

View File

@@ -0,0 +1,49 @@
# -*- encoding: utf-8 -*-
'''
First, install the latest release of Python wrapper: $ pip install ovh
'''
import json
import ovh
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--ip', help='foo help')
args = parser.parse_args()
# Instanciate an OVH Client.
# You can generate new credentials with full access to your account on
# the token creation page
client = ovh.Client(
endpoint='ovh-eu', # Endpoint of API OVH Europe (List of available endpoints)
application_key='{{ application_key }}', # Application Key
application_secret='{{ application_secret }}', # Application Secret
consumer_key='{{ consumer_key }}', # Consumer Key
)
result = client.get('/domain/zone/valczeryba.ovh/record',
fieldType='A',
subDomain='api',
)
# Pretty print
if len(result) > 0:
for idDns in result:
result = client.get('/domain/zone/valczeryba.ovh/record/{0}'.format(idDns))
if result["target"] != args.ip:
result = client.put('/domain/zone/valczeryba.ovh/record/{0}'.format(idDns),
subDomain='api',
target=args.ip,
)
print(result)
else:
result = client.post('/domain/zone/valczeryba.ovh/record',
fieldType='A',
subDomain='api',
target=args.ip,
ttl=None,
)
print(result)