How to use docker_image and docker_container module in Ansible - docker

I would like to make a simple script that can deploy and download docker image from docker hub using Ansible but when I am trying to execute mycode I experience some errors as follows:
PLAY [manage docker using ansible] *********************************************
TASK [Gathering Facts] *********************************************************
ok: [controller]
ok: [worker]
TASK [ping hosts] **************************************************************
ok: [controller]
ok: [worker]
TASK [pull docker image] *******************************************************
fatal: [controller]: FAILED! => {"changed": false, "msg": "Unsupported parameters for (docker_image) module: source Supported parameters include: api_version,archive_path,buildargs,cacert_path,cert_path,container_limits,debug,docker_host,dockerfile,filter_logger,force,http_timeout,key_path,load_path,name,nocache,path,pull,push,repository,rm,ssl_version,state,tag,timeout,tls,tls_hostname,tls_verify,use_tls"}
fatal: [worker]: FAILED! => {"changed": false, "msg": "Unsupported parameters for (docker_image) module: source Supported parameters include: api_version,archive_path,buildargs,cacert_path,cert_path,container_limits,debug,docker_host,dockerfile,filter_logger,force,http_timeout,key_path,load_path,name,nocache,path,pull,push,repository,rm,ssl_version,state,tag,timeout,tls,tls_hostname,tls_verify,use_tls"}
to retry, use: --limit #/home/testuser/docker_manage.retry
PLAY RECAP *********************************************************************
controller : ok=2 changed=0 unreachable=0 failed=1
worker : ok=2 changed=0 unreachable=0 failed=1
here is my code:
---
- name: manage docker using ansible
hosts: all
become: true
tasks:
- name: ping hosts
ping:
- name: pull docker image
docker_image:
name: busybox
source: pull
- name: deploy container
docker_container:
name: first_container
image: busybox
state: present
I have tried to follow guide on this [link] (https://docs.ansible.com/ansible/latest/scenario_guides/guide_docker.html) to config my ansible.cfg file.

Unsupported parameters for (docker_image) module: source
Although you did not provide your ansible version in your question, I'm quite sure you need to upgrade (or to remove this parameter). The source parameter was added in ansible 2.8
Ref: https://docs.ansible.com/ansible/latest/modules/docker_image_module.html#parameter-source

Related

Jenkins ERROR: script returned exit code 4

I got a simple Groovy script to install agents on my servers using Ansible.
After I run the pipeline I get error about
ERROR: script returned exit code 4
Finished: FAILURE
The error happens because I have two instances not running (I don't want them running) and I get connection time out from them.
Is there a way to get Jenkins to ignore such errors?
A not-so-ideal solution would be to just state ignore_unreachable: yes at the top of you playbook.
This is no ideal because you risk missing on unreachable hosts you do care about.
A possibly better solution would be to gracefully end those unreachable hosts in a meta task based on a list of host(s) you don't need up and running.
For example:
- hosts: localhost, ok-if-down
gather_facts: no
pre_tasks:
- ping:
ignore_unreachable: yes
register: ping
- meta: end_host
when:
- inventory_hostname in _possibly_unreachable_hosts
- ping is unreachable
vars:
_possibly_unreachable_hosts:
- ok-if-down
## add more host(s) name in this list, here
tasks:
## here goes your current tasks
When run, the exit code of this playbook would be 0:
$ ansible-playbook play.yml; echo "Return code is $?"
PLAY [localhost, ok-if-down] **************************************************
TASK [ping] *******************************************************************
fatal: [ok-if-down]: UNREACHABLE! => changed=false
msg: 'Failed to connect to the host via ssh: ssh: Could not resolve hostname ok-if-down: Name does not resolve'
skip_reason: Host ok-if-down is unreachable
unreachable: true
ok: [localhost]
TASK [meta] *******************************************************************
skipping: [localhost]
TASK [meta] *******************************************************************
PLAY RECAP ********************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ok-if-down : ok=0 changed=0 unreachable=1 failed=0 skipped=1 rescued=0 ignored=0
Return code is 0

Failing to start nginx container when volumes is used (using ansible and docker-compose)

I am trying to start an nginx container using ansible with docker-compose from one machine to a different machine.
Whenever I include nginx.conf to the volumes, there is an error which I do not understand. The container is only created but not starting.
MACHINE-1
Command to run the playbook: ansible-playbook -v nginx-playbook.yml -l ubuntu_node_1 -u root
my playbook:
- name: nginx-docker_compose
hosts: all
gather_facts: yes
become: yes
tasks:
- community.general.docker_compose:
project_name: nginx
definition:
version: '2'
services:
web:
image: nginx:latest
volumes:
- ./vars/nginx.conf:/etc/nginx/nginx.conf:ro
ports:
- "8080:80"
[EDITED]
Here is the error:
Using /etc/ansible/ansible.cfg as config file
PLAY [nginx-docker_compose] ********************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************
[DEPRECATION WARNING]: Distribution Ubuntu 18.04 on host 172.31.15.176 should use /usr/bin/python3, but is using /usr/bin/python for backward compatibility with prior
Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. This feature will be removed in version 2.12. Deprecation
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
ok: [172.31.15.176]
TASK [community.general.docker_compose] ********************************************************************************************************************************
fatal: [172.31.15.176]: FAILED! => {"changed": false, "errors": [], "module_stderr": "Recreating nginx_web_1 ... \n\u001b[1A\u001b[2K\nRecreating nginx_web_1 ... \n\u001b[1B", "module_stdout": "", "msg": "Error starting project Encountered errors while bringing up the project."}
PLAY RECAP *************************************************************************************************************************************************************
172.31.15.176 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
[root#ip-172-31-12-130 docker_server]# ansible-playbook -v nginx-playbook.yml -l ubuntu_node_1 -u root
Using /etc/ansible/ansible.cfg as config file
PLAY [nginx-docker_compose] ********************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************
[DEPRECATION WARNING]: Distribution Ubuntu 18.04 on host 172.31.15.176 should use /usr/bin/python3, but is using /usr/bin/python for backward compatibility with prior
Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. This feature will be removed in version 2.12. Deprecation
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
ok: [172.31.15.176]
TASK [community.general.docker_compose] ********************************************************************************************************************************
fatal: [172.31.15.176]: FAILED! => {"changed": false, "errors": [], "module_stderr": "Recreating 9b102bbf98c2_nginx_web_1 ... \n\u001b[1A\u001b[2K\nRecreating 9b102bbf98c2_nginx_web_1 ... \n\u001b[1B", "module_stdout": "", "msg": "Error starting project Encountered errors while bringing up the project."}
PLAY RECAP *************************************************************************************************************************************************************
172.31.15.176 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
NOTE: When I try to run nginx container directly using docker-compose with the same config on MACHINE-2, it works.
I believe there are some permission issues happening while trying to execute the playbook from MACHINE-1 to MACHINE-2 but can not figure it out.
It works now. Thanks to #mdaniel.
Things I changed:
I wrote the entire directory in the playbook- /home/some_more_folders/nginx.conf
and copied the same file with same directory structure on the destination machine.
Still open questions:
Any idea why is it necessary to copy any file to the destination machine (such as nginx.conf)?
How this manual process of copying of config files to destination machine for docker-compose be automated?

Ansible Skipping Docker Build

Trying to get Ansible set up to learn about it, so could be a very simple mistake but I can't find the answer to it anywhere. When I try to run ansible-playbook it's just simply skipping the job with the following output:
ansible-playbook -i hosts simple-devops-image.yml --check
PLAY [all] ***********************************************************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************************************************************************************************************************************************************
[WARNING]: Platform linux on host 127.0.0.1 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more
information.
ok: [127.0.0.1]
TASK [build docker image using war file] *****************************************************************************************************************************************************************************************************************************************************
skipping: [127.0.0.1]
PLAY RECAP ***********************************************************************************************************************************************************************************************************************************************************************************
127.0.0.1 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
My .yml playbook file:
---
- hosts: all
become: yes
tasks:
- name: build docker image using war file
command: docker build -t simple-devops-image .
args:
chdir: /usr/local/src
My hosts file:
[localhost]
127.0.0.1 ansible_connection=local
command module is skipped when executing with check mode. Remove —check from ansible-playbook command to build docker image.
Here is a note from the doc:
Check mode is supported when passing creates or removes. If running in check mode and either of these are specified, the module will check for the existence of the file and report the correct changed status. If these are not supplied, the task will be skipped.

Launching Multiple Docker Containers using Ansible

I am trying to build an image and trying to launch multiple docker containers using ansible playbook. I am not able to understand how do i publish the ports. The below playbook gives me an error which is quite obvious that the port is already allocated but then how do i achieve this because from outside the containers there will only be one port right to acces all the containers?
Playbook -
- name: Manage Docker instances via Ansible
hosts: shashank-VM
connection: local
become: yes
become_method: sudo
tasks:
- name: Building an image from Dockerfile
docker_image:
build:
path: .
pull: yes
name: web_new
source: build
- name: Creation of Docker Containers
docker_container:
name: my-app-{{ item }}
image: web_new
state: present
ports:
- "79:80"
with_sequence: count=3
- name: Starting Docker Containers
docker_container:
name: my-app-{{ item }}
image: web_new
state: started
with_sequence: count=3
Error -
changed: [shashank-VM]
TASK [Creation of Docker Containers] *********************************************************************************************************
changed: [shashank-VM] => (item=1)
changed: [shashank-VM] => (item=2)
changed: [shashank-VM] => (item=3)
TASK [Starting Docker Containers] ************************************************************************************************************
changed: [shashank-VM] => (item=1)
failed: [shashank-VM] (item=2) => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "ansible_loop_var": "item", "changed": false, "item": "2", "msg": "Error starting container beb7f1d204f47862d16722f70b812df7193ddacf12d15350a9095cec2ebf4d85: 500 Server Error: Internal Server Error (\"driver failed programming external connectivity on endpoint my-app-2 (880c06fe9e2efa75537e350734be1d46d0cc76e7acf70733d19ad38706dde5ab): Bind for 0.0.0.0:78 failed: port is already allocated\")"}
failed: [shashank-VM] (item=3) => {"ansible_loop_var": "item", "changed": false, "item": "3", "msg": "Error starting container 048f2f3ea6fed5e094fdf59a4650b2b3f8164d804ee7dc8875e6e95bda1300d7: 500 Server Error: Internal Server Error (\"driver failed programming external connectivity on endpoint my-app-3 (8247f75384b240cb9bf1ee66cc9f0404df5465e6c08903304f14bd813c218fa1): Bind for 0.0.0.0:78 failed: port is already allocated\")"}
NOTE : I have an application for which I am building an image and there will be multiple containers running for that image. How do i accessible my application from outside? How do i work on the ports?
Any help is appreciated
The cause of the issue is here:
> Bind for 0.0.0.0:78 failed: port is already allocated
Check what application/container blocks port 78
You can do it with ss:
sudo ss -plunt | grep :78
Or with lsof:
lsof -i :78
Or with fuser:
fuser -v -n tcp 78

using ansible with docker-compose

I am trying to deploy a docker setup using Ansible playbook. For this, I am using docker_service.
My Playbook looks like:
---
- name: Run Docker compose
hosts: all
gather_facts: no
tasks:
- debug: msg="Container - {{ inventory_hostname }}"
- docker_service:
project_src: "compose"
state: absent
- docker_service:
project_src: "compose"
state: present
Upon running this simple playbook as:
ansible-playbook -v playbook.yml --ask-sudo-pass
I added --ask-sudo-pass to ensure that it was not a permission issue.
OUTPUT
SUDO password:
PLAY [Run Docker compose] ******************************************************
TASK [debug] *******************************************************************
ok: [prolims-staging] => {
"msg": "Container - prolims-staging"
}
TASK [docker_service] **********************************************************
fatal: [prolims-staging]: FAILED! => {"changed": false, "msg": "Error connecting: Error while fetching server API version: ('Connection aborted.', error(13, 'Permission denied'))"}
to retry, use: --limit #/data/prolims-provision/provision-docker.retry
PLAY RECAP *********************************************************************
prolims-staging : ok=1 changed=0 unreachable=0 failed=1
I did try looking out for this issue on other forums as well ( and similar questions on this StackOverflow too), but those were not helpful.
Note: I am able to run docker-compose successfully in the target machine from its CLI (using sudo).
Also, I tried playing around with docker_container as well. I tried to execute a playbook with contents below:
...
- name: check container status
command: docker ps
register: result
- name: Create a container
docker_container:
name: db_pg
image: "postgres:latest"
state: present
recreate: yes
...
and running this playbook works perfectly fine.
I assume, posting my docker-compose file might not be relevant here.
I followed this example, but did not work. Maybe, I might be missing some stupid or really important thing here.
Any help on understanding and resolving this issue would be appreciated.
I am able to run docker-compose successfully in the target machine from its CLI (using sudo).
So you need to use become declaration for the task.
I added --ask-sudo-pass to ensure that it was not a permission issue.
Just adding --ask-sudo-pass to the ansible-playbook parameters doesn't have any effect unless the relevant tasks/plays have become declaration (and become_method is set to sudo, but this is by default).
Reference.

Resources