Search code examples
pythonmoduleansibleansible-awx

How to run Ansible Module in python with an ansible playbook


I have to develop a module ansible, in python to manage the configuration of fail2ban however I can not manage to run it when I run my playbook

the playbook code :

   - name: "test code"
  hosts: 10.7.150.113
  tasks:
  - name: "test"
      fail2ban_SSH_configurator:
        bantime: 10

the modules code :

from ansible.module_utils.basic import AnsibleModule  

def main(): 
    module = AnsibleModule( 
        argument_spec=dict( 
            path    = dict(required=False, type=’str’),
            bantime    = dict(required=True, type=’int’),
            findtime    = dict(required=False, type=’int’),
            maxretry    = dict(required=False, type=’int’),
            ignoreip    = dict(required=False, type=’str’),
            destemail    = dict(required=False, type=’str’),
            sender    = dict(required=False, type=’str’),
            sendername    = dict(required=False, type=’str’),
            mta    = dict(required=False, type=’str’),
            action    = dict(required=False, type=’str’),
        ) 
    )
    path_local = module.params.get('path')
    bantime_local = module.params.get('bantime')
    findtime_local = module.params.get('fintime')
    maxretry_local = module.params.get('maxretry')
    ignoreip_local = module.params.get('ignoreip')
    destmail_local = module.params.get('destmail')
    sender_local = module.params.get('sender')
    sendername_local = module.params.get('sendername')
    mta_local = module.params.get('mta')
    action_local = module.params.get('action')
    fichier = open("path", "a")
    fichier.write(" [DEFAULT]")
    fichier.write("\nbantime =")
    fichier.write("\nfindtime = ")
    fichier.write("\nmaxretry = ")
    fichier.write("\nignoreip = ")
    fichier.write("\ndestemail = ")
    fichier.write("\nsender = ")
    fichier.write("\nsendername = ")
    fichier.write("\nmta = ")
    fichier.write("\naction = ")
    fichier.close()


module.exit_json(changed=False, results=bantime_local)  

if __name__ == "__main__": 
    main()

the python code is far from over I just wanted to test it before going any further.

But I don't know how to run my module , when I try to lunch my playbook I have this error : enter image description here


Solution

  • fail2ban appears to be too much indented. The error complains about a yaml indentention problem.

    "fail2ban" and "name" should be aligned.

    Hope this helps.