如何复制问题:
/usr/bin/python3 -m pip install ansible
python3 -m venv /usr/local/kaas_virtualenv
/usr/local/kaas_virtualenv/bin/python3 -m pip install jmespath
中安装python模块可用的剧本和广告资源:
playbook.yaml
---
- hosts: control_host
gather_facts: yes
become: no
environment:
PYTHONPATH: "/usr/local/kaas_virtualenv/lib/python3.7/site-packages"
vars:
my_users:
- password: 123
username: broker
- password: 321
username: broker2
- password: 213
username: broker3
tasks:
- name: print users
debug:
msg: "users = {{ my_users|json_query('[*].username') }}"
server_inventory
[control_host]
localhost
[control_host:vars]
ansible_connection=local
ansible_python_interpreter=/usr/local/kaas_virtualenv/bin/python3
运行剧本:ansible-playbook -i server_inventory playbook.yaml
预期输出:
PLAY [control_host] ***********************
TASK [Gathering Facts] ***********************
ok: [localhost]
TASK [print users] ***********************
ok: [localhost] => {}
MSG:
users = ['broker', 'broker2', 'broker3']
PLAY RECAP ***********************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
实际输出:
bash-4.2# ansible-playbook -i server_inventory playbook.yaml -vvvv
ansible-playbook 2.9.6
config file = /ansible-dev/swap/ansible.cfg
configured module search path = ['/ansible-dev/swap/library']
ansible python module location = /usr/local/lib/python3.7/site-packages/ansible
executable location = /usr/local/bin/ansible-playbook
python version = 3.7.6 (default, Feb 26 2020, 20:54:15) [GCC 7.3.1 20180712 (Red Hat 7.3.1-6)]
Using /ansible-dev/swap/ansible.cfg as config file
setting up inventory plugins
host_list declined parsing /ansible-dev/swap/server_inventory as it did not pass its verify_file() method
script declined parsing /ansible-dev/swap/server_inventory as it did not pass its verify_file() method
auto declined parsing /ansible-dev/swap/server_inventory as it did not pass its verify_file() method
Set default localhost to localhost
Parsed /ansible-dev/swap/server_inventory inventory source with ini plugin
Loading callback plugin debug of type stdout, v2.0 from /usr/local/lib/python3.7/site-packages/ansible/plugins/callback/debug.py
PLAYBOOK: playbook.yaml ***********************
Positional arguments: playbook.yaml
verbosity: 4
connection: smart
timeout: 10
become_method: sudo
tags: ('all',)
inventory: ('/ansible-dev/swap/server_inventory',)
forks: 5
1 plays in playbook.yaml
PLAY [control_host] ***********************
TASK [Gathering Facts] ***********************
task path: /ansible-dev/swap/playbook.yaml:2
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: root
<localhost> EXEC /bin/sh -c 'echo ~root && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1587713210.496898-92940637298090 `" && echo ansible-tmp-1587713210.496898-92940637298090="` echo /root/.ansible/tmp/ansible-tmp-1587713210.496898-92940637298090 `" ) && sleep 0'
Using module file /usr/local/lib/python3.7/site-packages/ansible/modules/system/setup.py
<localhost> PUT /root/.ansible/tmp/ansible-local-3398m567tl8a/tmprytb4g6g TO /root/.ansible/tmp/ansible-tmp-1587713210.496898-92940637298090/AnsiballZ_setup.py
<localhost> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1587713210.496898-92940637298090/ /root/.ansible/tmp/ansible-tmp-1587713210.496898-92940637298090/AnsiballZ_setup.py && sleep 0'
<localhost> EXEC /bin/sh -c 'PYTHONPATH=/usr/local/kaas_virtualenv/lib/python3.7/site-packages /usr/local/kaas_virtualenv/bin/python3 /root/.ansible/tmp/ansible-tmp-1587713210.496898-92940637298090/AnsiballZ_setup.py && sleep 0'
ok: [localhost]
META: ran handlers
TASK [print users] ***********************
task path: /ansible-dev/swap/playbook.yaml:18
fatal: [localhost]: FAILED! => {}
MSG:
You need to install "jmespath" prior to running json_query filter
PLAY RECAP **********************
可安装:
bash-4.2# ansible --version
ansible 2.9.6
config file = /ansible-dev/swap/ansible.cfg
configured module search path = ['/ansible-dev/swap/library']
ansible python module location = /usr/local/lib/python3.7/site-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.7.6 (default, Feb 26 2020, 20:54:15) [GCC 7.3.1 20180712 (Red Hat 7.3.1-6)]
即使我激活virtualenv . /usr/local/kaas_virtualenv/bin/activate
并运行ansible-playbook ansible-playbook -i server_inventory playbook.yaml -vvvv
在激活 virtualenv的情况下运行剧本:
(kaas_virtualenv) bash-4.2# ansible-playbook -i server_inventory playbook.yaml -vvvv
ansible-playbook 2.9.6
config file = /ansible-dev/swap/ansible.cfg
configured module search path = ['/ansible-dev/swap/library']
ansible python module location = /usr/local/lib/python3.7/site-packages/ansible
executable location = /usr/local/bin/ansible-playbook
python version = 3.7.6 (default, Feb 26 2020, 20:54:15) [GCC 7.3.1 20180712 (Red Hat 7.3.1-6)]
Using /ansible-dev/swap/ansible.cfg as config file
setting up inventory plugins
host_list declined parsing /ansible-dev/swap/server_inventory as it did not pass its verify_file() method
script declined parsing /ansible-dev/swap/server_inventory as it did not pass its verify_file() method
auto declined parsing /ansible-dev/swap/server_inventory as it did not pass its verify_file() method
Set default localhost to localhost
Parsed /ansible-dev/swap/server_inventory inventory source with ini plugin
Loading callback plugin debug of type stdout, v2.0 from /usr/local/lib/python3.7/site-packages/ansible/plugins/callback/debug.py
PLAYBOOK: playbook.yaml ***********************
Positional arguments: playbook.yaml
verbosity: 4
connection: smart
timeout: 10
become_method: sudo
tags: ('all',)
inventory: ('/ansible-dev/swap/server_inventory',)
forks: 5
1 plays in playbook.yaml
PLAY [control_host] ***********************
TASK [Gathering Facts] ***********************
task path: /ansible-dev/swap/playbook.yaml:2
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: root
<localhost> EXEC /bin/sh -c 'echo ~root && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1587716569.74761-59629609450822 `" && echo ansible-tmp-1587716569.74761-59629609450822="` echo /root/.ansible/tmp/ansible-tmp-1587716569.74761-59629609450822 `" ) && sleep 0'
Using module file /usr/local/lib/python3.7/site-packages/ansible/modules/system/setup.py
<localhost> PUT /root/.ansible/tmp/ansible-local-4319jytc92b2/tmpwbji1240 TO /root/.ansible/tmp/ansible-tmp-1587716569.74761-59629609450822/AnsiballZ_setup.py
<localhost> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1587716569.74761-59629609450822/ /root/.ansible/tmp/ansible-tmp-1587716569.74761-59629609450822/AnsiballZ_setup.py && sleep 0'
<localhost> EXEC /bin/sh -c 'PYTHONPATH=/usr/local/kaas_virtualenv/lib/python3.7/site-packages /usr/local/kaas_virtualenv/bin/python3 /root/.ansible/tmp/ansible-tmp-1587716569.74761-59629609450822/AnsiballZ_setup.py && sleep 0'
ok: [localhost]
META: ran handlers
TASK [print users] ***********************
task path: /ansible-dev/swap/playbook.yaml:18
fatal: [localhost]: FAILED! => {}
MSG:
You need to install "jmespath" prior to running json_query filter
PLAY RECAP ***********************
localhost : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
使用virtualenv 已激活来检查pythom模块:
(kaas_virtualenv) bash-4.2# python -c "import jmespath"
(kaas_virtualenv) bash-4.2#
使用virtualenv 停用检查pythom模块:
bash-4.2# python -c "import jmespath"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named jmespath
bash-4.2#
存在python模块的唯一地方:
bash-4.2# find / -name jmespath
/usr/local/kaas_virtualenv/lib/python3.7/site-packages/jmespath
bash-4.2#
答案 0 :(得分:0)
可用的过滤器插件 未使用ansible_python_interpreter
。过滤器插件必须遵循Ansible安装,并具有安装Ansible的依赖关系。
过滤器始终在控制器上运行,因为模板仅在控制器上发生,否则将需要jinja2 +将所有清单+所有变量传递到远程,出于许多原因我们不这样做。