Skip to content

Commit 5c8b6fb

Browse files
committed
Update podman_container_exec
Signed-off-by: nishipy <[email protected]>
1 parent fd515da commit 5c8b6fb

File tree

1 file changed

+72
-44
lines changed

1 file changed

+72
-44
lines changed

plugins/modules/podman_container_exec.py

Lines changed: 72 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,10 @@
33

44
# Copyright (c) 2023, Takuya Nishimura <@nishipy>
55
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6-
from __future__ import (absolute_import, division, print_function)
7-
from ansible_collections.containers.podman.plugins.module_utils.podman.common import compare_systemd_file_content
8-
import json
9-
from ansible.module_utils.basic import AnsibleModule
10-
from ansible.module_utils._text import to_bytes, to_native, to_text
11-
from ansible.module_utils.six import string_types
12-
import shlex
13-
import os
6+
from __future__ import absolute_import, division, print_function
147
__metaclass__ = type
158

16-
17-
DOCUMENTATION = '''
9+
DOCUMENTATION = r'''
1810
module: podman_container_exec
1911
author:
2012
- Takuya Nishimura (@nishipy)
@@ -30,12 +22,12 @@
3022
command:
3123
description:
3224
- The command to run in the container.
33-
- One of the O(command) or O(args) is required.
25+
- One of the I(command) or I(args) is required.
3426
type: str
3527
argv:
3628
description:
3729
- Passes the command as a list rather than a string.
38-
- One of the O(command) or O(args) is required.
30+
- One of the I(command) or I(args) is required.
3931
type: list
4032
elements: str
4133
detach:
@@ -73,40 +65,71 @@
7365
- See L(the Podman documentation,https://docs.podman.io/en/latest/markdown/podman-exec.1.html) for details of podman-exec(1).
7466
'''
7567

76-
EXAMPLES = '''
77-
To Be Added.
68+
EXAMPLES = r'''
69+
- name: Execute a command with workdir
70+
containers.podman.podman_container_exec:
71+
name: ubi8
72+
command: "cat redhat-release"
73+
workdir: /etc
74+
75+
- name: Execute a command with a list of args and enviroment variables
76+
containers.podman.podman_container_exec:
77+
name: test_container
78+
argv:
79+
- /bin/sh
80+
- -c
81+
- echo $HELLO $BYE
82+
env:
83+
HELLO: hello world
84+
BYE: goodbye world
85+
86+
- name: Execute command in background by using detach
87+
containers.podman.podman_container_exec:
88+
name: detach_container
89+
command: "cat redhat-release"
90+
detach: true
7891
'''
7992

80-
RETURN = '''
93+
RETURN = r'''
8194
stdout:
8295
type: str
83-
returned: success and O(detach=false)
96+
returned: success
8497
description:
85-
- The standard output of the command executed in the container.
98+
- The standard output of the command executed in the container.
8699
stderr:
87100
type: str
88-
returned: success and O(detach=false)
101+
returned: success
89102
description:
90-
- The standard output of the command executed in the container.
103+
- The standard output of the command executed in the container.
91104
rc:
92105
type: int
93-
returned: success and O(detach=false)
106+
returned: success
107+
sample: 0
94108
description:
95-
- The exit code of the command executed in the container.
109+
- The exit code of the command executed in the container.
96110
exec_id:
97111
type: str
98-
returned: success and O(detach=true)
112+
returned: success and I(detach=true)
99113
sample: f99002e34c1087fd1aa08d5027e455bf7c2d6b74f019069acf6462a96ddf2a47
100114
description:
101-
- The ID of the exec session.
115+
- The ID of the exec session.
102116
'''
103117

118+
import shlex
119+
from ansible.module_utils.six import string_types
120+
from ansible.module_utils._text import to_text
121+
from ansible.module_utils.basic import AnsibleModule
122+
from ansible_collections.containers.podman.plugins.module_utils.podman.common import run_podman_command
104123

105-
def container_exec(module: AnsibleModule):
106-
exec_command = ['podman', 'container', 'exec']
107-
# always returns as changed
124+
125+
def run_container_exec(module: AnsibleModule) -> dict:
126+
'''
127+
Execute podman-container-exec for the given options
128+
'''
129+
exec_with_args = ['container', 'exec']
130+
# podman_container_exec always returns changed=true
108131
changed = True
109-
command_options = []
132+
exec_options = []
110133

111134
name = module.params['name']
112135
argv = module.params['argv']
@@ -122,7 +145,7 @@ def container_exec(module: AnsibleModule):
122145
argv = shlex.split(command)
123146

124147
if detach:
125-
command_options.append('--detach')
148+
exec_options.append('--detach')
126149

127150
if env is not None:
128151
for key, value in env.items():
@@ -131,39 +154,43 @@ def container_exec(module: AnsibleModule):
131154
msg="Specify string value %s on the env field" % (value))
132155

133156
to_text(value, errors='surrogate_or_strict')
134-
command_options += ['--env',
135-
'%s="%s"' % (key, value)]
157+
exec_options += ['--env',
158+
'%s="%s"' % (key, value)]
136159

137160
if privileged:
138-
command_options.append('--privileged')
161+
exec_options.append('--privileged')
139162

140163
if tty:
141-
command_options.append('--tty')
164+
exec_options.append('--tty')
142165

143166
if user is not None:
144-
command_options += ['--user',
145-
to_text(user, errors='surrogate_or_strict')]
167+
exec_options += ['--user',
168+
to_text(user, errors='surrogate_or_strict')]
146169

147170
if workdir is not None:
148-
command_options += ['--workdir',
149-
to_text(workdir, errors='surrogate_or_strict')]
171+
exec_options += ['--workdir',
172+
to_text(workdir, errors='surrogate_or_strict')]
173+
174+
exec_options.append(name)
175+
exec_options.extend(argv)
150176

151-
command_options.append(name)
152-
command_options.extend(argv)
177+
exec_with_args.extend(exec_options)
153178

154-
exec_command.extend(command_options)
155-
exec_command_str = ' '.join(exec_command)
156-
rc, stdout, stderr = module.run_command(exec_command_str)
179+
rc, stdout, stderr = run_podman_command(
180+
module=module, executable='podman', args=exec_with_args)
157181

158182
result = {
159183
'changed': changed,
160-
'podman_command': command_options,
184+
'podman_command': exec_options,
161185
'rc': rc,
162186
'stdout': stdout,
163187
'stderr': stderr,
164188
}
165189

166-
module.exit_json(**result)
190+
if detach:
191+
result['exec_id'] = stdout.replace('\n', '')
192+
193+
return result
167194

168195

169196
def main():
@@ -211,7 +238,8 @@ def main():
211238
required_one_of=[('argv', 'command')],
212239
)
213240

214-
container_exec(module)
241+
result = run_container_exec(module)
242+
module.exit_json(**result)
215243

216244

217245
if __name__ == '__main__':

0 commit comments

Comments
 (0)