# Ansible running in Docker Container"Connection refused Failed to connect to new control master"

**URL:** <https://forum.ansible.com/t/ansible-running-in-docker-container-connection-refused-failed-to-connect-to-new-control-master/30719>\
**Category:** Ansible Project\
**Created:** [July 6, 2019, 6:30pm UTC](https://forum.ansible.com/t/ansible-running-in-docker-container-connection-refused-failed-to-connect-to-new-control-master/30719 "2019-07-06T18:30:45Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Claudio\_Rivas](https://avatars.discourse-cdn.com/v4/letter/c/a5b964/32.png) [@Claudio\_Rivas](https://forum.ansible.com/u/Claudio_Rivas)\
**Post date:** [July 6, 2019, 6:30pm UTC](https://forum.ansible.com/t/ansible-running-in-docker-container-connection-refused-failed-to-connect-to-new-control-master/30719/1 "2019-07-06T18:30:45Z")

</div>

Hi,

I’m trying to connect to remote host through SSH and a Private Key to run a simple test to start working with Ansible to generate my inventories:

ansible -m ping -i hosts test1 -vvv

Hosts file have a simple configuration

[test]

test1 ansible\_host=remote\_host ansible\_user=remote\_user ansible\_private\_key\_file=/var/jenkins\_home/ansible/id\_rsa

SSH is working fine to connect to remote server with this key:

jenkins@0bde7f6019a2:~/ansible$ ssh -i id\_rsa remote\_user@remote\_host

Enter passphrase for key ‘id\_rsa’:

Last login: Fri Jul 5 12:10:51 2019 from jenkins.jenkins\_net

While trying to do the connection with ansible seems to have problems I can’t find or troubleshoot, the error indicates:

**test1 | UNREACHABLE! =\> {**

**“changed”: false,**

**“msg”: “Failed to connect to the host via ssh: Control socket connect(/var/jenkins\_home/.ansible/cp/45a4bb8b49): Connection refused\r\nFailed to connect to new control master”,**

**“unreachable”: true**

**}**

Any help will or advice be appreciated.

Best regards,

---

<div class="post-metadata">

**Author:** ![Indian\_Velumani1](https://avatars.discourse-cdn.com/v4/letter/i/f1d935/32.png) [@Indian\_Velumani1](https://forum.ansible.com/u/Indian_Velumani1)\
**Post date:** [July 6, 2019, 6:32pm UTC](https://forum.ansible.com/t/ansible-running-in-docker-container-connection-refused-failed-to-connect-to-new-control-master/30719/2 "2019-07-06T18:32:49Z")

</div>

Hi,

Run docker command with --net=host.

Thanks.

---

<div class="post-metadata">

**Author:** ![Claudio\_Rivas](https://avatars.discourse-cdn.com/v4/letter/c/a5b964/32.png) [@Claudio\_Rivas](https://forum.ansible.com/u/Claudio_Rivas)\
**Post date:** [July 6, 2019, 6:48pm UTC](https://forum.ansible.com/t/ansible-running-in-docker-container-connection-refused-failed-to-connect-to-new-control-master/30719/3 "2019-07-06T18:48:49Z")

</div>

Thank you for your quick response, but where do you suggest to run it? In my case the element that has Ansible is a Docker container. Docker container have Jenkins and Ansible.  
Ansible script doesn’t run any Docker command actually. Sorry is my question sounds dumb but I’m new to Docker and Ansible.

Best regards,

---

<div class="post-metadata">

**Author:** ![Indian\_Velumani1](https://avatars.discourse-cdn.com/v4/letter/i/f1d935/32.png) [@Indian\_Velumani1](https://forum.ansible.com/u/Indian_Velumani1)\
**Post date:** [July 6, 2019, 7:06pm UTC](https://forum.ansible.com/t/ansible-running-in-docker-container-connection-refused-failed-to-connect-to-new-control-master/30719/4 "2019-07-06T19:06:55Z")

</div>

Hi,

When want to start a docker image as container means you have run a command like  
docker run -d (image: version).  
On there just add like this  
docker run --net=host -d (image: version)

Then login to the docker using  
docker exec command  
Then execute your ansible playbook.

---

<div class="post-metadata">

**Author:** ![Robert\_Williams](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@Robert\_Williams](https://forum.ansible.com/u/Robert_Williams)\
**Post date:** [July 6, 2019, 8:20pm UTC](https://forum.ansible.com/t/ansible-running-in-docker-container-connection-refused-failed-to-connect-to-new-control-master/30719/5 "2019-07-06T20:20:05Z")

</div>

Is your ssh key password protected and if so did you configured forwarding in the ssh-agent or a similar work around?

---

<div class="post-metadata">

**Author:** ![Claudio\_Rivas](https://avatars.discourse-cdn.com/v4/letter/c/a5b964/32.png) [@Claudio\_Rivas](https://forum.ansible.com/u/Claudio_Rivas)\
**Post date:** [July 6, 2019, 9:08pm UTC](https://forum.ansible.com/t/ansible-running-in-docker-container-connection-refused-failed-to-connect-to-new-control-master/30719/6 "2019-07-06T21:08:30Z")

</div>

Thank you, yes I have generated the key with a password, but didn’t configured it for the Ansible connection, now I’m searching how to do it, but if you have an example will be helpful.

Thank you again

---

<div class="post-metadata">

**Author:** ![Robert\_Williams](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@Robert\_Williams](https://forum.ansible.com/u/Robert_Williams)\
**Post date:** [July 6, 2019, 9:30pm UTC](https://forum.ansible.com/t/ansible-running-in-docker-container-connection-refused-failed-to-connect-to-new-control-master/30719/7 "2019-07-06T21:30:28Z")

</div>

I do not currently have an example of this or an environment to test it in, but a quick search turned up:  
[https://www.calazan.com/using-ssh-agent-forwarding-with-ansible/](https://www.calazan.com/using-ssh-agent-forwarding-with-ansible/)

You could also regenerate the key without a passphrase or fallback to UN/PW auth.

Out of curiosity are you running Jenkins and ansible in the same container or separate?

---

<div class="post-metadata">

**Author:** ![Claudio\_Rivas](https://avatars.discourse-cdn.com/v4/letter/c/a5b964/32.png) [@Claudio\_Rivas](https://forum.ansible.com/u/Claudio_Rivas)\
**Post date:** [July 6, 2019, 10:04pm UTC](https://forum.ansible.com/t/ansible-running-in-docker-container-connection-refused-failed-to-connect-to-new-control-master/30719/8 "2019-07-06T22:04:29Z")

</div>

Same container.  
Generating the certificate without passwords makes problems with ssh server.

---

<div class="post-metadata">

**Author:** ![Robert\_Williams](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@Robert\_Williams](https://forum.ansible.com/u/Robert_Williams)\
**Post date:** [July 6, 2019, 11:06pm UTC](https://forum.ansible.com/t/ansible-running-in-docker-container-connection-refused-failed-to-connect-to-new-control-master/30719/9 "2019-07-06T23:06:02Z")

</div>

I understand and do not disagree.

I would try the process noted on that link. Keep in mind that because you are in a container, you will need to add those to your dockerfile and reaping the container.

I’m presently using a main Jenkins container that starts ephemeral ansible/jnlp containers as executors which works great.

---

<div class="post-metadata">

**Author:** ![Claudio\_Rivas](https://avatars.discourse-cdn.com/v4/letter/c/a5b964/32.png) [@Claudio\_Rivas](https://forum.ansible.com/u/Claudio_Rivas)\
**Post date:** [July 6, 2019, 11:17pm UTC](https://forum.ansible.com/t/ansible-running-in-docker-container-connection-refused-failed-to-connect-to-new-control-master/30719/10 "2019-07-06T23:17:03Z")

</div>

Thanks for the hint, I’ll start moving on with your suggestions. I’ll come back to documento t this and let you know.

---

<div class="post-metadata">

**Author:** ![Claudio\_Rivas](https://avatars.discourse-cdn.com/v4/letter/c/a5b964/32.png) [@Claudio\_Rivas](https://forum.ansible.com/u/Claudio_Rivas)\
**Post date:** [July 8, 2019, 12:40pm UTC](https://forum.ansible.com/t/ansible-running-in-docker-container-connection-refused-failed-to-connect-to-new-control-master/30719/11 "2019-07-08T12:40:02Z")

</div>

Hi Robert, thanks for the advice and the link. I’ve tried the mentioned method and noticed there’s a directory known as "cp"on the route “/var/jenkins\_home/.ansible” (yes, with a dot) where a file is generated while trying to connect to the remote server where generates a file named “45a4bb8b49”.  
Is important to say that my working directory is “/var/jenkins\_home/ansible (yes without a dot)”

So I’ve Changing the “ansible.cfg” file with the to add -o ControlPath=/home/jenkins/.ansible/cp/ansible-ssh-%h-%p-%r  
After that I’ve noticed that the file was too long so I used the control\_path\_dir=/var/jenkins\_home/.ansible/cp  
So I’ve placed the configuration as -o ControlPath=%(directory)s/ansible-ssh-%h-%p-% now do not recognize something in the path and is returning:

**test1 | UNREACHABLE! =\> {**

**“changed”: false,**

**“msg”: “Failed to connect to the host via ssh: percent\_expand: unknown key %(”,**

**“unreachable”: true**

**}**

All this is making me think about the ssh server side and the way it’s configured the ssh connection by default for ansible.

Stay tunned,

---

<div class="post-metadata">

**Author:** ![Claudio\_Rivas](https://avatars.discourse-cdn.com/v4/letter/c/a5b964/32.png) [@Claudio\_Rivas](https://forum.ansible.com/u/Claudio_Rivas)\
**Post date:** [July 8, 2019, 5:59pm UTC](https://forum.ansible.com/t/ansible-running-in-docker-container-connection-refused-failed-to-connect-to-new-control-master/30719/12 "2019-07-08T17:59:46Z")

</div>

Ok the thing now is running, seems it needs to store on a temporal repository, mine was pointing to /var/jenkins/.ansible, so y generated a ansible.cfg with this configuration on the same folder where my hosts file is:

ssh\_args = -C -o ControlMaster=auto -o ControlPersist=60s  
control\_path = /dev/shm/cp%%h-%%p-%%r

So pointing the control path to /dev/shm do the work, don’t forget to use -C on the ssh\_args because it seems to use a particular compresion used for the ssh connection.

If iI’m wrong with something any feedback is appreciated.

Best regards,

---

<div class="post-metadata">

**Author:** ![Robert\_Williams](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@Robert\_Williams](https://forum.ansible.com/u/Robert_Williams)\
**Post date:** [July 8, 2019, 6:30pm UTC](https://forum.ansible.com/t/ansible-running-in-docker-container-connection-refused-failed-to-connect-to-new-control-master/30719/13 "2019-07-08T18:30:05Z")

</div>

Happy to hear its working for you!

ssh-agent likely gets a little tricky when in the container. I can’t speak to a better way to do this as I havent had to overcome the Passphrase-protected keys yet. That said, if you continue to have issues, let me know what OS Base you are using for the containers and I could spin up a test lab.

Kind Regards,  
Robert Williams

---

<div class="post-metadata">

**Author:** ![Jesus\_David\_Posada\_E](https://avatars.discourse-cdn.com/v4/letter/j/b9e5f3/32.png) [@Jesus\_David\_Posada\_E](https://forum.ansible.com/u/Jesus_David_Posada_E)\
**Post date:** [August 25, 2020, 6:06pm UTC](https://forum.ansible.com/t/ansible-running-in-docker-container-connection-refused-failed-to-connect-to-new-control-master/30719/14 "2020-08-25T18:06:18Z")

</div>

At the same level where the host inventory is, create a file named ansible.cfg with the following:

[ssh\_connection]  
ssh\_args = -C -o ControlMaster=auto -o ControlPersist=60s  
control\_path = /dev/shm/cp%%h-%%p-%%r

That will solve your problem
