Passing variables as arguments to module from Ansible Playbook

4,492 views
Skip to first unread message

Joseph Jenkins

unread,
Sep 22, 2014, 5:15:28 PM9/22/14
to ansible...@googlegroups.com
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.

Michael DeHaan

unread,
Sep 22, 2014, 7:43:43 PM9/22/14
to ansible...@googlegroups.com
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!

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/60e7e71f-705a-44e3-af22-498259cb7ef8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Joseph Jenkins

unread,
Sep 22, 2014, 8:43:57 PM9/22/14
to ansible...@googlegroups.com
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:

---

- name: Run the Command on the switch

  ans_check_dis.py: 

Michael DeHaan

unread,
Sep 22, 2014, 8:47:47 PM9/22/14
to ansible...@googlegroups.com
"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.





Joseph Jenkins

unread,
Sep 22, 2014, 9:04:09 PM9/22/14
to ansible...@googlegroups.com
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 <module>
    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

---

routers:

 - { user: somepass, host: somehost, password: somepass }

Is this the correct place to be putting these variables to get passed as arguments?  That is my first problem is that I am not sure I have the values in the right place to get treated as arguments to be passed to the module.

Matt Martz

unread,
Sep 22, 2014, 9:17:40 PM9/22/14
to ansible...@googlegroups.com
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:

---

- name: Run the Command on the switch

  ans_check_dis.py: user="{{item.user}}" password="{{item.password}}" host="{{item.host}}"

  with_items: routers




For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

Joseph Jenkins

unread,
Sep 22, 2014, 9:57:25 PM9/22/14
to ansible...@googlegroups.com
That's what I was missing.  Thanks for that.
Reply all
Reply to author
Forward
0 new messages