How to use Docker link in Ansible when link var defined - docker

I want playbook that will start an container (in a task) and only link it to another container if the link is provided in a variable. For example:
- name: Start container
docker_container:
image: somerepo/app-server:{{ var_tag }}
name: odoo-server
state: started
log_opt: "tag=app-server-{{ var_tag }}"
expose:
- 8080
links:
- "{{ var_db_link }}"
when: var_db_link is defined
But of course this does not work. (I know - without a value is invalid ~ this is just pseudo code)
The whole task is actually quite a bit larger because it includes other directives so I really don't to have 2 versions of the task defined, one for starting with a link and another without.

when use '-', it means there is certain value , so I have a way to avoid it.
---
- hosts: localhost
tasks:
- name: Start container
docker_container:
image: centos
name: odoo-server
state: started
expose:
- 8080
links: "{{ var_db_link | default([]) }}"
then test it use
ansible-playbook ha.yml -e var_db_link="redis-master:centos"
ansible-playbook ha.yml
It runs normally!

Related

Ansible molecule using docker - how to specify memory limit

I have a molecule test which spins up 2 Docker containers, for testing 2 application versions at once.
dependency:
name: galaxy
driver:
name: docker
lint:
name: yamllint
platforms:
- name: molecule1
hostname: molecule1
image: "geerlingguy/docker-${MOLECULE_DISTRO:-centos7}-ansible:latest"
command: ${MOLECULE_DOCKER_COMMAND:-""}
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
privileged: true
pre_build_image: true
- name: molecule2
hostname: molecule2
image: "geerlingguy/docker-${MOLECULE_DISTRO:-centos7}-ansible:latest"
command: ${MOLECULE_DOCKER_COMMAND:-""}
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
privileged: true
pre_build_image: true
provisioner:
name: ansible
inventory:
host_vars:
molecule1:
app_version: "v1"
molecule2:
app_version: "v2"
lint:
name: ansible-lint
scenario:
name: default
converge_sequence:
- syntax
- lint
- create
- prepare
- converge
- idempotence
- verify
verifier:
name: goss
lint:
name: yamllint
I am looking for a way to specify the memory like -m or --memory= as described here.
I understand that molecule makes use of the docker_container ansible module, which support the memory parameter, but somehow I cannot find a way to make this work in molecule.
Any ideas how to accomplish this?
PS: My guess is that this parameter is not yet implemented in molecule, if my assumption is correct that this is the implementation.
Thanks in advance.
++Update++
--memory is indeed not yet implemented in molecule docker provisioner.
If anybody is interested, here is the relevant change to the source code:
diff --git a/molecule/provisioner/ansible/playbooks/docker/create.yml b/molecule/provisioner/ansible/playbooks/docker/create.yml
index 7a04b851..023a720a 100644
--- a/molecule/provisioner/ansible/playbooks/docker/create.yml
+++ b/molecule/provisioner/ansible/playbooks/docker/create.yml
## -121,6 +121,8 ##
hostname: "{{ item.hostname | default(item.name) }}"
image: "{{ item.pre_build_image | default(false) | ternary('', 'molecule_local/') }}{{ item.image }}"
pull: "{{ item.pull | default(omit) }}"
+ kernel_memory: "{{ item.kernel_memory | default(omit) }}"
+ memory: "{{ item.memory | default(omit) }}"
state: started
recreate: false
log_driver: json-file
My fork has now been merged to Molecule.

start docker container several ports

I need to start a docker container with several port mapping as following:
- name: Run My container
docker_container:
name: "MyContainer"
image: "MyImage"
state: present
pull: true
restart_policy: always
published_ports:
- 1200:1200
- 1201:1201
- 1202:1202
- 1203:1203
.
.
.
- 1300:1300
What I want to do is to run Ansible script as:
- name: Run My container
docker_container:
name: "MyContainer"
image: "MyImage"
state: present
pull: true
restart_policy: always
published_ports:
- 1200-1300:1200-1300
Although, It doesn't work, Ansible give me the following error message:
File \"/tmp/ansible_8zDYC9/ansible_module_docker_container.py\", line 987, in _parse_publish_ports\r\n container_port = int(parts[-1])\r\nValueError: invalid literal for int() with base 10: '1200-1300'
Is possible to map several ports betwen host and container in the same line?
PS: I'm using Ansible 2.3
I am not sure if I have the conclusion but u need to set the ports in double quotes like these:
"1200-1300:1200-1300"

building docker container with startup scripts via ansible

I am trying to build docker container which should include startup scripts in container's /etc/my_init.d directory via ansible. I have difficulty finding any documentation how to do this. Here is relevant portion of my yaml file:
- name: Create container
docker:
name: myserver
image: "{{ docker_repo }}/myserver:{{ server.version }}"
state: started
restart_policy: always
docker_api_version: 1.18
registry: "{{ docker_repo }}"
username: "{{ registry_user }}"
password: "{{ registry_password }}"
links:
- "mywebservices"
ports:
- "8000:8000"
- "9899:9899"
volumes:
- "{{ myserver_home_dir }}/logs:/var/log/my_server"
env:
MY_ENVIRONMENT: "{{ my_environment }}"
when: myserver_action == "create"
or (myserver_action == "diff-create" and myserver.changed)
or myserver_action == "update"
What should I add in here to tell ansible to put my files into container's /etc/my_init.d during build?
First of all, you can't build container (you can start it), you build images.
Second, docker module is deprecated, use docker_image to build images.
You should copy your files into build directory (with copy or synchronize modules), for example:
/tmp/build
Then create Dockerfile that will take them from build directory and add into your image.
After that call docker_image:
docker_image:
path: /tmp/build
name: myimage
Finally start your container:
docker_container:
image: myimage
name: mycontainer
Unsure if it's relevant, as I don't know what your startup Ansible content is doing, but it's probably worth looking at the Ansible Container project.
https://github.com/ansible/ansible-container
You can build your container images using Ansible roles instead of a Dockerfile, orchestrate them locally, and deploy them to production Kubernetes or Red Hat OpenShift.

Write conditional docker properties in Ansible module

I'm trying to write conditional properties in the Ansible module docker_container. For example adding log_options when a var is set. Any idea? Ansible doesn't seem very elastic in this task.
name: vault
become: yes
docker_container:
name: vault
image: vault
state: started
restart: yes
network_mode: host
command: server
capabilities:
- IPC_LOCK
You may want to read about omitting parameters:
docker_container:
...
log_options: "{{ my_log_opts | default(omit) }}"
...

Selenium node/chrome docker image and selenium/hub docker image in different host machines

I have a situation where i have to use the node/chrome and selenium/hub images in different host machines. However problem is although i am linking them in the ansible role as below:
- name: seleniumchromenode container
docker:
name: seleniumhubchromenode
image: "{{ seleniumchromenode_image }}"
state: "{{ 'started' }}"
pull: always
restart_policy: always
links: seleniumhub:hub
It doesnt get linked , or in other words the hub is not discovering the node. Please let me know if linking works only when the hub and node are within the same host machine.
Links don't work across machines. You can either specify the IP address/hostname and let it connect through that, or you can use Docker Swarm Mode to deploy your containers - that lets you do something very close to linking (it sets up a mesh network across the swarm nodes, so services can find each other).
Simplest: just pass the hostname in Ansible.
Below is what finally worked for me. Note that the SE_OPTS is necessary for the node to be able to link successfully to the hub that is on a different host.
- name: seleniumchromenode container
docker_container:
name: seleniumhubchromenode
image: "{{ seleniumchromenode_image }}"
state: "{{ 'started' }}"
pull: true
restart_policy: always
exposed_ports:
- "{{seleniumnode_port}}"
published_ports:
- "{{seleniumnode_port}}:{{seleniumnode_port}}"
env:
HUB_PORT_4444_TCP_ADDR: "{{seleniumhub_host}}"
HUB_PORT_4444_TCP_PORT: "{{seleniumhub_port}}"
SE_OPTS: "-host {{seleniumnode_host}} -port {{seleniumnode_port}}"
NODE_MAX_INSTANCES: "5"
NODE_MAX_SESSION: "5"

Resources