Docker Community Forums

Share and learn in the Docker community.

Using ansible to manage services

(Raúl Cruz) #1

I’m trying to manage services via Ansible but I’m getting an error because python is not installed in the manager machines.
I’ve tried to install via sources but I’m getting this error:
~/Python-2.7.12 $ ./configure
-sh: can’t fork

How can I install it?

Edit: I’ve seen that apk is available to install packages but I need root permissions, how can I get it?

(Michael Friis) #2

Generally, you should try to run stuff in containers. Eg. you can grab a container on Docker for AWS with the userland you want. You can even volume-mount the Docker socket if you need your python script to start and stop containers.

Alternatively, you can setup a SSH tunnel to your local environment (which I suspect has Python) and run you script against your Docker for AWS swarm:

(Raúl Cruz) #3

I want to use Ansible as automation tool to create, scale and remove services (Ansible will launch docker service create, docker service rm, etc).
Ansible uses modules, some of those modules are written in Python so Ansible needs python in the manager machine to be able to execute his own scripts.
Regarding to your answer I don’t know how to fit a python container with my needs, can you show me an example?

(Raúl Cruz) #4

I’ve figured out how to use Ansible without Python, there is a module called raw module that allows bash commands execution, so there is no need for python. Anyway it will be great to use python modules.

Thank you for your answer.

(Michael Friis) #5

How about running ansible in a container?

docker run -ti williamyeh/ansible:1.9-ubuntu12.04 bash

(Raúl Cruz) #6

I don’t want to execute Ansible inside the manager, I want to run Ansible from jenkins to create a friendly CLI to create, remove or scale services.

(Michael Friis) #7

Got it, thanks - out of curiosity, what is it that you would like to run on the manager?

(Raúl Cruz) #8

I want to execute this command:
docker service update --image latest redis

Ansible has playbooks, so if I configure manager ssh connection within the playbook and I set this command as a task, I can execute from jenkins:
ansible-playbook redis-update.yml
And it will connect via ssh and executes the service update command in the manager.

(Michael Friis) #9

Alright, so it looks like the only thing to be executed on the manager is docker service update --image latest redis - why is Python required for that (sorry if I’m being dense, just trying to understand your use case so that we can better support it).

(Raúl Cruz) #10

No problem.

Python is required by Ansible playbooks and modules to be present in remote machines. i.e.: Ansible has a core module called ping written in python, so when you execute ansible -m ping, Ansible executes an internal python script in the remote machine to make ping and get a response.

(Kiview) #11

Using Ansible inside a container works really well for this use case (we use it like this all the time). You can even have your own Ansible image which will contain all your roles and playbooks. You’ll then use this container to run Ansible commands, so the container is not a long living service but more like a CLI tool.

Bundle python in the ami for devops capability
(Raúl Cruz) #12

Thank you for your answer.
I will try your solution which is similar to Michael’s solution.

Edit: How do you connect to the manager? Using some CI tool, manually?

(Raúl Cruz) #13

I forgot to thank you for your answers.

(V1k0d3n) #14

you could use the raw module to install python and then use the rest of the modules like normal. xenial removed python 2.7, so you need to add it back in order for ansible to work again (trusty does’t have this issue). you use the raw module to ‘apt-get install -y python’ when given a variable (i use bootstrap_os: xenial in my group_vars file), and then you can use ansible as normal for everything else.

not sure if this helps, or if you’ve already solved it. sorry if this didn’t provide any extra help.

(Raúl Cruz) #15

Docker for AWS AMI does not provide permissions to install via apt-get, yum, aptitude or apk so this solution is not possible :sob:

(V1k0d3n) #16

ah, i misunderstood entirely.

(Michael Friis) #17

Running Ansible in the container sounds like a good solution. I guess it’s similar to what’s outlined here?

(Kiview) #18

Exactly, this works really well.