模板化字符串时出现错误模板错误:没有名为“json_query”的过滤器;

发布于 2025-01-15 18:19:40 字数 4114 浏览 6 评论 0原文

通过此剧本,

---
- 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.8pip、<代码>ansible-core、ansible-base & jmespath 已安装。

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

ansible-playbook test.yml

但是,当我使用 user2 登录并运行相同的内容时,我得到下面的错误

任务[setDomainName] ****************************************************** ****************************************** 2022 年 3 月 21 日星期一 01:16:40 - 0500 (0:00:00.140) 0:00:04.221 ********** 致命:[localhost]:失败! => {“msg”:“模板化字符串时出现模板错误:没有名为“json_query”的过滤器..字符串:{{ json数据 | json_query(jmesquery) }}"}

注意: 当使用 user1 运行时,playbook 工作正常。错误仅在使用 user2 运行时出现。

是我使用 user2 设置的方式有问题吗?

user2 JmesPath 的详细信息

[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

下面是安装

$ 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

:您能否建议解决该问题的方法?

With this playbook

---
- 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'

There are two users on my Linux server: user1 and user2

python3.8, pip, ansible-core, ansible-base & jmespath were installed.

I login with user1 and the above playbook runs fine

ansible-playbook test.yml

However, when I login with user2 and run the same I get the below error

TASK [setDomainName]
****************************************************************************************** Monday 21 March 2022 01:16:40 -0500 (0:00:00.140) 0:00:04.221
********** fatal: [localhost]: FAILED! => {"msg": "template error while templating string: No filter named 'json_query'.. String: {{
jsondata | json_query(jmesquery) }}"}

Note: When run with user1 the playbook works fine. The error is when run with user2 only.

Is there something wrong with the way I setup things with user2?

Below are the details for 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 was installed as below:

$ 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

Can you please suggest a solution to the issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世界和平 2025-01-22 18:19:40

因为您确实使用 pip 包安装了 Ansible ansible-core & ansible-base,您没有此过滤器所属的集合community.general

您有多个选择:

  1. 当使用有问题的用户登录时,安装缺少的集合:
    ansible-galaxy集合安装community.general
    
  2. 安装完整的 Ansible 软件包:
    pip uninstall ansible-core ansible-base
    pip 安装 ansible
    
  3. 使用选项 -p 在共享文件夹中重新安装集合
    ansible-galaxy集合安装community.general \
        -p /usr/share/ansible/集合
    

Because you did install Ansible with the pip packages ansible-core & ansible-base, you don't have the collection community.general, which this filter is part of.

You have multiple options:

  1. Install the missing collection, when logged in with the problematic user:
    ansible-galaxy collection install community.general
    
  2. Install the full Ansible package:
    pip uninstall ansible-core ansible-base
    pip install ansible
    
  3. Reinstall the collection in the shared folder, with the option -p
    ansible-galaxy collection install community.general \
        -p /usr/share/ansible/collections
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文