Debian Codegraph @

ansible_collections/containers/podman/plugins/modules/podman_container_copy.py

raw
1 #!/usr/bin/python
2 # Copyright (c) 2024 Ansible Project
3 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4
5 from __future__ import absolute_import, division, print_function
6
7 __metaclass__ = type
8
9
10 DOCUMENTATION = r"""
11 module: podman_container_copy
12 author:
13 - Alessandro Rossi (@kubealex)
14 short_description: Copy file to/from a container
15 notes:
16 - Podman may required elevated privileges in order to run properly.
17 description:
18 - Copy file or folder from the host to a container and vice-versa.
19 options:
20 executable:
21 description:
22 - Path to C(podman) executable if it is not in the C($PATH) on the machine running C(podman)
23 default: 'podman'
24 type: str
25 src:
26 description:
27 - Path of the file/folder to copy from/to the container
28 type: str
29 required: True
30 dest:
31 description:
32 - Path of the destination file/folder to copy from/to the container
33 required: True
34 type: str
35 container:
36 description:
37 - Name/ID of the container to copy from/to
38 required: True
39 type: str
40 from_container:
41 description:
42 - Specify whether or not the file must be copied from the container to the host
43 required: False
44 default: False
45 type: bool
46 archive:
47 description:
48 - Chown copied files to the primary uid/gid of the destination container.
49 required: False
50 default: True
51 type: bool
52 overwrite:
53 description:
54 - Allow to overwrite directories with non-directories and vice versa
55 required: False
56 default: False
57 type: bool
58 """
59
60 EXAMPLES = r"""
61 - name: Copy file "test.yml" on the host to the "apache" container's root folder
62 containers.podman.podman_container_copy:
63 src: test.yml
64 dest: /
65 container: apache
66 - name: Copy file "test.yml" in the "apache" container's root folder to the playbook's folder
67 containers.podman.podman_container_copy:
68 src: /test.yml
69 dest: ./
70 container: apache
71 from_container: True
72 """
73
74 from ansible.module_utils.basic import AnsibleModule
75
76
77 def copy_file(module, executable, src, dest, container, from_container, archive, overwrite):
78 if from_container:
79 command = [executable, "cp", "{0}:{1}".format(container, src), dest]
80 else:
81 command = [executable, "cp", src, "{0}:{1}".format(container, dest)]
82
83 if not archive:
84 command.append("--archive=False")
85
86 if overwrite:
87 command.append("--overwrite")
88
89 rc, out, err = module.run_command(command)
90
91 if rc != 0:
92 module.fail_json(msg="Unable to copy file to/from container - {out}".format(out=err))
93 else:
94 changed = True
95 return changed, out, err
96
97
98 def main():
99 module = AnsibleModule(
100 argument_spec=dict(
101 executable=dict(type="str", default="podman"),
102 src=dict(type="str", required=True),
103 dest=dict(type="str", required=True),
104 container=dict(type="str", required=True),
105 from_container=dict(type="bool", required=False, default=False),
106 archive=dict(type="bool", required=False, default=True),
107 overwrite=dict(type="bool", required=False, default=False),
108 ),
109 supports_check_mode=False,
110 )
111
112 executable = module.params["executable"]
113 src = module.params["src"]
114 dest = module.params["dest"]
115 container = module.params["container"]
116 from_container = module.params["from_container"]
117 archive = module.params["archive"]
118 overwrite = module.params["overwrite"]
119
120 executable = module.get_bin_path(executable, required=True)
121
122 changed, out, err = copy_file(module, executable, src, dest, container, from_container, archive, overwrite)
123
124 results = dict(changed=changed)
125
126 module.exit_json(**results)
127
128
129 if __name__ == "__main__":
130 main()