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"
Related
Iam just writing simple ansible playbook to run container getting error
This is my playbook code
---
- name: Create container
docker_container:
name: mydata
image: busybox
volumes:
- /data
Getting error like this.
ERROR! docker container' is not a valid attribute for a Play
Anybody help please.
You need to add some more lines to your playbook.
- name: Play name
hosts: your_hosts
tags: your tag
gather_facts: no|yes
tasks:
- name: Create container
docker_container:
name: mydata
image: busybox
volumes:
- /data
If you haven't yet, do:
ansible-galaxy collection install community.general where you are running the playbook (Ansible host).
Then, `ansible-playbook [your_playbook.yaml]``
Notice if you are using volume you may want to use docker_volume module to config it before the container starts. Also, try to map the volume, like - /data:/my/container/path so you can find it easier.
I am using ansible playbook to deploy my app with docker in Jenkins,
Recently I want to limit the log size of the app container,
with "docker run" has "–log-opt max-size=xxxm" option,
but How can I add the same function in ansible playbook?
I tried to set ansible playbook as below, but the test shows it not works.
- name: start container
docker_container:
name: "{{docker_container}}"
image: "{{docker_image}}"
log_options:
max-size: 256m
max-file: 2
volumes:
- "/etc/timezone:/etc/timezone"
- "/etc/localtime:/etc/localtime"
published_ports:
- "{{published_ports}}"
recreate: yes
The location of log file of container which I want to limit the size is as the file below:
/var/lib/docker/containers/2fdcae9fb35f4954639a3ff254873f0ae8518f2ede011807/2fdcae9fb35f4954639a3ff254873f0ae8518f2ede0118072304077918deb20f-json.log
Thanks for your reply first :)
Why not log to syslog?
Your ansible playbook would look like this:
- name: start container
docker_container:
name: "{{docker_container}}"
image: "{{docker_image}}"
volumes:
- "/etc/timezone:/etc/timezone"
- "/etc/localtime:/etc/localtime"
published_ports:
- "{{published_ports}}"
recreate: yes
log_driver: syslog
log_options:
tag: "{{docker_container}}"
syslog-facility: local4
And your syslog config could look like this (/etc/rsyslog.d/22-docker.conf):
local4.* /var/log/docker.log
In my case (Ubuntu 16.04) syslog is maintaining the log file size automatically.
I don't know wether you see this warning
[WARNING]: log_options is ignored when log_driver is not specified
you can try set log_driver:json-file like below
- name: Run docker container
docker_container:
name: "{{ docker_name }}"
image: "test/test:{{ image_tag }}"
log_driver: json-file
log_options:
max-size: 100m
recreate: yes
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!
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"
In ansible playbook docker parameter extra_host takes two parts host: ip_address. I am trying to pass the host and ipaddress in as variables. They are from prompt vars. The end result in my hosts file is: 1.2.3.4 {{server_hostname}}. Here is the code:
vars_prompt:
- name: "server_ip"
prompt: "Please enter the server IP address"
private: no
- name: "server_hostname"
prompt: "Please enter the server hostname"
private: no
tasks:
- name: Install Tomcat
docker:
image: tomcat:8.0
pull: missing
name: tomcat
state: restarted
ports:
- "8080:8080"
- "443:443"
extra_hosts:
"{{server_hostname}}": "{{server_ip}}"
I am new to ansible playbook any help would be greatly appreciated.
Make server_host_ip dict with set_fact before your task:
- set_fact:
server_host_ip: "{'{{host_name}}':'{{host_ip}}'}"
And use {{server_host_ip}} in the docker module.