1
0
Fork 0
mirror of https://github.com/containers/ansible-podman-collections.git synced 2026-02-04 07:11:49 +00:00

Convert log-opts to dictionary and idempotent

This commit is contained in:
Sagi Shnaidman 2020-12-20 19:52:05 +02:00
parent 27d5a718e6
commit 65e331bca0
3 changed files with 167 additions and 14 deletions

View file

@ -77,7 +77,11 @@ ARGUMENTS_SPEC_CONTAINER = dict(
log_level=dict(
type='str',
choices=["debug", "info", "warn", "error", "fatal", "panic"]),
log_opt=dict(type='str', aliases=['log_options']),
log_opt=dict(type='dict', aliases=['log_options'],
options=dict(
max_size=dict(type='str'),
path=dict(type='str'),
tag=dict(type='str'))),
mac_address=dict(type='str'),
memory=dict(type='str'),
memory_reservation=dict(type='str'),
@ -387,7 +391,14 @@ class PodmanModuleParams:
return c + ['--log-driver', self.params['log_driver']]
def addparam_log_opt(self, c):
return c + ['--log-opt', self.params['log_opt']]
for k, v in self.params['log_opt'].items():
if v is not None:
c += ['--log-opt',
b"=".join([to_bytes(k.replace('max_size', 'max-size'),
errors='surrogate_or_strict'),
to_bytes(v,
errors='surrogate_or_strict')])]
return c
def addparam_log_level(self, c):
return c + ['--log-level', self.params['log_level']]
@ -880,11 +891,33 @@ class PodmanContainerDiff:
# Parameter has limited idempotency, unable to guess the default log_path
def diffparam_log_opt(self):
before = self.info['logpath']
if self.module_params['log_opt'] in [None, '']:
after = before
else:
after = self.params['log_opt'].split("=")[1]
before, after = {}, {}
# Log path
if 'logpath' in self.info:
path_before = self.info['logpath']
if (self.module_params['log_opt'] and
'path' in self.module_params['log_opt'] and
self.module_params['log_opt']['path'] is not None):
path_after = self.params['log_opt']['path']
else:
path_after = path_before
if path_before != path_after:
before.update({'log-path': path_before})
after.update({'log-path': path_after})
# Log tag
if 'logtag' in self.info:
tag_before = self.info['logtag']
if (self.module_params['log_opt'] and
'tag' in self.module_params['log_opt'] and
self.module_params['log_opt']['tag'] is not None):
tag_after = self.params['log_opt']['tag']
else:
tag_after = ''
if tag_before != tag_after:
before.update({'log-tag': tag_before})
after.update({'log-tag': tag_after})
return self._diff_update_and_compare('log_opt', before, after)
def diffparam_mac_address(self):

View file

@ -421,11 +421,27 @@ options:
log_opt:
description:
- Logging driver specific options. Used to set the path to the container
log file. For example log_opt
"path=/var/log/container/mycontainer.json"
type: str
log file.
type: dict
aliases:
- log_options
suboptions:
path:
description:
- Specify a path to the log file (e.g. /var/log/container/mycontainer.json).
type: str
required: false
max_size:
description:
- Specify a max size of the log file (e.g 10mb).
type: str
required: false
tag:
description:
- Specify a custom log tag for the container.
type: str
required: false
mac_address:
description:
- Specify a MAC address for the container, for example

View file

@ -77,7 +77,7 @@
command: 1h
register: test5
- name: Check info with changed environment vars
- name: Check info with log level
assert:
that: test5 is changed
@ -90,7 +90,7 @@
command: 1h
register: test6
- name: Check info with changed environment vars
- name: Check info with log level again
assert:
that: test6 is not changed
@ -103,7 +103,7 @@
command: 1h
register: test7
- name: Check info with changed environment vars
- name: Check info with changed log level
assert:
that: test7 is changed
@ -115,6 +115,110 @@
command: 1h
register: test8
- name: Check info with changed environment vars
- name: Check info with default log level
assert:
that: test8 is not changed
- name: Run container with log opt tag
containers.podman.podman_container:
image: "{{ idem_image }}"
name: idempotency
state: present
log_opt:
tag: nonotag
log_driver: journald
command: 1h
register: test9
- name: Check info with log opt tag
assert:
that: test9 is changed
- name: Run container with log opt tag - again
containers.podman.podman_container:
image: "{{ idem_image }}"
name: idempotency
state: present
log_opt:
tag: nonotag
log_driver: journald
command: 1h
register: test10
- name: Check info with log opt tag - again
assert:
that: test10 is not changed
- name: Run container with default log opt tag
containers.podman.podman_container:
image: "{{ idem_image }}"
name: idempotency
state: present
command: 1h
log_driver: journald
register: test11
- name: Check info with default log opt tag
assert:
that: test11 is changed
- name: Run container with log opt path
containers.podman.podman_container:
image: "{{ idem_image }}"
name: idempotency
state: present
log_opt:
path: /tmp/container.log
log_driver: journald
command: 1h
register: test12
- name: Check info with log opt path
assert:
that: test12 is changed
- name: Run container with changed log opt path
containers.podman.podman_container:
image: "{{ idem_image }}"
name: idempotency
state: present
log_opt:
path: /tmp/container2.log
log_driver: journald
command: 1h
register: test13
- name: Check info with changed log opt path
assert:
that: test13 is changed
- name: Run container with default log opt path
containers.podman.podman_container:
image: "{{ idem_image }}"
name: idempotency
state: present
log_driver: journald
command: 1h
register: test14
# We can't guess the default log path
- name: Check info with default log opt path
assert:
that: test14 is not changed
- name: Run container with all log-opts
containers.podman.podman_container:
image: "{{ idem_image }}"
name: idempotency
state: present
log_driver: journald
log_opt:
path: /tmp/container3.log
max_size: 100mb
tag: sometag
command: 1h
- name: Remove test container
containers.podman.podman_container:
name: idempotency
state: absent