container: Support static ip for container

This commit is contained in:
uumas
2025-09-14 03:08:24 +03:00
parent ad50e05ee9
commit 0b73582f36
4 changed files with 24 additions and 1 deletions

View File

@@ -4,6 +4,7 @@ container_user: ""
container_mounts: [] container_mounts: []
container_publish_ports: [] container_publish_ports: []
container_networks: [] container_networks: []
container_ip: ""
container_secrets: [] container_secrets: []
container_env: {} container_env: {}
container_auto_start: true container_auto_start: true

View File

@@ -115,6 +115,11 @@ argument_specs:
required: false required: false
default: [] default: []
elements: str elements: str
container_ip:
description: IPv4 address for the container in the first network defined in container_networks
type: str
required: false
default: ""
container_secrets: container_secrets:
description: A list of secrets available to the container as file or environment variable description: A list of secrets available to the container as file or environment variable
type: list type: list

View File

@@ -15,10 +15,16 @@
name: network name: network
vars: vars:
network_name: "{{ network }}" network_name: "{{ network }}"
network_subnet: >-
{{
container_ip | ansible.utils.ipsubnet(24)
if (container_ip | length > 0 and network_index == 0) else ''
}}
when: network_created_networks is not defined or network not in network_created_networks when: network_created_networks is not defined or network not in network_created_networks
loop: "{{ container_networks }}" loop: "{{ container_networks }}"
loop_control: loop_control:
loop_var: network loop_var: network
index_var: network_index
- name: Create volumes for container {{ container_name }} - name: Create volumes for container {{ container_name }}
ansible.builtin.include_role: ansible.builtin.include_role:
@@ -45,7 +51,7 @@
command: "{{ container_command or omit }}" command: "{{ container_command or omit }}"
user: "{{ container_user or omit }}" user: "{{ container_user or omit }}"
mount: "{{ _container_mounts | map('items') | map('map', 'join', '=') | map('join', ',') }}" mount: "{{ _container_mounts | map('items') | map('map', 'join', '=') | map('join', ',') }}"
network: "{{ container_networks | map('regex_replace', '$', '.network') }}" network: "{{ _container_networks_with_ip }}"
publish: "{{ container_publish_ports }}" publish: "{{ container_publish_ports }}"
secrets: "{{ _container_secrets }}" secrets: "{{ _container_secrets }}"
env: "{{ container_env }}" env: "{{ container_env }}"

View File

@@ -1,6 +1,17 @@
--- ---
_container_image: "{{ container_image | replace('/', '_') ~ '.image' }}" _container_image: "{{ container_image | replace('/', '_') ~ '.image' }}"
_container_networks: "{{ container_networks | map('regex_replace', '$', '.network') }}"
_container_networks_with_ip: >-
{{
[
_container_networks[0] ~ (
':ip=' ~ container_ip if container_ip | length > 0 else ''
)
]
+ _container_networks[1:]
}}
_container_volumes: "{{ container_mounts | selectattr('type', '==', 'volume') }}" _container_volumes: "{{ container_mounts | selectattr('type', '==', 'volume') }}"
_container_mount_sources: "{{ container_mounts | map(attribute='source') }}" _container_mount_sources: "{{ container_mounts | map(attribute='source') }}"