# Custom Connection Plugins - A new one: AWS Run Command

**URL:** <https://forum.ansible.com/t/custom-connection-plugins-a-new-one-aws-run-command/21270>\
**Category:** Ansible Developer\
**Tags:** aws\
**Created:** [April 25, 2016, 9:26pm UTC](https://forum.ansible.com/t/custom-connection-plugins-a-new-one-aws-run-command/21270 "2016-04-25T21:26:08Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fran\_Fitzpatrick](https://avatars.discourse-cdn.com/v4/letter/f/7ab992/32.png) [@Fran\_Fitzpatrick](https://forum.ansible.com/u/Fran_Fitzpatrick)\
**Post date:** [April 25, 2016, 9:26pm UTC](https://forum.ansible.com/t/custom-connection-plugins-a-new-one-aws-run-command/21270/1 "2016-04-25T21:26:08Z")

</div>

Hi everyone,

I’ve been working on a custom connection plugin to be used with AWS Run Command, allowing Ansible to be run natively on AWS instances that have the SSM agent installed and the proper IAM permissions/roles assigned. The major benefit of this for us is that internally we are using the SSM agent everywhere, and I no longer need to have access to the instance via SSH.

Aaaaanyway, I have a nice POC working beautifully with Ansible 2.0, and I am getting ready to port our plugin to Ansible 1.9, but it definitely looks like there was a ton of improvements made to connection plugins with v2.0.

Does anyone have any references on developing backwards compatible connection plugins?

Thanks,  
Fran

PS: First post! Woot!

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [April 25, 2016, 9:30pm UTC](https://forum.ansible.com/t/custom-connection-plugins-a-new-one-aws-run-command/21270/2 "2016-04-25T21:30:06Z")

</div>

None right now, we are still waiting to write the documentation to port plugins from 1.9 to 2.0 …

---

<div class="post-metadata">

**Author:** ![Sicheng\_Tang](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@Sicheng\_Tang](https://forum.ansible.com/u/Sicheng_Tang)\
**Post date:** [April 28, 2016, 2:26am UTC](https://forum.ansible.com/t/custom-connection-plugins-a-new-one-aws-run-command/21270/3 "2016-04-28T02:26:58Z")

</div>

Actually, I am working on implementing a custom HTTP connection plugin too. There are a lot of changes from 1.9 to 2.0 at the source code level, and I decided to implement my connection plugin on 2.0, which seems more object oriented.  
Did you already implemented a ASW Run Command plugin on 1.9? What word should I do, if I want to write my own connection plugin? Would you share your experiences?

在 2016年4月26日星期二 UTC+8上午5:26:08，Fran Fitzpatrick写道：

---

<div class="post-metadata">

**Author:** ![Fran\_Fitzpatrick](https://avatars.discourse-cdn.com/v4/letter/f/7ab992/32.png) [@Fran\_Fitzpatrick](https://forum.ansible.com/u/Fran_Fitzpatrick)\
**Post date:** [April 28, 2016, 2:50pm UTC](https://forum.ansible.com/t/custom-connection-plugins-a-new-one-aws-run-command/21270/4 "2016-04-28T14:50:00Z")

</div>

Hi Sicheng,

I created my plugin first with 2.0 as a proof of concept (meaning I know it works by running it a few times; probably not ready for production). When I backported it to 1.9 (still as a POC; I’m working on getting it production ready now), it landed up not being that hard. Here’s the big things that I’ve seen so far (the diff is FROM\_2.0 to TO\_1.9):

NOTE: I don’t consider my work production ready, so… take it with a grain of salt. 😛

-from ansible.plugins.connection import ConnectionBase  
-from ansible.utils.vars import combine\_vars  
+from ansible.callbacks import vv, vvv, vvvv, verbose # since Display doesn’t seem to be in 1.9

-try:

- from **main** import display  
-except ImportError:
- from ansible.utils.display import Display
- display = Display()

-class Connection(ConnectionBase):  
+class Connection(object):

# And because there is no ConnectionBase apparently, gotta get rid of those super() calls.

- def **init** (self, play\_context, new\_stdin, \*args, \*\*kwargs):
- super(Connection, self). **init** (play\_context, new\_stdin,
- \*args, \*\*kwargs)

- def **init** (self, runner, host, port, user, password, \*args, \*\*kwargs):

- def \_connect(self):

- def connect(self): # Looks like 1.9 needs a public connect method

- def exec\_command(self, cmd, in\_data=None, sudoable=True):
- ‘’’ run a command on the local host ‘’’
- super(Connection, self).exec\_command(cmd,
- in\_data=in\_data,
- sudoable=sudoable)
- display.vvv(“EXEC length {}”.format(len(cmd)))

- def exec\_command(self, cmd, tmp\_path=‘’, become\_user=None, sudoable=False,
- executable=None, in\_data=None):

| |
| | | |
| | | |

---

<div class="post-metadata">

**Author:** ![Fran\_Fitzpatrick](https://avatars.discourse-cdn.com/v4/letter/f/7ab992/32.png) [@Fran\_Fitzpatrick](https://forum.ansible.com/u/Fran_Fitzpatrick)\
**Post date:** [May 23, 2016, 2:28pm UTC](https://forum.ansible.com/t/custom-connection-plugins-a-new-one-aws-run-command/21270/5 "2016-05-23T14:28:31Z")

</div>

Hi all,

Just wanted to let you know that I’ve been making some great headway for this. Right now I have this connection plugin running on Linux hosts perfectly, and I have logic in the plugin to determine whether or not instance is Windows/Linux, dynamically determining whether or not to do “shell” things or “powershell” things.

However, I’m running into a problem with Windows. Even though my plugin has logic to determine whether or not an instance is Windows/Linux, the Ansible **framework** seems to always think it’s Linux. For example the first time the framework calls my exec\_command() function [on a Windows machine], it passes the command: “mkdir \<…\> && chmod \<…\> && echo \<…\>”

What am I missing here? How do I let the framework know, or identify somewhere within my connection plugin, that I’m expecting powershelly type things?

Thanks,  
Fran

---

<div class="post-metadata">

**Author:** ![Fran\_Fitzpatrick](https://avatars.discourse-cdn.com/v4/letter/f/7ab992/32.png) [@Fran\_Fitzpatrick](https://forum.ansible.com/u/Fran_Fitzpatrick)\
**Post date:** [May 23, 2016, 3:52pm UTC](https://forum.ansible.com/t/custom-connection-plugins-a-new-one-aws-run-command/21270/6 "2016-05-23T15:52:55Z")

</div>

So, it seems like what I was looking for was setting the `.default_shell` property. I landed up doing something like this, and things are now looking up:

@property  
def default\_shell(self):  
if self.platform\_type == ‘Windows’:  
return ‘powershell’  
else:  
return ‘sh’

---

<div class="post-metadata">

**Author:** ![Matthijs\_Suringa](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@Matthijs\_Suringa](https://forum.ansible.com/u/Matthijs_Suringa)\
**Post date:** [June 20, 2017, 2:38pm UTC](https://forum.ansible.com/t/custom-connection-plugins-a-new-one-aws-run-command/21270/7 "2017-06-20T14:38:07Z")

</div>

Hi Fran,

This sounds like quite a interesting option for me to look into, but unfortunately I am not a good enough developer to make something like this myself. Do you still have this plugin, and are you willing to share?

Thank you.

Regards,  
Matt

---

<div class="post-metadata">

**Author:** ![Fran\_Fitzpatrick](https://avatars.discourse-cdn.com/v4/letter/f/7ab992/32.png) [@Fran\_Fitzpatrick](https://forum.ansible.com/u/Fran_Fitzpatrick)\
**Post date:** [June 20, 2017, 2:42pm UTC](https://forum.ansible.com/t/custom-connection-plugins-a-new-one-aws-run-command/21270/8 "2017-06-20T14:42:23Z")

</div>

Matt,

Yeah, definitely! I’m actually trying to get the entire plugin upstreamed, but blocked until this other PR gets merged: [https://github.com/ansible/ansible/pull/25012](https://github.com/ansible/ansible/pull/25012)

The plugin (awsrun) works really well right now, but it is very slow. I’m hoping that once we get it upstreamed, we’ll be able to work on some performance improvements to it, but we’re still at the mercy of the AWS APIs. ☹

Fran

---

<div class="post-metadata">

**Author:** ![Matthijs\_Suringa](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@Matthijs\_Suringa](https://forum.ansible.com/u/Matthijs_Suringa)\
**Post date:** [June 20, 2017, 3:15pm UTC](https://forum.ansible.com/t/custom-connection-plugins-a-new-one-aws-run-command/21270/9 "2017-06-20T15:15:45Z")

</div>

Good to hear it’s still in business! I guess I’ll have to wait for the other work to be done

Thanks anyway!

Matt

---

<div class="post-metadata">

**Author:** ![Ting-Li\_Chou](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Ting-Li\_Chou](https://forum.ansible.com/u/Ting-Li_Chou)\
**Post date:** [September 17, 2018, 3:58pm UTC](https://forum.ansible.com/t/custom-connection-plugins-a-new-one-aws-run-command/21270/10 "2018-09-17T15:58:02Z")

</div>

Hi Fran,

Any good news for this nice plugin? Is it already in the upstream?

---

<div class="post-metadata">

**Author:** ![Fran\_Fitzpatrick](https://avatars.discourse-cdn.com/v4/letter/f/7ab992/32.png) [@Fran\_Fitzpatrick](https://forum.ansible.com/u/Fran_Fitzpatrick)\
**Post date:** [September 24, 2018, 1:33pm UTC](https://forum.ansible.com/t/custom-connection-plugins-a-new-one-aws-run-command/21270/11 "2018-09-24T13:33:40Z")

</div>

Hi Ting-Li,

I've actually changed jobs since working on this, and my current  
employer doesn't really use AWS so I haven't even attempted to  
upstream it.

Note: We did find some significant downsides to it, specifically AWS  
rate limiting us by always asking "Hey, is it done yet? Hey, is it  
done yet? Hey, is it done yet?" so I'd think there should probably be  
some smarter way of handling getting the responses from Run Command  
(Lambda? SNS?).

Fran
