# Running Code on (Local) Ansible Controller

**URL:** <https://forum.ansible.com/t/running-code-on-local-ansible-controller/26433>\
**Category:** Ansible Developer\
**Created:** [January 7, 2018, 12:47pm UTC](https://forum.ansible.com/t/running-code-on-local-ansible-controller/26433 "2018-01-07T12:47:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Charles\_De\_Silva](https://avatars.discourse-cdn.com/v4/letter/c/34f0e0/32.png) [@Charles\_De\_Silva](https://forum.ansible.com/u/Charles_De_Silva)\
**Post date:** [January 7, 2018, 12:47pm UTC](https://forum.ansible.com/t/running-code-on-local-ansible-controller/26433/1 "2018-01-07T12:47:35Z")

</div>

Hi Everyone,

I’ve just started with writing Ansible modules but am having trouble figuring out the best way to have a module run directly on the Ansible controller itself. My primary development needs are to run commands against network appliances for which I can’t copy code to (similar to the existing Cisco IOS/ASA modules).

I worked through the tutorial and the dummy module works fine  
[https://docs.ansible.com/ansible/devel/dev\_guide/developing\_modules\_general.html](https://docs.ansible.com/ansible/devel/dev_guide/developing_modules_general.html)

When I try and add my own code to this, for some reason it switches from using the local provider to network\_cli. Other than specifying to import AnsibleModule, I’ve not asked to load any other modules…perhaps the “smart” method is doing something not-so clever.

The code for my module can be found here, mainly boilerplate at the moment based of the dummy module:  
[https://pastebin.com/SjZ5Nfa2](https://pastebin.com/SjZ5Nfa2)

In the playbook I’ve tried specifying ‘connection: local’ both at the playbook and task level which hasn’t helped. Also in the inventory file I’ve tried using ‘ansible\_connection: local’ which also doesn’t seem to work for me.

The below documentation says that I should be able to do this as well as specify on the CLI:  
[http://docs.ansible.com/ansible/latest/playbooks\_delegation.html#local-playbooks](http://docs.ansible.com/ansible/latest/playbooks_delegation.html#local-playbooks)

Stack Overflow has similar comments:  
[https://stackoverflow.com/questions/18900236/run-command-on-the-ansible-host/18907395#18907395](https://stackoverflow.com/questions/18900236/run-command-on-the-ansible-host/18907395#18907395)

My playbook is really simple:

---

<div class="post-metadata">

**Author:** ![Charles\_De\_Silva](https://avatars.discourse-cdn.com/v4/letter/c/34f0e0/32.png) [@Charles\_De\_Silva](https://forum.ansible.com/u/Charles_De_Silva)\
**Post date:** [January 7, 2018, 1:15pm UTC](https://forum.ansible.com/t/running-code-on-local-ansible-controller/26433/2 "2018-01-07T13:15:03Z")

</div>

Sorry forgot to include version info:  
ansible-playbook 2.5.0 (devel 0b6c231ac1) last updated 2018/01/07 12:05:45 (GMT +100)

---

<div class="post-metadata">

**Author:** ![Charles\_De\_Silva](https://avatars.discourse-cdn.com/v4/letter/c/34f0e0/32.png) [@Charles\_De\_Silva](https://forum.ansible.com/u/Charles_De_Silva)\
**Post date:** [January 8, 2018, 8:28pm UTC](https://forum.ansible.com/t/running-code-on-local-ansible-controller/26433/3 "2018-01-08T20:28:11Z")

</div>

So I’ve played around a bit more with this, rebuilt my dev environment and still having the issue.

What I’ve discovered though is that it is the name of the module that seems to cause the problem.

I completely removed my module and just worked with the one from the tutorial and I’ve found that if I rename the test module from ‘my\_new\_test\_module.py’ to ‘asa\_my\_new\_test\_module.py’ and now even the dummy module tries to use ‘network\_cli’

Playbook used:

- name: test my new module  
connection: local  
hosts: localhost  
gather\_facts: no  
tasks:

- name: run the new module  
my\_new\_test\_module:  
name: ‘blah’  
new: true  
register: testout

- name: dump test output  
debug:  
msg: ‘{{ testout }}’

$ ansible-playbook my\_new\_test\_module.yml -vvvvv  
PLAYBOOK: my\_new\_test\_module.yml \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
1 plays in my\_new\_test\_module.yml

PLAY [test my new module] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
META: ran handlers

TASK [run the new module] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /home//tests/dummy\_playbook/my\_new\_test\_module.yml:7  
\<127.0.0.1\> ESTABLISH LOCAL CONNECTION FOR USER:

Rename the module to ‘asa\_my\_new\_test\_module.py’ and amend playbook:

- name: run the new module  
asa\_my\_new\_test\_module:  
name: ‘blah’  
new: true  
register: testout

$ ansible-playbook my\_new\_test\_module.yml -vvvvv  
PLAYBOOK: my\_new\_test\_module.yml \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
1 plays in my\_new\_test\_module.yml

PLAY [test my new module] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
META: ran handlers

TASK [run the new module] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /home//tests/dummy\_playbook/my\_new\_test\_module.yml:7  
\<127.0.0.1\> using connection plugin network\_cli  
\<127.0.0.1\> starting connection from persistent connection plugin  
\<127.0.0.1\> local domain socket does not exist, starting it

It only seems to matter if the ‘asa’ bit is at the beginning, for instance ‘my\_new\_asa\_test\_module.py’ works fine.

Am I understanding it correctly that the prefix of the module in some way determines which provider to use?

Same happens if I prefix with ios, nxos but not with panos so it doesn’t constant issue.

Hope all I’ve said above makes sense.

Would really appreciate if someone could explain why this is happening.

Thanks again

Charles

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [January 8, 2018, 9:09pm UTC](https://forum.ansible.com/t/running-code-on-local-ansible-controller/26433/4 "2018-01-08T21:09:36Z")

</div>

This behavior is defined by the following bit of code: [https://github.com/ansible/ansible/blob/3d0da8f0933fd7a7b233d28eb408a618c66eba70/lib/ansible/executor/task\_executor.py#L777](https://github.com/ansible/ansible/blob/3d0da8f0933fd7a7b233d28eb408a618c66eba70/lib/ansible/executor/task_executor.py#L777)

If the module name prefix matches one of the prefixes for the network modules from the config, it utilizes a different action plugin:

[https://github.com/ansible/ansible/blob/3d0da8f0933fd7a7b233d28eb408a618c66eba70/lib/ansible/config/base.yml#L1314](https://github.com/ansible/ansible/blob/3d0da8f0933fd7a7b233d28eb408a618c66eba70/lib/ansible/config/base.yml#L1314)

---

<div class="post-metadata">

**Author:** ![Charles\_De\_Silva](https://avatars.discourse-cdn.com/v4/letter/c/34f0e0/32.png) [@Charles\_De\_Silva](https://forum.ansible.com/u/Charles_De_Silva)\
**Post date:** [January 9, 2018, 8:29am UTC](https://forum.ansible.com/t/running-code-on-local-ansible-controller/26433/5 "2018-01-09T08:29:20Z")

</div>

Thanks Mark,

After posting I’d started looking into actions but wasn’t able to find the right area of code, thanks for the links.

Still a lot to learn.

Charles
