Ansible“ansible_python_interpreter”错误

tha*_*lin 6 python python-3.x ansible influxdb

我想安装 influxdb 并使用 ansible 配置。文件复制和 influxdb 配置正常,但创建数据库和用户创建部分会出现“ansible_python_interpreter”错误。

我搜索了这个错误并尝试了一些东西,但我无法自己解决这个问题

这是我的 ansible 主机文件

[loadbalancer]
lb      ansible_host=192.168.255.134

[loadbalancer:vars]
ansible_python_interpreter="/usr/bin/python3"
#ansible_python_interpreter="/usr/bin/env python"
#ansible_python_interpreter="/usr/libexec/platform-python"
Run Code Online (Sandbox Code Playgroud)

这是我的 yaml 文件

# influxdb install and configuration

---
  - hosts: lb
    become: true
    tasks:
      - name: Copy Repo Files
        copy:
          src: ./files/influxdb.j2
          dest: /etc/yum.repos.d/influxdb.repo
          remote_src: no
      - name: Install Influxdb
        yum:
          name: influxdb
          state: latest
        notify:
             influxdb_ok
      - name: Crete Database
        community.general.influxdb_database:
          hostname: 192.168.255.134
          database_name: deneme
      - name: Create User
        community.general.influxdb_user:
          user_name: deneme_user
          user_password: deneme123

    handlers:
      - name: Start Influx Service
        service:
          name: influxdb
          state: started
          enabled: yes
        listen: influxdb_ok
Run Code Online (Sandbox Code Playgroud)

我试图安装 python3 远程虚拟机(lb)。我试图更改解释器参数。我尝试使用 pip3 安装请求模块。

[root@centos8 influx]# ansible-playbook influxdb.yaml -K
BECOME password:

PLAY [lb] ***********************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************
ok: [lb]

TASK [Copy Repo Files] **********************************************************************************************
ok: [lb]

TASK [Install Influxdb] *********************************************************************************************
ok: [lb]

TASK [Crete Database] ***********************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ModuleNotFoundError: No module named 'requests'
fatal: [lb]: FAILED! => {"changed": false, "msg": "Failed to import the required Python library (requests) on loadbalancer.servicepark.local's Python /usr/bin/python3. Please read the module documentation and install it in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter"}

PLAY RECAP **********************************************************************************************************
lb                         : ok=3    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
Run Code Online (Sandbox Code Playgroud)

我尝试安装 requests 模块和当前的 ansible 版本

现在我的ansible机器版本

[root@centos8 influx]# python3 --version
Python 3.6.8
[root@centos8 influx]# ansible --version
ansible 2.10.1
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Apr 16 2020, 01:36:27) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
Run Code Online (Sandbox Code Playgroud)

lb vm 的版本

[root@loadbalancer ~]# influx --version
InfluxDB shell version: 1.8.2
[root@loadbalancer ~]# python3 --version
Python 3.6.8
Run Code Online (Sandbox Code Playgroud)

小智 16

如果您在远程主机上遇到此问题,可以通过 3 种方法解决:

\n
    \n
  1. 为所有默认安装了 python3 的主机设置 ansible_python_interpreter: /usr/bin/python3 变量
  2. \n
  3. 使用 Ansible\xe2\x80\x99s 原始模块安装 Python 2
  4. \n
  5. 使用 Ansible\xe2\x80\x99s 原始模块将 /usr/bin/python3 符号链接到 /usr/bin/python。
  6. \n
\n

所有 3 个选项都可以在 Ansible 中完成,无需 ssh 到主机。

\n

例子

\n
- name: misc task on ubuntu 18.04 instance\n  hosts: "*"\n  vars:\n    ansible_python_interpreter: /usr/bin/python3\n  tasks:\n    - debug: var=ansible_host\n
Run Code Online (Sandbox Code Playgroud)\n

选项 3 - 使用 Ansible\xe2\x80\x99s 原始模块的符号链接 /usr/bin/python -> /usr/bin/python3\n与选项 2 类似的另一个选项是使用原始模块到 \xe2\x80\ x9csymlink\xe2\x80\x9d /usr/bin/python -> /usr/bin/python3.

\n

借助一些 shell 魔法,我们可以使用条件语句创建一个命令,根据两个文件是否存在来有条件地执行此操作:

\n
if [ -f /usr/bin/python3 ] && [ ! -f /usr/bin/python ]; then \n  ln --symbolic /usr/bin/python3 /usr/bin/python; \nfi\n
Run Code Online (Sandbox Code Playgroud)\n


小智 5

这对我有用。

- name: Install requests python package
  pip:
    name: requests
  vars:
    ansible_python_interpreter: /usr/bin/python3
Run Code Online (Sandbox Code Playgroud)