Passing variables as arguments to module from Ansible Playbook

I created an Ansible module in python that works correctly when I call it from the command line with:

ansible routers -m ans_check_dis.py -a ‘user=someuser host=somehost password=somepassword’ -k

Now I am trying to build this into a playbook to be called and have the variables used as the arguments for the module. I have tried a bunch of different iterations and tried searching for it and can’t seem to find how to make this work. Any recommendations or guidance would be greatly appreciated.

Hi Joseph,

It won’t matter, but typical convention is to not have modules end in “.py”, but it seems like your problem may be that it’s not in the module path.

This is configured in Ansible.cfg, or you can just drop the module into a “./library” directory alongside your playbook.

I’d have to see the line from the playbook to see if you were having a specific problem with the invocation, but my guess is that’s the problem.

If that’s not it, I’d need more information about what “doesn’t work” means in your case.

Thanks!

The module is sitting in /usr/share/ansible in the root and it does have a .py. The module is ans_check_dis.py.

The playbook calls the module just fine, however nothing was working and it’s complaining about the variables being empty. So I put a 30 second wait into the module so that I could check the arguments file and I found that it’s empty.

Here is the main.yml from my tasks folder:

“however nothing was working and it’s complaining about the variables being empty”

Can you paste the specific output and the rest of your ansible playbook you are using to test this?

Also, the /usr/share/ansible directory may expect your module to be in a category subdirectory, it’s been so long that we’ve had categories, I don’t know if that’s true without spelunking through the source. The subdirectory name would not be important.

Here is the output from ansible-playbook command:

jjenkins@CAARPWATCH:~/ANSIBLE/TEST$ ansible-playbook site.yml -k

SSH password:
PLAY [Lookup information on the Switch] ***************************************
GATHERING FACTS ***************************************************************
ok: [localhost]
TASK: [router | Run the Command on the switch] ********************************
fatal: [localhost] => failed to parse: Traceback (most recent call last):
File “/home/jjenkins/.ansible/tmp/ansible-tmp-1411433982.14-168150244858379/ans_check_dis.py”, line 38, in
remote_conn_pre.connect(dest, username=userid, password=passw, allow_agent=False,look_for_keys=False)
NameError: name ‘dest’ is not defined
FATAL: all hosts have already failed – aborting
PLAY RECAP ********************************************************************
to retry, use: --limit @/home/jjenkins/site.retry
localhost : ok=1 changed=0 unreachable=1 failed=0

When I the arguments file under: /home/jjenkins/.ansible/tmp/ansible-tmp-1411433982.14-168150244858379/arguments
it’s empty, its like it didn’t grab the vars out of the vars/main.yml

I may be missing something here, but in your example you did not seem to be passing anything to your module in your playbook. You mention that the vars are in vars/main.yml, however you still need to pass those vars to your module. I imagine something like the following based on your output of vars/main.yml:

That’s what I was missing. Thanks for that.