# How the network operation happens in a module?

**URL:** <https://forum.ansible.com/t/how-the-network-operation-happens-in-a-module/15597>\
**Category:** Ansible Developer\
**Created:** [July 3, 2014, 1:01am UTC](https://forum.ansible.com/t/how-the-network-operation-happens-in-a-module/15597 "2014-07-03T01:01:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Wang](https://avatars.discourse-cdn.com/v4/letter/j/cdc98d/32.png) [@John\_Wang](https://forum.ansible.com/u/John_Wang)\
**Post date:** [July 3, 2014, 1:01am UTC](https://forum.ansible.com/t/how-the-network-operation-happens-in-a-module/15597/1 "2014-07-03T01:01:55Z")

</div>

Hi,

I’m a fresher in Ansible(actually all kinds of CMS), and trying to know how the modules works. I can easily understand the “time” example and even some more complex ones. However those involving operations over network,.e.g., _ **copy** _ module, confuse me a bit.

To my understanding, modules, who are pushed out of the ansible machine(A) to remote machines(B), are pieces of code running on B(hence obey the rules of the environment of B). It reports results back to A, and get deleted from B afterward. This means any operations over network in a modules are actually initiated on B, which also means, operation like _ **copy, template, synchronize** _,etc, are all pull-style operations, from the standpoint of the B. Is my understanding correct?

If the above understanding is correct. How that works on network operation in modules? I’ve checked [the code of](https://github.com/ansible/ansible/blob/devel/library/files/copy) the _ **copy** _ module, but find no such kind of code there, e.g., something like “scp foo@A:/the/src /dest/on/B” . There is a call on AnsibleModule.run\_command() which seems do the trick, but[it looks like](https://github.com/ansible/ansible/blob/devel/lib/ansible/module_utils/basic.py#L1162) just a simple local copy. I think my understanding has gone awry, but have no idea what it is. So I come here.

Thanks and Regards  
John

---

<div class="post-metadata">

**Author:** ![Chris\_Church1](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@Chris\_Church1](https://forum.ansible.com/u/Chris_Church1)\
**Post date:** [July 3, 2014, 2:29am UTC](https://forum.ansible.com/t/how-the-network-operation-happens-in-a-module/15597/2 "2014-07-03T02:29:19Z")

</div>

The network-related operations can all be found in the connection plugins (e.g. [https://github.com/ansible/ansible/blob/devel/lib/ansible/runner/connection\_plugins/ssh.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/runner/connection_plugins/ssh.py)). Each module is only concerned with its particular logic, and not how it is copied and executed on the remote machines.

Modules like copy are made up of code that runs on the control machine (implemented as action plugins, e.g. [https://github.com/ansible/ansible/blob/devel/lib/ansible/runner/action\_plugins/copy.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/runner/action_plugins/copy.py)) combined with code that runs on the remote machines (implemented in the modules themselves, e.g. [https://github.com/ansible/ansible/blob/devel/library/files/copy](https://github.com/ansible/ansible/blob/devel/library/files/copy)). Most modules, however, are implemented without action plugins.

Runner ([https://github.com/ansible/ansible/blob/devel/lib/ansible/runner/ **init**.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/runner/ __init__.py)) is responsible for pushing the module, running it and cleaning up afterward.

---

<div class="post-metadata">

**Author:** ![John\_Wang](https://avatars.discourse-cdn.com/v4/letter/j/cdc98d/32.png) [@John\_Wang](https://forum.ansible.com/u/John_Wang)\
**Post date:** [July 3, 2014, 3:14am UTC](https://forum.ansible.com/t/how-the-network-operation-happens-in-a-module/15597/3 "2014-07-03T03:14:52Z")

</div>

Ooh, That’s clear, I see. Thank you very much, Chris.

BTW, I guess those kind of information are in somewhere in the official document. Could you do me one more favor to point it out? I’d like to go through it thoroughly.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 3, 2014, 1:46pm UTC](https://forum.ansible.com/t/how-the-network-operation-happens-in-a-module/15597/4 "2014-07-03T13:46:08Z")

</div>

“BTW, I guess those kind of information are in somewhere in the official document.”

There are some good things to read about modules and action\_plugins in [http://docs.ansible.com/developing.html](http://docs.ansible.com/developing.html) – it’s not comprehensive, but it’s a start.

---

<div class="post-metadata">

**Author:** ![John\_Wang](https://avatars.discourse-cdn.com/v4/letter/j/cdc98d/32.png) [@John\_Wang](https://forum.ansible.com/u/John_Wang)\
**Post date:** [July 4, 2014, 2:28am UTC](https://forum.ansible.com/t/how-the-network-operation-happens-in-a-module/15597/5 "2014-07-04T02:28:20Z")

</div>

Thanks, Micheal,

I’ve gone through the modules part of the document, will head on the action parts. Chris has unveiled that there must be a cooperation between code running on control machine(i.e., plugin) and code running on remote machine(module itself) for some kind of modules(e.g., _ **copy** _ ) to function properly. I’ve not found any elaboration on this cooperation in the development document though. Anyway, Ansible has been good enough for most of work. I’m just curious on that development part.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 4, 2014, 4:51pm UTC](https://forum.ansible.com/t/how-the-network-operation-happens-in-a-module/15597/6 "2014-07-04T16:51:04Z")

</div>

Easiest would be to just read the ‘action\_plugins’ directory in the source tree, particularly how copy moves the file and the calls the ‘copy’ remote module, etc.

---

<div class="post-metadata">

**Author:** ![John\_Wang](https://avatars.discourse-cdn.com/v4/letter/j/cdc98d/32.png) [@John\_Wang](https://forum.ansible.com/u/John_Wang)\
**Post date:** [July 5, 2014, 3:34am UTC](https://forum.ansible.com/t/how-the-network-operation-happens-in-a-module/15597/7 "2014-07-05T03:34:22Z")

</div>

Thanks Michael.

Isn’t this a good practice on developing modules? I mean implementing a module by two parts(i.e., plugin and remote module) working together. A plugin is like kernel thing to me, which should be avoided in most case when developing a module I guess. Maybe for a new guy like me,I’ve gone way too far from the common cases.

Anyway I should read the source first. Thanks all you guys and don’t bother answer my question any more.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 7, 2014, 7:11pm UTC](https://forum.ansible.com/t/how-the-network-operation-happens-in-a-module/15597/8 "2014-07-07T19:11:41Z")

</div>

action\_plugins are most always not needed except for things that have some local behavior.

95% (approx) of our modules do not have or need them, and most people don’t even know they exist 🙂
