模板字符串时出现错误模板错误:没有名为"json_query"的筛选器



使用此战术手册

---
- name: ReadJsonfile
hosts: localhost
tasks:

- name: Display the JSON file content
shell: "cat config.json"
register: result
- name: save the Json data to a Variable as a Fact
set_fact:
jsondata: "{{ result.stdout | from_json }}"
- name: setDomainName
set_fact:
domain_name: "{{ jsondata | json_query(jmesquery) }}"
vars:
jmesquery: '[].domain[].name'

我的Linux服务器上有两个用户:user1user2

python3.8pipansible-coreansible-base&安装了jmespath

我使用user1登录,上面的剧本运行良好

ansible-playbook test.yml

然而,当我使用user2登录并运行相同的程序时,我会得到以下错误

TASK[setDomainName]******************************************************************************************2022年3月21日星期一01:16:40-0500(0:00:00.140(0:00:04.221**********致命:[localhost]:失败=>{在模板化字符串时出现模板错误:没有名为"json_query"的筛选器。字符串:{{jsondata|json_query(jmesquery(}}"}

注意:使用user1运行时,剧本运行良好。该错误仅在使用user2运行时出现。

我用user2设置的方式有什么问题吗?

以下是user2的详细信息

[user2@myhost Migrator]$ ansible --version
ansible [core 2.12.3]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/user2/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/user2/.local/lib/python3.8/site-packages/ansible
ansible collection location = /home/user2/.ansible/collections:/usr/share/ansible/collections
executable location = /bin/ansible
python version = 3.8.12 (default, Mar 21 2022, 00:59:19) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
jinja version = 3.0.3
libyaml = True

[user2@myhost Migrator]$ ansible-playbook --version
ansible-playbook [core 2.12.3]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/user2/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/user2/.local/lib/python3.8/site-packages/ansible
ansible collection location = /home/user2/.ansible/collections:/usr/share/ansible/collections
executable location = /bin/ansible-playbook
python version = 3.8.12 (default, Mar 21 2022, 00:59:19) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
jinja version = 3.0.3
libyaml = True
[user2@myhost Migrator]$ pip --version
bash: pip: command not found
[user2@myhost Migrator]$ pip3 --version
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
pip 21.3.1 from /home/user2/.local/lib/python3.6/site-packages/pip (python 3.6)
[user2@myhost Migrator]$ pip3 install jmespath
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: jmespath in /usr/local/lib/python3.6/site-packages (0.10.0)
[user2@myhost Migrator]$
[user2@myhost Migrator]$ python3.8 --version
Python 3.8.12

JmesPath安装如下:

$ sudo pip3.8 install jmespath
Collecting jmespath
Downloading jmespath-1.0.0-py3-none-any.whl (23 kB)
Installing collected packages: jmespath
Successfully installed jmespath-1.0.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

你能为这个问题提出一个解决方案吗?

因为您确实使用pip包ansible-core&ansible-base,您没有集合community.general,此筛选器是该集合的一部分。

您有多种选择:

  1. 当与有问题的用户一起登录时,安装丢失的集合:
    ansible-galaxy collection install community.general
    
  2. 安装完整的Ansible软件包:
    pip uninstall ansible-core ansible-base
    pip install ansible
    
  3. 使用选项-p将集合重新安装到共享文件夹中
    ansible-galaxy collection install community.general 
    -p /usr/share/ansible/collections
    

最新更新