mhutter / ansible-docker-systemd-service Goto Github PK
View Code? Open in Web Editor NEWAnsible role for creating Systemd services for docker containers
Home Page: https://galaxy.ansible.com/mhutter/docker-systemd-service
License: MIT License
Ansible role for creating Systemd services for docker containers
Home Page: https://galaxy.ansible.com/mhutter/docker-systemd-service
License: MIT License
Starting import: task_id=2084247621079170876822522174674628311, pulp_id=01916957-70ec-7593-82bb-bf3530cceed7
==== PARAMETERS ====
importer username: mhutter
matched user: mhutter id:4464
github_user: mhutter
github_repo: ansible-docker-systemd-service
github_reference: master
alternate_clone_url: None
alternate_namespace_name: None
alternate_role_name: None
==== CHECK FOR MATCHING ROLE(S) ====
user:mhutter repo:ansible-docker-systemd-service matched existing role mhutter.docker-systemd-service id:19056
===== CLONING REPO =====
cloning https://github.com/mhutter/ansible-docker-systemd-service ...
===== GIT ATTRIBUTES =====
github_reference(branch): master
github_commit: 41d7736531eb0b4912b72b6cdd97c93c07b99656
github_commit_message: chore(master): release 2.10.1 (#71)
github_commit_date: 2024-08-19T08:30:46+02:00
===== LOADING ROLE =====
Importing with galaxy-importer 0.4.20
Role loading failed! unknown field in galaxy_info: LegacyGalaxyInfo.__init__() got an unexpected keyword argument 'name'
Traceback (most recent call last):
File "/venv/lib64/python3.11/site-packages/galaxy_importer/schema.py", line 532, in parse
galaxy_info = LegacyGalaxyInfo(**metadata["galaxy_info"])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: LegacyGalaxyInfo.__init__() got an unexpected keyword argument 'name'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/app/galaxy_ng/app/api/v1/tasks.py", line 427, in legacy_role_import
result = import_legacy_role(checkout_path, namespace.name, importer_config, logger)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib64/python3.11/site-packages/galaxy_importer/legacy_role.py", line 51, in import_legacy_role
return _import_legacy_role(dirname, namespace, cfg, logger)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib64/python3.11/site-packages/galaxy_importer/legacy_role.py", line 57, in _import_legacy_role
data = LegacyRoleLoader(dirname, namespace, cfg, logger).load()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib64/python3.11/site-packages/galaxy_importer/loaders/legacy_role.py", line 41, in load
self.metadata = self._load_metadata()
^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib64/python3.11/site-packages/galaxy_importer/loaders/legacy_role.py", line 77, in _load_metadata
return schema.LegacyMetadata.parse(meta_path)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib64/python3.11/site-packages/galaxy_importer/schema.py", line 534, in parse
raise exc.LegacyRoleSchemaError(f"unknown field in galaxy_info: {e}") from e
galaxy_importer.exceptions.LegacyRoleSchemaError: unknown field in galaxy_info: LegacyGalaxyInfo.__init__() got an unexpected keyword argument 'name'
Failed
This Ansible module uses its own parameters to support various docker run
command line arguments.
Out of those, multiple types are used:
Examples for single strings:
container_network
maps to --network
container_user
maps to --user
container_hostname
maps to --hostname
Examples for lists:
container_volumes
maps to --volume
container_ports
maps to --publish
container_hosts
maps to --add-host
This is great and marks explicitly how to use this module.
The big downside on this: You need to add additional support for each docker run
command line argument into this module. And the docker run reference is looooong.
One proposal would be to make this Ansible more flexible and avoid the burden of implementing every single docker run
command line argument by offering ways to add arbitrarily
--user
)--volume
)How a possible Ansible usage can look like in the future / Usage of the module:
- name: Start image
include_role:
name: mhutter.docker-systemd-service
vars:
container_name: "hello-world"
container_image: "hello:v1"
container_docker_pull_force_source: false
container_single_args:
network: my-network
user: nonroot
hostname: world
[...]
container_multi_args:
publish:
- '127.0.0.1:3030:3000'
- '127.0.0.1:1234:5678'
volume:
- '/opt/foo:/var/lib/bar:rw'
[...]
[...]
The top-level keys under container_single_args
and container_multi_args
are not validated They can be defined to whatever you want.
A few keys, like container_image
might be good to keep separate, primarily when those are used in a different context (e.g., to craft the systems unit name).
The same can be done for boolean flags via a list like container_boolean_args
for keys like --rm
or --tty
:
container_boolean_args:
- 'rm'
- 'tty'
Latest release on galaxy is 2.0. There are tags fo 2.1.0 and 2.2.0 on github.
Would you mind releasing these versions to galaxy as well?
Hi,
i'm getting the error {"changed": false, "msg": "state is present but all of the following are missing: source"}
and i'm assume, thats fixed in the 2.4.0 release, but ansible galaxy lists only 2.2.0
Basically I'm proposing to add a flag that changes the unit file to contain
TimeoutStartSec=0
ExecStartPre=-/usr/bin/docker kill image
ExecStartPre=-/usr/bin/docker rm image
ExecStartPre=/usr/bin/docker pull user/image
as opposed to the current
ExecStartPre=-{{ docker_path }} rm -f {{ container_name }}
I'm going to implement this for my own use, so let me know if you'd like me to submit a pull request.
Hello, I have found there is an open issue against ansible for handlers not working when using include_role
- See ansible/ansible#20493
It was documented as a Note in the documentation until it is addressed: ansible/ansible#72744
In my case I am getting the following error that the handler does not exist
TASK [mhutter.docker-systemd-service : Create unit myapp-instance1_container.service] ****************************************************************************************************************************************************************************
ERROR! The requested handler 'restart container myapp-instance1' was not found in either the main handlers list nor in the listening handlers list
Has anyone else faced this problem?
Recently templates/unit.j2
has been updated with if condition around container_cmd
{{ container_image }} {% if container_cmd is string %}{{ container_cmd | trim }}{% else %}{{ container_cmd | join(' ') | trim }}{% endif %}
It seems jinja trim line ending by "if" condition and we end up with systems config like
ExecStart=/usr/bin/docker run \
--name vault \
--rm \
--env-file /etc/default/vault \
--volume /opt/cycloid/vault/config:/vault/config --volume /opt/cycloid/vault/file:/vault/file \
--publish 8200:8200 \
\
\
\
\
\
\
--cap-add IPC_LOCK \
vault:1.5.4 vault server -config /vault/config/local.jsonExecStop=/usr/bin/docker stop vault
ExecStartPost=-/opt/cycloid/vault/unseal.sh
As you can see the newline before ExecStop is not respected.
Should we force a new line before ExecStop ?
Version
ansible 2.9.27
jinja2 2.10-2
This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.
This repository currently has no open or pending branches.
requirements.yml
community.docker 3.12.1
.github/workflows/ci.yml
actions/checkout v4
actions/checkout v4
.github/workflows/release.yml
googleapis/release-please-action v4
If the ansible_ssh_user
is different from root (but still a sudoer), the created systemd service fails to start with this error
docker: open /etc/default/<container name>: permission denied.
My current workaround is to change permissions right after the included mhutter.docker-systemd-service
role
- name: Fix https://github.com/mhutter/ansible-docker-systemd-service/issues/45
ansible.builtin.file:
path: /etc/default/<container name>
owner: '{{ ansible_user }}'
mode: '0644'
Hi,
I'm currently facing the task of generating a few containers through service templates. Is service templates supported in this project? If yes, how exactly do I have to do this? If no, this can be considered as a feature request. :-)
The example defines myapp
but the commentary uses the name mysql
. Commentary should also use the name myapp
.
yamllint
See old travis.yml
:
I have an Ansible role in which I include your role a number of times to have long running containers on a dedicated machine. I developed everything against Vagrant + the Ansible provisioner. Everything works like a charm.
Then came the time I had to roll it out on the AWS EC2 production machine. Bam, it broke on setting up the first docker systemd service with this error (when running Ansible with -vvv
):
fatal: [10.130.20.211]: FAILED! => {
"changed": false,
"invocation": {
"module_args": {
"daemon_reload": true,
"enabled": true,
"masked": false,
"name": "elasticsearch_container.service",
"no_block": false,
"state": "{'code': 16, 'name': 'running'}",
"user": false
}
},
"msg": "value of state must be one of: reloaded, restarted, started, stopped, got: {'code': 16, 'name': 'running'}"
}
So the given state became {'code': 16, 'name': 'running'}
. Googling this type of message structure always directs me to the boto
Python AWS SDK library. While not really knowing how Ansible internals work, my assumption is that something of the boto
usage leaks down in the namespace of the Ansible role variables. The value given above could easily be the state of the VM being checked first.
To test this, I renamed the default variable state
in this role to service_state
and updated the usages in install.yml
. Executed my playbook again and with good results.
As I don't know Ansible well enough, I first created this ticket here. It might be the case that this is a valid case of Variable Precedence, but it might be that this collision is a core Ansible bug. In this case, I hope we can work together in the investigation before we file a bug in the Ansible core project.
See https://github.com/mhutter/ansible-docker-systemd-service/blob/master/templates/unit.j2#L48
I don't think this is intended and leads to me not able to connect to additional docker networks ๐ข.
If this is an actual bug, I am more then happy to create PR for it.
When using the module, a deprecation warning is thrown during execution:
[DEPRECATION WARNING]: "include" is deprecated, use include_tasks/import_tasks instead. This feature will be removed in version 2.16. Deprecation warnings can be disabled by setting deprecation_warnings=False in
ansible.cfg.
ansible [core 2.13.4]
config file = None
configured module search path = ['/path/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /opt/homebrew/Cellar/ansible/6.4.0/libexec/lib/python3.10/site-packages/ansible
ansible collection location = /Users/andy.grunwald/.ansible/collections:/usr/share/ansible/collections
executable location = /opt/homebrew/bin/ansible
python version = 3.10.6 (main, Aug 30 2022, 04:58:14) [Clang 13.1.6 (clang-1316.0.21.2.5)]
jinja version = 3.1.2
libyaml = True
It looks like commit 22495f0 had an invalid refactor of the variable names in install.yml
I think the container_enabled, container_masked, container_state should all be renamed to service_*
I'm using this role to have systemd start docker containers, and some of these containers need a Samba share to be mounted locally. These mounts are described in /etc/fstab
, causing systemd to automatically create associated .mount
unit configurations. I'd like systemd to only start the container after the mount unit.
To do this, we'd need to be able to support an additional after
parameter, empty by default, that would allow us to supercharge the unit after
clause.
WDYT? I'm ready to provide the patch if you think this is a good idea. Thanks!
The --add-host
argument for docker run can be used to access the docker host from inside a container. See https://docs.docker.com/engine/reference/commandline/run/#add-host
With lines like --add-host=host.docker.internal:host-gateway
.
Support for the --add-host
command in docker run would be awesome.
The container_args are rendered as
ExecStart=/usr/bin/docker run --name foo --rm --env-file /etc/default/foo ['--add-host idadwh.db:10.0.1.3'] nginx:latest
There have been a few useful changes since the 2.1.0 release currently on Galaxy. Could you make a new release to Galaxy so we can update to a stable version via our requirements.yml ? Thanks.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.