custom module, how to locate ansible magic vars

20 views
Skip to first unread message

texas living

unread,
Sep 28, 2018, 7:35:01 PM9/28/18
to Ansible Project
I am trying to write my first custom module and cannot find how to locate an ansible var that is not part of the task parameters sent. For example, let's say I want to find the ansible_ssh_host or inventory_hostname and they are not in the environment vars. I searched for import ansible.constants but could not find any examples or if this is the right module to call for this.

Any help would be much appreciated.

Brian Coca

unread,
Sep 28, 2018, 9:13:21 PM9/28/18
to Ansible Project
You cannot, you only have access to the options passed into the module itself.

--
----------
Brian Coca

texas living

unread,
Sep 29, 2018, 9:20:21 AM9/29/18
to Ansible Project
I am curious how for example the copy module works. There are no options to pass the user, password or server but somehow it must work. I suspect it uses ssh private keys that are set in ansible.cfg file. Is this how it works and if so how does the copy module access these values? I know I can pass whatever I want into the environment, but was hoping there was an ansible module which could access the values in ansible.cfg. I found the  ansible.config.manager module and was wondering if it is used to parse the ansible.cfg file.

texas living

unread,
Sep 29, 2018, 11:56:36 AM9/29/18
to Ansible Project
I was able to parse the ansible.cfg file using "ansible.module_utils.six.moves import configparser" and pull the values I am looking for. Now I just need a way to pull the remote_addr variable so I would not need to pass in the server: option from the task.

James Tanner

unread,
Sep 29, 2018, 12:32:59 PM9/29/18
to ansible...@googlegroups.com
There's another class of plugins that you can think of as "sitting on top of modules":


Action plugins can do a lot of complicated things such as accessing vars and internal play contexts.

On Sat, Sep 29, 2018 at 11:56 AM texas living <adolp...@gmail.com> wrote:
I was able to parse the ansible.cfg file using "ansible.module_utils.six.moves import configparser" and pull the values I am looking for. Now I just need a way to pull the remote_addr variable so I would not need to pass in the server: option from the task.

--
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/32cfedc7-0fc0-4567-9f05-abfe7dce9e4a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brian Coca

unread,
Oct 1, 2018, 3:41:58 PM10/1/18
to Ansible Project
@texas copy does not read the connection information, that is what the
connection plugin does, it is not module specific.
--
----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages