Reputation: 1457
I have been using jenkins to build docker images and push to docker hub with the tag latest in everytime. I have written a ansible playbook which will deploy the docker images by pulling the latest image from docker hub.Now issue is new latest images has not been pulled by ansible once it deployed its previous version with same tag.Can you please check the playbook and let me know which part should i update to get the desired work.
Playbook:
---
- hosts: flask04
tasks:
- name: Pull Flask app image
docker_image:
name: taybur/flaskapp_27032019
tag: latest
state: present
- name: remove flask app container
docker_container:
name: first_flaskapp
image: taybur/flaskapp_27032019
state: absent
- name: Create flask app container
docker_container:
name: first_flaskapp
image: taybur/flaskapp_27032019
ports:
- "5001:5001"
state: started
Upvotes: 9
Views: 6582
Reputation: 10757
I usually remove the old image as part of cleaning up before installing. You should first remove the image, just like you remove the container. This will force ansible to pull the new version of the image.
---
- hosts: flask04
tasks:
- name: Remove Flask app image
docker_image:
name: taybur/flaskapp_27032019
tag: latest
force: true
state: absent
- name: Pull Flask app image
docker_image:
name: taybur/flaskapp_27032019
tag: latest
state: present
- name: remove flask app container
docker_container:
name: first_flaskapp
image: taybur/flaskapp_27032019
state: absent
- name: Create flask app container
docker_container:
name: first_flaskapp
image: taybur/flaskapp_27032019
ports:
- "5001:5001"
state: started
Upvotes: 0
Reputation: 44760
The docker_image
module will not automatically pull the image if it is already present. You have to use the force_source: yes
parameter (with source: pull
) to force pulling on every run.
Note: until ansible 2.8, the parameter was force: yes
. It has been deprecated in 2.9 and removed in 2.12. Mentioning source: pull
is also mandatory since that release
Moreover, docker_container
can pull the image for you if it is not present. And you can tell it to attempt to pull on every run (pull: true
) and restart the container if needed. So you can reduce your set of tasks to a single one in this case:
- name: Create/Update the flask app container if needed
docker_container:
name: first_flaskapp
image: taybur/flaskapp_27032019
pull: true
ports:
- "5001:5001"
state: started
See the module documentation for docker_container and docker_image
To go further:
restart_policy: always
if you need this feature.pull
parameter dynamic with a variable you will use as an extra var on the command line: - name: Create/Update the flask app container if needed
docker_container:
name: first_flaskapp
image: taybur/flaskapp_27032019
pull: "{{ upgrade_flaskapp | default(false) | bool }}"
restart_policy: always
ports:
- "5001:5001"
state: started
Now if you run the playbook normally, it will:
If you run with ansible-playbook -i <inventory> playbook.yml -e upgrade_flaskapp=true
it will:
Upvotes: 14
Reputation: 360
Ideally, we should have our tasks/roles idempotent(skip duplicate work if ran repeatedly). So, I think it is cleaner to tag your builds with version numbers and use the version number in your deployment instead of latest.
Upvotes: 1