Skip to content

Commit fd515da

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

File tree

1 file changed

+218
-0
lines changed

1 file changed

+218
-0
lines changed
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
#!/usr/bin/python
2+
# coding: utf-8 -*-
3+
4+
# Copyright (c) 2023, Takuya Nishimura <@nishipy>
5+
# 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
14+
__metaclass__ = type
15+
16+
17+
DOCUMENTATION = '''
18+
module: podman_container_exec
19+
author:
20+
- Takuya Nishimura (@nishipy)
21+
short_description: Executes a command in a running container.
22+
description:
23+
- Executes a command in a running container.
24+
options:
25+
name:
26+
description:
27+
- Name of the container where the command is executed.
28+
type: str
29+
required: true
30+
command:
31+
description:
32+
- The command to run in the container.
33+
- One of the O(command) or O(args) is required.
34+
type: str
35+
argv:
36+
description:
37+
- Passes the command as a list rather than a string.
38+
- One of the O(command) or O(args) is required.
39+
type: list
40+
elements: str
41+
detach:
42+
description:
43+
- If true, the command runs in the background.
44+
- The exec session is automatically removed when it completes.
45+
type: bool
46+
default: false
47+
env:
48+
description:
49+
- Set environment variables.
50+
type: dict
51+
privileged:
52+
description:
53+
- Give extended privileges to the container.
54+
type: bool
55+
default: false
56+
tty:
57+
description:
58+
- Allocate a pseudo-TTY.
59+
type: bool
60+
default: false
61+
user:
62+
description:
63+
- The username or UID used and, optionally, the groupname or GID for the specified command.
64+
- Both user and group may be symbolic or numeric.
65+
type: str
66+
workdir:
67+
description:
68+
- Working directory inside the container.
69+
type: str
70+
requirements:
71+
- podman
72+
notes:
73+
- See L(the Podman documentation,https://docs.podman.io/en/latest/markdown/podman-exec.1.html) for details of podman-exec(1).
74+
'''
75+
76+
EXAMPLES = '''
77+
To Be Added.
78+
'''
79+
80+
RETURN = '''
81+
stdout:
82+
type: str
83+
returned: success and O(detach=false)
84+
description:
85+
- The standard output of the command executed in the container.
86+
stderr:
87+
type: str
88+
returned: success and O(detach=false)
89+
description:
90+
- The standard output of the command executed in the container.
91+
rc:
92+
type: int
93+
returned: success and O(detach=false)
94+
description:
95+
- The exit code of the command executed in the container.
96+
exec_id:
97+
type: str
98+
returned: success and O(detach=true)
99+
sample: f99002e34c1087fd1aa08d5027e455bf7c2d6b74f019069acf6462a96ddf2a47
100+
description:
101+
- The ID of the exec session.
102+
'''
103+
104+
105+
def container_exec(module: AnsibleModule):
106+
exec_command = ['podman', 'container', 'exec']
107+
# always returns as changed
108+
changed = True
109+
command_options = []
110+
111+
name = module.params['name']
112+
argv = module.params['argv']
113+
command = module.params['command']
114+
detach = module.params['detach']
115+
env = module.params['env']
116+
privileged = module.params['privileged']
117+
tty = module.params['tty']
118+
user = module.params['user']
119+
workdir = module.params['workdir']
120+
121+
if command is not None:
122+
argv = shlex.split(command)
123+
124+
if detach:
125+
command_options.append('--detach')
126+
127+
if env is not None:
128+
for key, value in env.items():
129+
if not isinstance(value, string_types):
130+
module.fail_json(
131+
msg="Specify string value %s on the env field" % (value))
132+
133+
to_text(value, errors='surrogate_or_strict')
134+
command_options += ['--env',
135+
'%s="%s"' % (key, value)]
136+
137+
if privileged:
138+
command_options.append('--privileged')
139+
140+
if tty:
141+
command_options.append('--tty')
142+
143+
if user is not None:
144+
command_options += ['--user',
145+
to_text(user, errors='surrogate_or_strict')]
146+
147+
if workdir is not None:
148+
command_options += ['--workdir',
149+
to_text(workdir, errors='surrogate_or_strict')]
150+
151+
command_options.append(name)
152+
command_options.extend(argv)
153+
154+
exec_command.extend(command_options)
155+
exec_command_str = ' '.join(exec_command)
156+
rc, stdout, stderr = module.run_command(exec_command_str)
157+
158+
result = {
159+
'changed': changed,
160+
'podman_command': command_options,
161+
'rc': rc,
162+
'stdout': stdout,
163+
'stderr': stderr,
164+
}
165+
166+
module.exit_json(**result)
167+
168+
169+
def main():
170+
argument_spec = {
171+
'name': {
172+
'type': 'str',
173+
'required': True
174+
},
175+
'command': {
176+
'type': 'str',
177+
},
178+
'argv': {
179+
'type': 'list',
180+
},
181+
'detach': {
182+
'type': 'bool',
183+
'default': False
184+
},
185+
'env': {
186+
'type': 'dict',
187+
},
188+
'privileged': {
189+
'type': 'bool',
190+
'default': False
191+
},
192+
'privileged': {
193+
'type': 'bool',
194+
'default': False
195+
},
196+
'tty': {
197+
'type': 'bool',
198+
'default': False
199+
},
200+
'user': {
201+
'type': 'str',
202+
},
203+
'workdir': {
204+
'type': 'str',
205+
},
206+
}
207+
208+
module = AnsibleModule(
209+
argument_spec=argument_spec,
210+
supports_check_mode=True,
211+
required_one_of=[('argv', 'command')],
212+
)
213+
214+
container_exec(module)
215+
216+
217+
if __name__ == '__main__':
218+
main()

0 commit comments

Comments
 (0)