Basic protoype
- Set/get Creole values by their names - Handles multi values and master/slaves - Basic playbook with module instanciation
This commit is contained in:
parent
5522463e89
commit
3185f8a46c
|
@ -0,0 +1,2 @@
|
||||||
|
/inventory
|
||||||
|
*.retry
|
35
Makefile
35
Makefile
|
@ -1,29 +1,10 @@
|
||||||
eole-2.6.2:
|
DESTDIR := ./
|
||||||
docker build \
|
install:
|
||||||
--build-arg HTTP_PROXY=$(HTTP_PROXY) \
|
install -d $(DESTDIR)usr/share/ansible/plugins/modules
|
||||||
--build-arg HTTPS_PROXY=$(HTTPS_PROXY) \
|
install src/eole_config.py $(DESTDIR)usr/share/ansible/plugins/modules
|
||||||
--build-arg http_proxy=$(http_proxy) \
|
|
||||||
--build-arg https_proxy=$(https_proxy) \
|
|
||||||
-t eolebase-2.6.2:latest \
|
|
||||||
./misc/eole-2.6.2
|
|
||||||
|
|
||||||
ansible-eole: eole-2.6.2
|
link-dev:
|
||||||
docker build \
|
mkdir -p "$(HOME)/.ansible/plugins/modules"
|
||||||
--build-arg HTTP_PROXY=$(HTTP_PROXY) \
|
ln -s "$(PWD)/src" "$(HOME)/.ansible/plugins/modules/eole"
|
||||||
--build-arg HTTPS_PROXY=$(HTTPS_PROXY) \
|
|
||||||
--build-arg http_proxy=$(http_proxy) \
|
|
||||||
--build-arg https_proxy=$(https_proxy) \
|
|
||||||
-t ansible-eole:latest \
|
|
||||||
./misc/ansible-eole
|
|
||||||
|
|
||||||
dev-env:
|
.PHONY: link-locally install
|
||||||
docker run \
|
|
||||||
-it \
|
|
||||||
--rm \
|
|
||||||
-v "$(PWD)/src/eole_config.py:/ansible/lib/ansible/modules/system/eole_config.py:ro" \
|
|
||||||
-v "$(PWD)/testdata:/testdata:ro" \
|
|
||||||
--network host \
|
|
||||||
--name ansible-creole-dev-env \
|
|
||||||
ansible-eole
|
|
||||||
|
|
||||||
.PHONY: eole-2.6.2 ansible-eole
|
|
36
README.md
36
README.md
|
@ -1,9 +1,41 @@
|
||||||
# Ansible - EOLE
|
# Ansible - EOLE
|
||||||
|
|
||||||
Module Ansible permettant de configurer une distribution EOLE
|
Module Ansible permettant de manipuler les valeurs de configuration Creole d'un module EOLE.
|
||||||
|
|
||||||
|
**Stabilité** expérimental
|
||||||
|
|
||||||
|
## Démonstration
|
||||||
|
|
||||||
|
[![asciicast](https://asciinema.org/a/3PyxG4M43cXW0wiUQlgkt3pwQ.svg)](https://asciinema.org/a/3PyxG4M43cXW0wiUQlgkt3pwQ)
|
||||||
|
|
||||||
|
Voir le fichier [`playbook-sample.yml`](./playbook-sample.yml) pour plus de détails.
|
||||||
|
|
||||||
|
## Utilisation
|
||||||
|
|
||||||
|
```shell
|
||||||
|
# Récupérer les sources du projet
|
||||||
|
git clone https://forge.cadoles.com/wpetit/ansible-eole.git
|
||||||
|
cd ansible-eole
|
||||||
|
|
||||||
|
# Exposer le module à Ansible
|
||||||
|
# Voir la section ressources pour plus d'informations sur les modules locaux.
|
||||||
|
make link-dev
|
||||||
|
|
||||||
|
# Créer un fichier d'inventaire simple
|
||||||
|
cat > inventory <<EOF
|
||||||
|
[all]
|
||||||
|
<ip_machine> ansible_user=root
|
||||||
|
EOF
|
||||||
|
|
||||||
|
# Utiliser le playbook d'exemple avec le fichier d'inventaire
|
||||||
|
ansible-playbook -i inventory playbook-sample.yml
|
||||||
|
```
|
||||||
|
|
||||||
## Ressources
|
## Ressources
|
||||||
|
|
||||||
- [Développement de modules Ansible](https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_general.html)
|
- [Développement de modules Ansible](https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_general.html)
|
||||||
|
- [Ajout des modules/plugins locaux à Ansible](https://docs.ansible.com/ansible/latest/dev_guide/developing_locally.html)
|
||||||
|
|
||||||
/usr/lib/python2.7/dist-packages/tiramisu/config.py
|
## Licence
|
||||||
|
|
||||||
|
GPL-3.0
|
|
@ -1,20 +0,0 @@
|
||||||
FROM eolebase-2.6.2:latest
|
|
||||||
|
|
||||||
ARG HTTP_PROXY=
|
|
||||||
ARG HTTPS_PROXY=
|
|
||||||
ARG http_proxy=
|
|
||||||
ARG https_proxy=
|
|
||||||
ARG DEBIAN_FRONTEND=noninteractive
|
|
||||||
|
|
||||||
RUN apt-get update -y &&\
|
|
||||||
apt-get install -y build-essential libssl-dev libffi-dev python-dev python-pip git &&\
|
|
||||||
rm -rf /var/lib/apt/lists/*
|
|
||||||
|
|
||||||
RUN git clone https://github.com/ansible/ansible.git &&\
|
|
||||||
cd ansible &&\
|
|
||||||
pip install wheel &&\
|
|
||||||
pip install -r requirements.txt
|
|
||||||
|
|
||||||
COPY bashrc /root/.bashrc
|
|
||||||
|
|
||||||
WORKDIR ansible
|
|
|
@ -1 +0,0 @@
|
||||||
. hacking/env-setup
|
|
|
@ -1,39 +0,0 @@
|
||||||
FROM ubuntu:16.04
|
|
||||||
|
|
||||||
ARG HTTP_PROXY=
|
|
||||||
ARG HTTPS_PROXY=
|
|
||||||
ARG http_proxy=
|
|
||||||
ARG https_proxy=
|
|
||||||
ENV DEBIAN_FRONTEND=noninteractive
|
|
||||||
|
|
||||||
# systemd/docker compatibility configuration
|
|
||||||
# See https://developers.redhat.com/blog/2016/09/13/running-systemd-in-a-non-privileged-container/
|
|
||||||
ENV container=docker
|
|
||||||
STOPSIGNAL SIGRTMIN+3
|
|
||||||
|
|
||||||
RUN apt-get update -y && apt-get install -y locales wget keyboard-configuration
|
|
||||||
RUN locale-gen --purge fr_FR.UTF-8
|
|
||||||
|
|
||||||
ENV LC_ALL=fr_FR.UTF-8
|
|
||||||
ENV LANG=french
|
|
||||||
|
|
||||||
# Install apt-show-versions
|
|
||||||
# See https://askubuntu.com/questions/916199/install-apt-show-versions-inside-an-ubuntu-docker-container
|
|
||||||
RUN rm /etc/apt/apt.conf.d/docker-gzip-indexes &&\
|
|
||||||
apt-get purge apt-show-versions &&\
|
|
||||||
rm /var/lib/apt/lists/*lz4 &&\
|
|
||||||
apt-get -o Acquire::GzipIndexes=false update &&\
|
|
||||||
apt-get install -y apt-show-versions
|
|
||||||
|
|
||||||
# Configure then install EOLE packages
|
|
||||||
RUN wget -q -O - "http://eole.ac-dijon.fr/eole/project/eole-2.6-repository.key" | apt-key --keyring /etc/apt/trusted.gpg.d/eole-archive-keyring.gpg add -
|
|
||||||
COPY eole.list /etc/apt/sources.list.d/eole.list
|
|
||||||
RUN apt-get update -y &&\
|
|
||||||
apt-get install -y --force-yes eole-server eole-exim-pkg &&\
|
|
||||||
rm -rf /var/lib/apt/lists/*
|
|
||||||
|
|
||||||
ENV DEBIAN_FRONTEND=text
|
|
||||||
|
|
||||||
ENTRYPOINT []
|
|
||||||
|
|
||||||
CMD ["/bin/bash"]
|
|
|
@ -1,3 +0,0 @@
|
||||||
deb http://eole.ac-dijon.fr/eole eole-2.6.2 main cloud
|
|
||||||
deb http://eole.ac-dijon.fr/eole eole-2.6.2-security main cloud
|
|
||||||
deb http://eole.ac-dijon.fr/eole eole-2.6.2-updates main cloud
|
|
|
@ -0,0 +1,79 @@
|
||||||
|
- name: Exemple de playbook Ansible pour le déploiement de module EOLE
|
||||||
|
hosts: all
|
||||||
|
tasks:
|
||||||
|
|
||||||
|
- name: Mise à jour des valeurs de configuration Creole
|
||||||
|
# On utilise le module Ansible "eole_config" implémenté
|
||||||
|
# par ce projet
|
||||||
|
eole_config:
|
||||||
|
name: "{{ item.name }}"
|
||||||
|
value: "{{ item.value | to_json }}"
|
||||||
|
# On définie ici l'ensemble des valeurs de configuration nécessaires
|
||||||
|
# à l'instanciation d'un module eolebase.
|
||||||
|
with_items:
|
||||||
|
- name: ip_ssh_eth0
|
||||||
|
value: [ 0.0.0.0 ]
|
||||||
|
- name: netmask_ssh_eth0
|
||||||
|
value: [ 0.0.0.0 ]
|
||||||
|
- name: ip_admin_eth0
|
||||||
|
value: [ 0.0.0.0 ]
|
||||||
|
- name: netmask_admin_eth0
|
||||||
|
value: [ 0.0.0.0 ]
|
||||||
|
- name: nom_machine
|
||||||
|
value: ansible-eole
|
||||||
|
- name: nom_domaine_local
|
||||||
|
value: ansible-eole
|
||||||
|
# Dans les 3 entrées suivantes, on utilise les données récoltées
|
||||||
|
# dans les "facts" Ansible pour définir l'adresse IP de la carte
|
||||||
|
# eth0, l'adresse IP de la passerelle et les serveurs DNS.
|
||||||
|
- name: adresse_ip_eth0
|
||||||
|
value: '{{ ansible_default_ipv4.address }}'
|
||||||
|
- name: adresse_ip_gw
|
||||||
|
value: '{{ ansible_default_ipv4.gateway }}'
|
||||||
|
- name: adresse_ip_dns
|
||||||
|
value: '{{ ansible_dns.nameservers }}'
|
||||||
|
- name: nom_academie
|
||||||
|
value: ac-ansible
|
||||||
|
- name: exim_relay_smtp
|
||||||
|
value: 127.0.0.1
|
||||||
|
- name: numero_etab
|
||||||
|
value: '000Test'
|
||||||
|
- name: libelle_etab
|
||||||
|
value: Ansible EOLE
|
||||||
|
|
||||||
|
# Le paquet python-pexpect est nécessaire
|
||||||
|
# à l'utilisation du module Ansible "expect"
|
||||||
|
- name: Installation de pexpect
|
||||||
|
package:
|
||||||
|
name: python-pexpect
|
||||||
|
state: present
|
||||||
|
|
||||||
|
# On récupère l'état du module (instancié ou non)
|
||||||
|
- name: On vérifie l'état du module
|
||||||
|
eole_config:
|
||||||
|
name: module_instancie
|
||||||
|
register: module_instancie
|
||||||
|
|
||||||
|
# On instancie le module si celui ci ne l'est pas déjà
|
||||||
|
- name: Instancier le module EOLE
|
||||||
|
when: module_instancie.value != 'oui'
|
||||||
|
expect:
|
||||||
|
command: /usr/bin/instance
|
||||||
|
# Attention, les mots de passe sont en clairs dans le playbook !
|
||||||
|
# Il serait préférable d'utiliser le mécanisme ansible_vault ici.
|
||||||
|
responses:
|
||||||
|
"Nouveau mot de passe:":
|
||||||
|
- "NotSoSecret;21"
|
||||||
|
- "NotSoSecret;21"
|
||||||
|
"Confirmation du mot de passe:":
|
||||||
|
- "NotSoSecret;21"
|
||||||
|
- "NotSoSecret;21"
|
||||||
|
'\[non\]':
|
||||||
|
- non
|
||||||
|
'\[oui\]':
|
||||||
|
- non
|
||||||
|
timeout: 600
|
||||||
|
|
||||||
|
# On reconfigure le module
|
||||||
|
- name: Reconfigurer le module EOLE
|
||||||
|
command: /usr/bin/reconfigure
|
|
@ -8,30 +8,30 @@ ANSIBLE_METADATA = {
|
||||||
|
|
||||||
DOCUMENTATION = '''
|
DOCUMENTATION = '''
|
||||||
---
|
---
|
||||||
module: creole
|
module: eole_config
|
||||||
|
|
||||||
short_description: This is my sample module
|
short_description: EOLE configuration values management
|
||||||
|
|
||||||
version_added: "2.4"
|
version_added: "2.4"
|
||||||
|
|
||||||
description:
|
description:
|
||||||
- "This is my longer description explaining my sample module"
|
- This module provides facilities to read/update EOLE configuration values.
|
||||||
|
|
||||||
options:
|
options:
|
||||||
name:
|
name:
|
||||||
description:
|
description:
|
||||||
- This is the message to send to the sample module
|
- The name of the configuration variable, as seen in "CreoleGet --list"
|
||||||
required: true
|
required: true
|
||||||
new:
|
value:
|
||||||
description:
|
description:
|
||||||
- Control to demo if the result of this module is changed or not
|
- If set, update the configuration variable with this value
|
||||||
required: false
|
required: false
|
||||||
|
|
||||||
extends_documentation_fragment:
|
extends_documentation_fragment:
|
||||||
- azure
|
- azure
|
||||||
|
|
||||||
author:
|
author:
|
||||||
- Your Name (@yourhandle)
|
- William Petit
|
||||||
'''
|
'''
|
||||||
|
|
||||||
EXAMPLES = '''
|
EXAMPLES = '''
|
||||||
|
@ -53,15 +53,13 @@ EXAMPLES = '''
|
||||||
'''
|
'''
|
||||||
|
|
||||||
RETURN = '''
|
RETURN = '''
|
||||||
original_message:
|
name:
|
||||||
description: The original name param that was passed in
|
|
||||||
type: str
|
|
||||||
message:
|
|
||||||
description: The output message that the sample module generates
|
description: The output message that the sample module generates
|
||||||
'''
|
'''
|
||||||
|
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
from creole.loader import creole_loader, config_save_values
|
from creole.loader import creole_loader, config_save_values
|
||||||
|
import json
|
||||||
|
|
||||||
def run_module():
|
def run_module():
|
||||||
|
|
||||||
|
@ -71,13 +69,13 @@ def run_module():
|
||||||
value=dict(type='json', required=False),
|
value=dict(type='json', required=False),
|
||||||
load_extra=dict(type='bool', required=False, default=True),
|
load_extra=dict(type='bool', required=False, default=True),
|
||||||
check_mandatory=dict(type='bool', required=False, default=False),
|
check_mandatory=dict(type='bool', required=False, default=False),
|
||||||
reload_config=dict(type='bool', required=False, default=False),
|
reload_config=dict(type='bool', required=False, default=True)
|
||||||
)
|
)
|
||||||
|
|
||||||
result = dict(
|
result = dict(
|
||||||
changed=False,
|
changed=False,
|
||||||
original_message='',
|
path='',
|
||||||
message=''
|
name=''
|
||||||
)
|
)
|
||||||
|
|
||||||
module = AnsibleModule(
|
module = AnsibleModule(
|
||||||
|
@ -86,44 +84,48 @@ def run_module():
|
||||||
)
|
)
|
||||||
|
|
||||||
update_value = not module.params['value'] is None
|
update_value = not module.params['value'] is None
|
||||||
c = creole_loader(rw=update_value, load_extra=module.params['load_extra'])
|
|
||||||
|
|
||||||
path = c.creole.find_first(byname=module.params['name'], type_='path')
|
try:
|
||||||
value = getattr(c, path)
|
c = creole_loader(rw=update_value, load_extra=module.params['load_extra'])
|
||||||
|
path = c.creole.find_first(byname=module.params['name'], type_='path')
|
||||||
result['state'] = dict(
|
|
||||||
name=module.params['name'],
|
|
||||||
value=value
|
|
||||||
)
|
|
||||||
|
|
||||||
if update_value:
|
|
||||||
setattr(c, path, module.params['value'])
|
|
||||||
if not module.check_mode:
|
|
||||||
try:
|
|
||||||
config_save_values(
|
|
||||||
c, 'creole',
|
|
||||||
check_mandatory=module.params['check_mandatory'],
|
|
||||||
reload_config=module.params['reload_config']
|
|
||||||
)
|
|
||||||
result['changed'] = True
|
|
||||||
except e:
|
|
||||||
module.fail_json(msg=str(e), **result)
|
|
||||||
else:
|
|
||||||
value = getattr(c, path)
|
value = getattr(c, path)
|
||||||
|
except Exception as ex:
|
||||||
|
module.fail_json(msg=ex.message, **result)
|
||||||
|
|
||||||
# use whatever logic you need to determine whether or not this module
|
result['name'] = module.params['name']
|
||||||
# made any modifications to your target
|
try:
|
||||||
# if module.params['new']:
|
result['value'] = json.loads(json.dumps(value))
|
||||||
# result['changed'] = True
|
except TypeError:
|
||||||
|
result['value'] = value
|
||||||
|
|
||||||
# during the execution of the module, if there is an exception or a
|
result['path'] = path
|
||||||
# conditional state that effectively causes a failure, run
|
|
||||||
# AnsibleModule.fail_json() to pass in the message and the result
|
try:
|
||||||
# if module.params['name'] == 'fail me':
|
if update_value:
|
||||||
# module.fail_json(msg='You requested this to fail', **result)
|
|
||||||
|
new_value = module.params['value']
|
||||||
|
|
||||||
|
if isinstance(new_value, str):
|
||||||
|
new_value = unicode(new_value)
|
||||||
|
|
||||||
|
new_value = json.loads(new_value)
|
||||||
|
|
||||||
|
if value != new_value:
|
||||||
|
|
||||||
|
setattr(c, path, new_value)
|
||||||
|
value = getattr(c, path)
|
||||||
|
|
||||||
|
if not module.check_mode:
|
||||||
|
config_save_values(
|
||||||
|
c, 'creole',
|
||||||
|
check_mandatory=module.params['check_mandatory'],
|
||||||
|
reload_config=module.params['reload_config']
|
||||||
|
)
|
||||||
|
result['changed'] = True
|
||||||
|
|
||||||
|
except Exception as ex:
|
||||||
|
module.fail_json(msg=ex.message, **result)
|
||||||
|
|
||||||
# in the event of a successful module execution, you will want to
|
|
||||||
# simple AnsibleModule.exit_json(), passing the key/value results
|
|
||||||
module.exit_json(**result)
|
module.exit_json(**result)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
Loading…
Reference in New Issue