Ansible Cron - Run another ansible task periodically - ansible

is there a way to start a podman container with ansible periodically?
I'd like to mix the ansible's cron, and podman modules, like:
- name: Run the DB Migrations docker image
ansible.builtin.cron:
hours: '10'
job:
containers.podman.podman_container:
name: my-podman-container
image: registry.gitlab.com/vilmosnagy/my-container
recreate: true
Yeah, I could write the podman run --rm --name my-podman-container registry.gitlab.com/vilmosnagy/my-container command in the cron module I'm looking for a better way to do this.
Thanks,

Related

Docker compose to have executed 'command: bash' and keep container open

The docker compose yml file below keeps the container open after I run docker compose up -d but command: bash does not get executed:
version: "3.8"
services:
service:
container_name: pw-cont
image: mcr.microsoft.com/playwright:v1.30.0-focal
stdin_open: true # -i
tty: true # -t
network_mode: host # --network host
volumes: # Ensures that updates in local/container are in sync
- $PWD:/playwright/
working_dir: /playwright
command: bash
After I spin the container up, I wanted to visit Docker Desktop > Running container's terminal.
Expectation: Since the file has command: bash, I expect that in docker desktop, when I go to the running container's terminal, it will show root#docker-desktop:/playwright#.
Actual: Container's terminal in docker desktop is showing #, still need to type bash to see root#docker-desktop:/playwright#.
Can the yml file be updated so that bash gets auto executed when spinning up the container?
docker compose doesn't provide that sort of interactive connection. Your docker-compose.yaml file is fine; once your container is running you can attach to it using docker attach pw-cont to access stdin/stdout for the container.
$ docker compose up -d
[+] Running 1/1
⠿ Container pw-cont Started 0.1s
$ docker attach pw-cont
root#rocket:/playwright#
root#rocket:/playwright#
I'm not sure what you are trying to achieve, but using the run command
docker-compose run service
gives me the prompt you expect.

Laravel Vapor Docker Runtime with Gitlab CI want not to be work

I use Laravel Vapor for deploying our microservices based on Laravel. This works very good so far, if the app with their dependencies is not too large. But if it is then it gets a little bit tricky.
Vapor provides a Docker runtime for this case where you are able to deploy apps up to 10GB size.
For local development we usually use Laradock.io because its easy and flexible.
That means if we deploy from our local environment it easy to enter the workspace container and and run the vapor deploy commands. After enabling Docker Client for the workspace container it works with the vapor Docker runtime properly.
But now we integrated the deployment process into Gitlab CI Pipeline. That works very well for our small services with Vapor PHP runtime.
But for the Docker runtime I desperate on the CI deployment.
The docker runtime needs an installed docker instance where vapor will be invoked. That means in the Gitlab-ci.yml I have to add an image with installed Docker and PHP to invoke the Vapor scripts.
So I created an docker image base on the laradock workspace container but the Gitlab-runner exits always with the error message no docker deamon is available.
This is the related part of my GitLab-CI yml (the image is only local available):
testing:
image:
name: lexitaldev/vapor-docker-deploy:latest
pull_policy: never
securityContext:
privileged: true
environment: testing
stage: deploy
only:
- test
script:
- composer install
- php vendor/bin/vapor deploy test
This is the specific output:
Error Output:
================
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the
docker daemon running?
I've tried to use the standard 'laravelphp/vapor:php80' image and install docker over the script section as well.
before_script:
- apk add docker
- addgroup root docker
But nothing helped. It seems to be there is a problem with the docker.sock.
Did anybody managed to add Vapor Docker Runtime deployment to CI scripts?
Best,
Michael
I would like to tell you, that you only need to add the Service: dind, but after you do that, it will throw an error, related to the image that Gitlab create for your pipelines. So you need to create a runner with volumes, privileged flag, and tags.
I did it, using gitlab-runner on my machine.
sudo gitlab-runner register -n \
--url {{ your_url }} \
--registration-token {{your_token}} \
--executor docker \
--description "{{ Describe your runner }}" \
--docker-image "docker:20.10.12-alpine3.15" \
--docker-privileged \
--docker-volumes="/certs/client" \
--docker-volumes="cache" \
--docker-volumes="/var/run/docker.sock:/var/run/docker.sock"
--tag-list {{ a_tag_for_your_pipeline }}
Once you did that, you would need to use a docker stable version in your gitlab-ci.yml file. For some reason, it doesn't work when I was trying to use version 20 or latest
image: docker:stable
services:
- name: docker:stable:dind
before_script:
- echo $CI_JOB_TOKEN | docker login $CI_REGISTRY -u $CI_REGISTRY_USER --password-stdin
build:
tags:
- {{the tag you defined in your runner}}
variables:
IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
script:
- echo $IMAGE_TAG
- docker build -t $CI_REGISTRY_IMAGE -f {{your Dockerfile}} .
- docker push $CI_REGISTRY_IMAGE
All the variables are previously defined in Gitlab, so don't worry, you can "copy & paste". Also, I added some advices that Gitlab mention on its documentation when you need to register your Docker container in Gitlab container.

Use Hadoop from CDH in Ansible

I want to use Hadoop from CDH docker image. CDH image is already installed on my machine and I can run it.
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
07a55a9d4cb9 4239cd2958c6 "/usr/bin/docker-quickstart" 18 minutes ago Up 18 minutes 0.0.0.0:32774->7180/tcp, 0.0.0.0:32773->8888/tcp container
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container
172.17.0.2
Local, I am writing an ansible playbook and I need to set Hadoop conf dir in CDH which is: "/etc/hadoop/conf".
How can I set the running docker image in my ansible playbook?
I tried:
- name: run cloudera
docker_container:
name: "container"
image: quickstart/cloudera
command: /usr/bin/docker-quickstart"
state: started
ports:
- 8888:8888
- 7180:7180
But this command runs another docker image and I would like to connect to the running one.
inventory.ini
container ansible_connection=docker
Note: I suggest for the future that you rename your container to something more distinct than container....
example playbook.yml
---
- hosts: container
tasks:
- name: I am a dummy task, write your own
file:
path: /tmp/helloContainer
state: file
Running the playbook
ansible-playbook -i inventory.ini playbook.yml

how to set --network=host in docker_image via ansible

I want to replace this command with an ansible playbook
'docker build -q --network host -t "ubuntu" . '
I have been going through docker_image module of ansible but couldn't figure it out. Any idea on how to proceed further?
Thanks in advance.
The nearest you can have it is:
---
- name: build the image
docker_image:
name: docker
tag: ubuntu
path: "/yourpath"
state: present
For the --network host, there is a request open in Github to have it.

Docker in Docker docker-compose daemon not running on host. Windows 10

Running docker in docker image (dind), on windows 10. Powershell is run as admin. I have a manager and a worker up container up through docker-compose.yml file.
The compose yaml file is as such:
version: '2'
services:
manager:
image: docker:latest
ports:
- "2375"
- "8080"
privileged: true
tty: true
worker:
image: docker:latest
ports:
- "8080"
privileged: true
tty: true
I don't know why or what tty: true even does but it's the only way to get it to stay up for some reason.
I try to init the manager with:
docker-compose exec manager docker swarm init --listen-addr 0.0.0.0:2377
I also tried with the port being 0.0.0.0:2375 as what is open in the compose yaml.
When I run the command and get this:
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
Here is the result of a docker-compose ps
Name Command State Ports
swarmtest_manager_1 docker-entrypoint.sh sh Up 0.0.0.0:32782->2375/tcp, 0.0.0.0:32781->8080/tcp
swarmtest_worker_1 docker-entrypoint.sh sh Up 0.0.0.0:32780->8080/tcp
Running and testing services in the dind environment would be ideal, although I still don't like compose as I am trying to learn how to use it better, creating a docker-machine and using the docker swarm mode seems much easier, although I'm truthfully not sure of the limitations with compose in this manner.

Resources