2021-08-07 15:02:21 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2022-08-05 22:45:15 +02:00
|
|
|
# Copyright (c) 2016 Dag Wieers <dag@wieers.com>
|
|
|
|
# Copyright (c) 2017 Ansible Project
|
2022-08-05 13:17:19 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2020-08-04 15:00:41 +02:00
|
|
|
DOCUMENTATION = r'''
|
2021-01-12 07:12:03 +01:00
|
|
|
name: filetree
|
2020-03-09 10:11:07 +01:00
|
|
|
author: Dag Wieers (@dagwieers) <dag@wieers.com>
|
|
|
|
short_description: recursively match all files in a directory tree
|
|
|
|
description:
|
|
|
|
- This lookup enables you to template a complete tree of files on a target system while retaining permissions and ownership.
|
|
|
|
- Supports directories, files and symlinks, including SELinux and other file properties.
|
|
|
|
- If you provide more than one path, it will implement a first_found logic, and will not process entries it already processed in previous paths.
|
|
|
|
This enables merging different trees in order of importance, or add role_vars to specific paths to influence different instances of the same role.
|
|
|
|
options:
|
|
|
|
_terms:
|
|
|
|
description: path(s) of files to read
|
|
|
|
required: True
|
|
|
|
'''
|
|
|
|
|
2020-08-04 15:00:41 +02:00
|
|
|
EXAMPLES = r"""
|
2020-03-09 10:11:07 +01:00
|
|
|
- name: Create directories
|
2020-07-14 17:28:08 +02:00
|
|
|
ansible.builtin.file:
|
2020-03-09 10:11:07 +01:00
|
|
|
path: /web/{{ item.path }}
|
|
|
|
state: directory
|
|
|
|
mode: '{{ item.mode }}'
|
2020-08-08 22:04:34 +02:00
|
|
|
with_community.general.filetree: web/
|
2020-03-09 10:11:07 +01:00
|
|
|
when: item.state == 'directory'
|
|
|
|
|
|
|
|
- name: Template files (explicitly skip directories in order to use the 'src' attribute)
|
2020-07-14 17:28:08 +02:00
|
|
|
ansible.builtin.template:
|
2020-03-09 10:11:07 +01:00
|
|
|
src: '{{ item.src }}'
|
2021-04-21 14:04:46 +02:00
|
|
|
# Your template files should be stored with a .j2 file extension,
|
|
|
|
# but should not be deployed with it. splitext|first removes it.
|
|
|
|
dest: /web/{{ item.path | splitext | first }}
|
2020-03-09 10:11:07 +01:00
|
|
|
mode: '{{ item.mode }}'
|
2020-08-08 22:04:34 +02:00
|
|
|
with_community.general.filetree: web/
|
2020-03-09 10:11:07 +01:00
|
|
|
when: item.state == 'file'
|
|
|
|
|
|
|
|
- name: Recreate symlinks
|
2020-07-14 17:28:08 +02:00
|
|
|
ansible.builtin.file:
|
2020-03-09 10:11:07 +01:00
|
|
|
src: '{{ item.src }}'
|
|
|
|
dest: /web/{{ item.path }}
|
|
|
|
state: link
|
2021-04-21 14:04:46 +02:00
|
|
|
follow: false # avoid corrupting target files if the link already exists
|
2020-03-09 10:11:07 +01:00
|
|
|
force: yes
|
|
|
|
mode: '{{ item.mode }}'
|
2020-08-08 22:04:34 +02:00
|
|
|
with_community.general.filetree: web/
|
2020-03-09 10:11:07 +01:00
|
|
|
when: item.state == 'link'
|
2020-08-04 15:00:41 +02:00
|
|
|
|
|
|
|
- name: list all files under web/
|
|
|
|
ansible.builtin.debug:
|
|
|
|
msg: "{{ lookup('community.general.filetree', 'web/') }}"
|
2020-03-09 10:11:07 +01:00
|
|
|
"""
|
|
|
|
|
2020-08-04 15:00:41 +02:00
|
|
|
RETURN = r"""
|
2020-03-09 10:11:07 +01:00
|
|
|
_raw:
|
2020-09-16 11:06:45 +02:00
|
|
|
description: List of dictionaries with file information.
|
|
|
|
type: list
|
|
|
|
elements: dict
|
2020-03-09 10:11:07 +01:00
|
|
|
contains:
|
|
|
|
src:
|
|
|
|
description:
|
2020-09-16 11:06:45 +02:00
|
|
|
- Full path to file.
|
|
|
|
- Not returned when I(item.state) is set to C(directory).
|
|
|
|
type: path
|
2020-03-09 10:11:07 +01:00
|
|
|
root:
|
2020-09-16 11:06:45 +02:00
|
|
|
description: Allows filtering by original location.
|
|
|
|
type: path
|
2020-03-09 10:11:07 +01:00
|
|
|
path:
|
2020-09-16 11:06:45 +02:00
|
|
|
description: Contains the relative path to root.
|
|
|
|
type: path
|
2020-03-09 10:11:07 +01:00
|
|
|
mode:
|
2020-08-04 15:00:41 +02:00
|
|
|
description: The permissions the resulting file or directory.
|
2020-09-16 11:06:45 +02:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
state:
|
|
|
|
description: TODO
|
2020-09-16 11:06:45 +02:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
owner:
|
2020-08-04 15:00:41 +02:00
|
|
|
description: Name of the user that owns the file/directory.
|
2020-09-16 11:06:45 +02:00
|
|
|
type: raw
|
2020-03-09 10:11:07 +01:00
|
|
|
group:
|
2020-08-04 15:00:41 +02:00
|
|
|
description: Name of the group that owns the file/directory.
|
2020-09-16 11:06:45 +02:00
|
|
|
type: raw
|
2020-03-09 10:11:07 +01:00
|
|
|
seuser:
|
2020-08-04 15:00:41 +02:00
|
|
|
description: The user part of the SELinux file context.
|
2020-09-16 11:06:45 +02:00
|
|
|
type: raw
|
2020-03-09 10:11:07 +01:00
|
|
|
serole:
|
2020-08-04 15:00:41 +02:00
|
|
|
description: The role part of the SELinux file context.
|
2020-09-16 11:06:45 +02:00
|
|
|
type: raw
|
2020-03-09 10:11:07 +01:00
|
|
|
setype:
|
2020-08-04 15:00:41 +02:00
|
|
|
description: The type part of the SELinux file context.
|
2020-09-16 11:06:45 +02:00
|
|
|
type: raw
|
2020-03-09 10:11:07 +01:00
|
|
|
selevel:
|
2020-08-04 15:00:41 +02:00
|
|
|
description: The level part of the SELinux file context.
|
2020-09-16 11:06:45 +02:00
|
|
|
type: raw
|
2020-03-09 10:11:07 +01:00
|
|
|
uid:
|
2020-08-04 15:00:41 +02:00
|
|
|
description: Owner ID of the file/directory.
|
2020-09-16 11:06:45 +02:00
|
|
|
type: int
|
2020-03-09 10:11:07 +01:00
|
|
|
gid:
|
2020-08-04 15:00:41 +02:00
|
|
|
description: Group ID of the file/directory.
|
2020-09-16 11:06:45 +02:00
|
|
|
type: int
|
2020-03-09 10:11:07 +01:00
|
|
|
size:
|
2020-08-04 15:00:41 +02:00
|
|
|
description: Size of the target.
|
2020-09-16 11:06:45 +02:00
|
|
|
type: int
|
2020-03-09 10:11:07 +01:00
|
|
|
mtime:
|
2020-08-04 15:00:41 +02:00
|
|
|
description: Time of last modification.
|
2020-09-16 11:06:45 +02:00
|
|
|
type: float
|
2020-03-09 10:11:07 +01:00
|
|
|
ctime:
|
2020-08-04 15:00:41 +02:00
|
|
|
description: Time of last metadata update or creation (depends on OS).
|
2020-09-16 11:06:45 +02:00
|
|
|
type: float
|
2020-03-09 10:11:07 +01:00
|
|
|
"""
|
|
|
|
import os
|
|
|
|
import pwd
|
|
|
|
import grp
|
|
|
|
import stat
|
|
|
|
|
|
|
|
HAVE_SELINUX = False
|
|
|
|
try:
|
|
|
|
import selinux
|
|
|
|
HAVE_SELINUX = True
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
from ansible.plugins.lookup import LookupBase
|
2021-06-26 23:59:11 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_native, to_text
|
2020-03-09 10:11:07 +01:00
|
|
|
from ansible.utils.display import Display
|
|
|
|
|
|
|
|
display = Display()
|
|
|
|
|
|
|
|
|
|
|
|
# If selinux fails to find a default, return an array of None
|
|
|
|
def selinux_context(path):
|
|
|
|
context = [None, None, None, None]
|
|
|
|
if HAVE_SELINUX and selinux.is_selinux_enabled():
|
|
|
|
try:
|
|
|
|
# note: the selinux module uses byte strings on python2 and text
|
|
|
|
# strings on python3
|
|
|
|
ret = selinux.lgetfilecon_raw(to_native(path))
|
|
|
|
except OSError:
|
|
|
|
return context
|
|
|
|
if ret[0] != -1:
|
|
|
|
# Limit split to 4 because the selevel, the last in the list,
|
|
|
|
# may contain ':' characters
|
|
|
|
context = ret[1].split(':', 3)
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
def file_props(root, path):
|
|
|
|
''' Returns dictionary with file properties, or return None on failure '''
|
|
|
|
abspath = os.path.join(root, path)
|
|
|
|
|
|
|
|
try:
|
|
|
|
st = os.lstat(abspath)
|
|
|
|
except OSError as e:
|
|
|
|
display.warning('filetree: Error using stat() on path %s (%s)' % (abspath, e))
|
|
|
|
return None
|
|
|
|
|
|
|
|
ret = dict(root=root, path=path)
|
|
|
|
|
|
|
|
if stat.S_ISLNK(st.st_mode):
|
|
|
|
ret['state'] = 'link'
|
|
|
|
ret['src'] = os.readlink(abspath)
|
|
|
|
elif stat.S_ISDIR(st.st_mode):
|
|
|
|
ret['state'] = 'directory'
|
|
|
|
elif stat.S_ISREG(st.st_mode):
|
|
|
|
ret['state'] = 'file'
|
|
|
|
ret['src'] = abspath
|
|
|
|
else:
|
|
|
|
display.warning('filetree: Error file type of %s is not supported' % abspath)
|
|
|
|
return None
|
|
|
|
|
|
|
|
ret['uid'] = st.st_uid
|
|
|
|
ret['gid'] = st.st_gid
|
|
|
|
try:
|
|
|
|
ret['owner'] = pwd.getpwuid(st.st_uid).pw_name
|
|
|
|
except KeyError:
|
|
|
|
ret['owner'] = st.st_uid
|
|
|
|
try:
|
|
|
|
ret['group'] = to_text(grp.getgrgid(st.st_gid).gr_name)
|
|
|
|
except KeyError:
|
|
|
|
ret['group'] = st.st_gid
|
|
|
|
ret['mode'] = '0%03o' % (stat.S_IMODE(st.st_mode))
|
|
|
|
ret['size'] = st.st_size
|
|
|
|
ret['mtime'] = st.st_mtime
|
|
|
|
ret['ctime'] = st.st_ctime
|
|
|
|
|
|
|
|
if HAVE_SELINUX and selinux.is_selinux_enabled() == 1:
|
|
|
|
context = selinux_context(abspath)
|
|
|
|
ret['seuser'] = context[0]
|
|
|
|
ret['serole'] = context[1]
|
|
|
|
ret['setype'] = context[2]
|
|
|
|
ret['selevel'] = context[3]
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
class LookupModule(LookupBase):
|
|
|
|
|
|
|
|
def run(self, terms, variables=None, **kwargs):
|
|
|
|
basedir = self.get_basedir(variables)
|
|
|
|
|
|
|
|
ret = []
|
|
|
|
for term in terms:
|
|
|
|
term_file = os.path.basename(term)
|
|
|
|
dwimmed_path = self._loader.path_dwim_relative(basedir, 'files', os.path.dirname(term))
|
|
|
|
path = os.path.join(dwimmed_path, term_file)
|
|
|
|
display.debug("Walking '{0}'".format(path))
|
|
|
|
for root, dirs, files in os.walk(path, topdown=True):
|
|
|
|
for entry in dirs + files:
|
|
|
|
relpath = os.path.relpath(os.path.join(root, entry), path)
|
|
|
|
|
|
|
|
# Skip if relpath was already processed (from another root)
|
|
|
|
if relpath not in [entry['path'] for entry in ret]:
|
|
|
|
props = file_props(path, relpath)
|
|
|
|
if props is not None:
|
|
|
|
display.debug(" found '{0}'".format(os.path.join(path, relpath)))
|
|
|
|
ret.append(props)
|
|
|
|
|
|
|
|
return ret
|