#!/usr/bin/python # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import (absolute_import, division, print_function) __metaclass__ = type DOCUMENTATION = r''' --- module: zephir_register short_description: This is a module to automate EOLE module registering against zephir # If this is part of a collection, you need to use semantic versioning, # i.e. the version is of the form "2.5.0" and not "2.4". version_added: "1.0.0" description: This is my longer description explaining my test module. options: zephir_user: description: zephir user name. required: true type: str zephir_user_password: description: zephir user password. required: true type: str zephir_address: description: address used to contact zephir server. required: required type: str server_id: description: id in zephir database the server will be registered with. required: true type: int action: description: action to perform regarding server configuration state. required: true type: str # Specify this value according to your collection # in format of namespace.collection.doc_fragment_name extends_documentation_fragment: - cadoles.eole.zephir_register author: - Cadoles ''' EXAMPLES = r''' # Pass in a message - name: Test with a message cadoles.eole.zephir_register: zephir_user: admin zephir_user_password: eole zephir_address: zephir.infra.lan action: download ''' RETURN = r''' # These are examples of possible return values, and in general should use other names for return values. id: description: id the server is associated with. type: int returned: always sample: 1 module: description: module the server is associated with. type: str returned: always sample: eolebase-2.7.2 module_id: description: module id the server is associated with. type: int returned: always sample: 42 variant: description: variant the server is associated with type: str returned: always sample: myeolebase variant_id: description: variant id the server is associated with type: int returned: always sample: 58 ''' from ansible.module_utils.basic import AnsibleModule def run_module(): # define available arguments/parameters a user can pass to the module module_args = dict( zephir_user=dict(type='str', required=True), zephir_user_password=dict(type='str', required=True), zephir_address=dict(type='str', required=True), server_id=dict(type='int', required=True), action=dict(type='str', required=True), ) # seed the result dict in the object # we primarily care about changed and state # changed is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict( changed=False, ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule( argument_spec=module_args, supports_check_mode=True ) if module.check_mode: module.exit_json(**result) ACTIONS = {'Ne rien faire': '1', 'Utiliser la configuration définie sur le serveur Zéphir': '2', 'Sauver la configuration actuelle sur le serveur Zéphir': '3', 'Modifier la variante de serveur': '4'} # if the user is working with this module in only check mode we do not # want to make any changes to the environment, just return the current # state with no modifications try: from zephir.zephir_conf.zephir_conf import id_serveur module.exit_json(**result) except: pass use_pexpect = True try: import importlib.machinery import importlib.util # Import mymodule loader = importlib.machinery.SourceFileLoader( 'enregistrement_zephir', '/usr/bin/enregistrement_zephir' ) spec = importlib.util.spec_from_loader( 'enregistrement_zephir', loader ) enregistrement_zephir = importlib.util.module_from_spec( spec ) loader.exec_module( enregistrement_zephir ) if hasattr(enregistrement_zephir, 'argparse'): use_pexpect = False from subprocess import run except: import pexpect if use_pexpect: responses = {"(.*)Voulez-vous établir une configuration réseau minimale(.*)": "N", "(.*)Entrez l'adresse(.*)": module.params['zephir_address'], "(.*)Entrez votre login pour l'application Zéphir(.*)": module.params['zephir_user'], "(.*)Mot de passe pour l'application Zéphir pour(.*)": module.params['zephir_user_password'], "(.*)créer le serveur dans la base du serveur Zéphir(.*)": "N", "(.*)rien pour saisir directement un n° de serveur(.*)": "", "(.*)entrez le n° identifiant le serveur l'application Zéphir(.*)": module.params['server_id'], "(.*)matériel(.*)": "", "(.*)processeur(.*)": "", "(.*)disque dur(.*)": "", "(.*)continuer(.*)": "O", "(.*)Entrez le numéro de votre choix(.*)": ACTIONS.get(module.params['action'], '2'), } registering_process = pexpect.spawn('/usr/bin/enregistrement_zephir') for key, value in responses.items(): registering_process.expect(key) registering_process.sendline(value) else: run(['/usr/bin/enregistrement_zephir', '--adresse_zephir', module.params['zephir_address'], '--user', module.params['zephir_user'], '--password', module.params['zephir_user_password'], '--id_serveur', module.params['server_id'], '--choix', ACTIONS.get(module.params['action'], '2')], capture_output=True, check=True) # manipulate or modify the state as needed (this is going to be the # part where your module will do what it needs to do) # use whatever logic you need to determine whether or not this module # made any modifications to your target try: from zephir.zephir_conf.zephir_conf import id_serveur result['changed'] = True except: module.fail_json(msg='Server not registered', **result) # during the execution of the module, if there is an exception or a # conditional state that effectively causes a failure, run # AnsibleModule.fail_json() to pass in the message and the 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) def main(): run_module() if __name__ == '__main__': main()