Type check fails when using in an ansible action plugin
S: triage
I'm hitting the following error when trying to use your library in ansible action plugin:
```
TASK [my_action] ************************************************************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: TypeError: Pattern and root_dir should be of the same type, not <class 'ansible.utils.unsafe_proxy.AnsibleUnsafeText'> and <class 'str'>
fatal: [localhost]: FAILED! => {"msg": "Unexpected failure during module execution: Pattern and root_dir should be of the same type, not <class 'ansible.utils.unsafe_proxy.AnsibleUnsafeText'> and <class 'str'>", "stdout": ""}
```
I've simplified this down to:
```
(kayobe-venv) [vagrant@controller1 vagrant]$ cat /tmp/test2.yml
---
- hosts: localhost
vars:
test: "{{ lookup('env', 'TEST_VAR') }}"
tasks:
- my_action:
glob: "{{ test }}"
```
and
```
(venv-test) [vagrant@controller1 vagrant]$ cat /tmp/action_plugins/my_action.py
#!/usr/bin/python
from ansible.plugins.action import ActionBase
from wcmatch import glob
class ActionModule(ActionBase):
def run(self, tmp=None, task_vars=None):
super(ActionModule, self).run(tmp, task_vars)
module_args = self._task.args.copy()
result = glob.glob(module_args.get('glob'), flags=glob.GLOBSTAR)
```
and you run can run it with:
```
[vagrant@controller1 vagrant]$ python3 -m venv ~/venv-test
[vagrant@controller1 vagrant]$ source ~/venv-test/bin/activate
(venv-test) [vagrant@controller1 vagrant]$ pip install -U pip setuptools
(venv-test) [vagrant@controller1 vagrant]$ pip install 'ansible>=7,<9.0' wcmatch===8.5
(venv-test) [vagrant@controller1 vagrant]$ export TEST_VAR=myglob
(venv-test) [vagrant@controller1 vagrant]$ ansible-playbook /tmp/test2.yml
```
This used to work until https://github.com/ansible/ansible/pull/82294 was merged. It looks like the behaviour to preserve AnsibleUnsafeText seems to be correct (as it was set from a user controllable environment variable), so I am just wondering if we make [the check](https://github.com/facelessuser/wcmatch/blob/876362e63d0b8d33c0d0fed55e02eb91d378f201/wcmatch/glob.py#L455-L460) less restrictive. Both instances pass instanceof(instance, str), but only the glob passes instanceof(instance, 'ansible.utils.unsafe_proxy.AnsibleUnsafeText).AnsibleUnsafeText behaves exactly like a string but is used to mark it as unsafe to template.
关闭于 2024-02-20 6 条评论