diff --git a/action_plugins/moinmoin_page.py b/action_plugins/moinmoin_page.py index dff9b56c65029ea4f2107f886910da1c3733d221..30bd8c4975eb1d95a18b825767fdc544d3127221 100755 --- a/action_plugins/moinmoin_page.py +++ b/action_plugins/moinmoin_page.py @@ -81,7 +81,7 @@ class ActionModule(ActionBase): search = re.search('name=\"ticket\" value=\"([^\"]*)\"', content) if not search: raise AnsibleError(to_native('no edit ticket was found')) - + return search.group(1) @@ -132,12 +132,10 @@ class ActionModule(ActionBase): if task_vars is None: task_vars = dict() - + result = super(ActionModule, self).run(tmp, task_vars) del tmp - result['changed'] = False - url = self._task.args.get("url") user = self._task.args.get("user") @@ -147,34 +145,32 @@ class ActionModule(ActionBase): cookie = self.login(url, user, password) - changed = False - try: raw = self.craft_request("?action=raw")(url, cookie) - if raw != content: - changed = True except urllib.error.HTTPError: # We will create the page. - changed = True raw = "" + diff = difflib.unified_diff(raw.splitlines(), content.splitlines(), fromfile="old", tofile="new", lineterm="") + i=0 + # Display any change - if changed: - diff = difflib.unified_diff(raw.splitlines(), content.splitlines(), fromfile="old", tofile="new", lineterm="") - for line in diff: - if line.startswith("-"): - display.display(line, "red") - elif line.startswith("+"): - display.display(line, "green") - elif line.startswith("@"): - display.display(line, "yellow") - else: - display.display(line) - - # Do apply the change if not in check mode - if not self._play_context.check_mode: - self.edit(url, user, password, content, revision_comment, cookie) - - + for line in diff: + i+=1 + if line.startswith("-"): + display.display(line, "red") + elif line.startswith("+"): + display.display(line, "green") + elif line.startswith("@"): + display.display(line, "yellow") + else: + display.display(line) + + # Do apply the change if not in check mode + if not self._play_context.check_mode: + self.edit(url, user, password, content, revision_comment, cookie) + + result['changed']=i>0 + self._supports_check_mode = True self._supports_async = False diff --git a/roles/moinmoin-gendoc/library/moinmoin_page.py b/roles/moinmoin-gendoc/library/moinmoin_page.py deleted file mode 100644 index b6f6ee91a23af165afd08f7ac01099d4f59c3563..0000000000000000000000000000000000000000 --- a/roles/moinmoin-gendoc/library/moinmoin_page.py +++ /dev/null @@ -1,139 +0,0 @@ -#!/usr/bin/env python3 - -# Copyright: (c) 2019, Alexandre Iooss <erdnaxe@crans.org> -# GNU General Public License v3.0+ - -""" -This module simulate the edition of a MoinMoin wiki page - -Example: - moinmoin_page: - url: https://wiki.crans.org/WikiErdnaxe - user: WikiErdnaxe - password: HoTuNeMeConnaisPas - content: "{{ lookup('template', 'mapage.j2') }}" - revision_comment: Bip bip -""" - -import re -import urllib.error -import urllib.parse -import urllib.request - -from ansible.module_utils.basic import AnsibleModule - - -def login(url, user, password): - """ - Log in and return session cookie or None if failed - - :param url: random wiki url (not root page) - :param user: wiki user - :param password: user's password - :return: session cookie - """ - # Send a HTTP POST request - data = urllib.parse.urlencode({ - 'action': 'login', - 'login': 'Connexion', - 'name': user, - 'password': password - }).encode() - req = urllib.request.Request(url, data) - try: - response = urllib.request.urlopen(req) - cookie = response.getheader('set-cookie') - except urllib.error.HTTPError as e: - # If 404, then also return header - cookie = e.getheader('set-cookie') - - # Check that authentication worked - assert cookie, 'server did not return a session cookie' - return cookie - - -def edit_ticket(url, cookie): - """ - Return edition ticket of url - - :param url: page to edit - :param cookie: session cookie - :return: edit ticket - """ - # Send request with session cookie - suffix = "?action=edit&editor=text" - req = urllib.request.Request(url + suffix) - req.add_header("Cookie", cookie) - content = urllib.request.urlopen(req).read().decode('utf-8') - - # Search for ticket - search = re.search('name=\"ticket\" value=\"([^\"]*)\"', content) - assert search, 'no edit ticket was found' - return search.group(1) - - -def edit(url, user, password, content, revision_comment): - """ - Edit a MoinMoin wiki page - - :param url: page to edit - :param user: wiki user - :param password: user's password - :param content: content to place on this page - :param revision_comment: revision comment - """ - # Connect and get edit ticket - cookie = login(url, user, password) - ticket = edit_ticket(url, cookie) - - # Create request and send - data = { - 'button_save': 'Enregistrer les modifications', - 'category': '', - 'comment': revision_comment.encode("utf-8"), - 'savetext': content.encode("utf-8"), - 'action': 'edit', - 'ticket': ticket - } - req = urllib.request.Request(url, urllib.parse.urlencode(data).encode()) - req.add_header("Cookie", cookie) - urllib.request.urlopen(req) - - -def run_module(): - # Define arguments that should be passed - module_args = { - 'url': {'type': 'str', 'required': True}, - 'user': {'type': 'str', 'required': True}, - 'password': {'type': 'str', 'required': True}, - 'content': {'type': 'str', 'required': True}, - 'revision_comment': {'type': 'str', 'required': True}, - } - - # Define arguments that are returned - result = { - 'changed': False, - } - - # Our AnsibleModule - module = AnsibleModule( - argument_spec=module_args, - supports_check_mode=True - ) - - # TODO: get current wiki page and compare - result['changed'] = True - - # If not is check mode and page need to change, then update page - if not module.check_mode and result['changed']: - edit(**module.params) - - module.exit_json(**result) - - -def main(): - run_module() - - -if __name__ == '__main__': - main() diff --git a/roles/moinmoin-gendoc/templates/server.j2 b/roles/moinmoin-gendoc/templates/server.j2 index e287e258f246eef8d6943296fcb24346694d0f9b..c49a503996995abfecc3f67cd972bf8580508106 100644 --- a/roles/moinmoin-gendoc/templates/server.j2 +++ b/roles/moinmoin-gendoc/templates/server.j2 @@ -4,7 +4,7 @@ == Caractéristiques matérielles == {% if ansible_form_factor != 'Other' and ansible_form_factor != 'Unknown' %} -'''Forme du serveur''' : +'''Forme du serveur''' : {{ ansible_form_factor }} {% endif %} @@ -56,7 +56,7 @@ et {{ (ansible_memory_mb.swap.total/1024)|round(1) }} GiB de SWAP. == Caractéristiques logicielles == '''Système d'exploitation''' : -{{ ansible_lsb.description }} +{{ ansible_distribution }} {{ ansible_distribution_major_version }} ({{ ansible_distribution_release }}) '''Noyau''' : {{ ansible_kernel }}