HEX
Server: LiteSpeed
System: Linux kapuas.iixcp.rumahweb.net 5.14.0-427.42.1.el9_4.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Nov 1 14:58:02 EDT 2024 x86_64
User: mirz4654 (1666)
PHP: 8.1.33
Disabled: system,exec,escapeshellarg,escapeshellcmd,passthru,proc_close,proc_get_status,proc_nice,proc_open,proc_terminate,shell_exec,popen,pclose,dl,pfsockopen,leak,apache_child_terminate,posix_kill,posix_mkfifo,posix_setsid,posix_setuid,posix_setpgid,ini_alter,show_source,define_syslog_variables,symlink,syslog,openlog,openlog,closelog,ocinumcols,listen,chgrp,apache_note,apache_setenv,debugger_on,debugger_off,ftp_exec,dll,ftp,myshellexec,socket_bind,mail,posix_getwpuid
Upload Files
File: //lib/python3.9/site-packages/ansible_collections/dellemc/os10/plugins/action/textfsm_parser.py
# -*- coding: utf-8 -*-

# (c) 2020, Ansible by Red Hat, inc
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
#
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from ansible.module_utils.six import StringIO, string_types

from ansible.plugins.action import ActionBase
from ansible.errors import AnsibleError

try:
    import textfsm
    HAS_TEXTFSM = True
except ImportError:
    HAS_TEXTFSM = False


class ActionModule(ActionBase):

    def run(self, tmp=None, task_vars=None):
        ''' handler for textfsm action '''

        if task_vars is None:
            task_vars = dict()

        result = super(ActionModule, self).run(tmp, task_vars)
        del tmp  # tmp no longer has any effect

        try:
            if not HAS_TEXTFSM:
                raise AnsibleError('textfsm_parser engine requires the TextFSM library to be installed')

            try:
                filename = self._task.args.get('file')
                src = self._task.args.get('src')
                content = self._task.args['content']
                name = self._task.args.get('name')
            except KeyError as exc:
                raise AnsibleError('missing required argument: %s' % exc)

            if src and filename:
                raise AnsibleError('`src` and `file` are mutually exclusive arguments')

            if not isinstance(content, string_types):
                return {'failed': True, 'msg': '`content` must be of type str, got %s' % type(content)}

            if filename:
                tmpl = open(filename)
            else:
                tmpl = StringIO()
                tmpl.write(src.strip())
                tmpl.seek(0)

            try:
                re_table = textfsm.TextFSM(tmpl)
                fsm_results = re_table.ParseText(content)

            except Exception as exc:
                raise AnsibleError(str(exc))

            final_facts = []
            for item in fsm_results:
                facts = {}
                facts.update(dict(zip(re_table.header, item)))
                final_facts.append(facts)

            if name:
                result['ansible_facts'] = {name: final_facts}
            else:
                result['ansible_facts'] = {}

        finally:
            self._remove_tmp_path(self._connection._shell.tmpdir)

        return result