【已解决】 “discovered_interpreter_python“: “/usr/bin/python“

目录

一、背景

1、ansible的部署情况:

2、需求:

3、报错:

二、问题分析

三、解决

1、解决错误一

2、解决错误二

3、解决错误三


一、背景

        最近用ansible-playbook 适配,麒麟、统信服务端操作系统,Centos 8。

关于oracle 19c适配麒麟、统信的可以参考我的文章 银河麒麟v10 服务器 和统信20 1050e 服务器 安装oracle 19c实战(适配成功)_太阳花先生可爱多的博客-CSDN博客

1、ansible的部署情况:

        当前部署了ansible的服务器是centos 7.9  :安装的ansible版本是 2.7.8  python 是 2.7.5

2、需求:

        从当前机器 部署服务到银河麒麟服务器、统信服务器、centos8 服务器上,

3、报错:

使用 yum、package或者dnf 安装rpm包时报错:

错误一: ansible-playbook执行 playbook剧本时没有基于python2的dnf或者yum

        剧本片段:

- name: install 麒麟 rpm from a local file
  yum:
    name: "{
   
   { rpm_lists.stdout_lines|join(',') }}"
    state: present
  when: 
    - ansible_pkg_mgr in ["yum","dnf"] 

        执行后报错信息:

{"changed": false, "cmd": "dnf install -y python2-dnf", 
"msg": "Error: Unable to find a match: python2-dnf", "rc": 1, 
"stderr": "Error: Unable to find a match: python2-dnf\n", 
"stderr_lines": ["Error: Unable to find a match: python2-dnf"], 
"stdout": "Last metadata expiration check: 0:39:16 ago on Thu 07 Jul 2022 08:56:45 AM CST.\nNo match for argument: python2-dnf\n",
 "stdout_lines": ["Last metadata expiration check: 0:39:16 ago on Thu 07 Jul 2022 08:56:45 AM CST.", "No match for argument: python2-dnf"]}

错误二: ansible 直接执行命令时报错

 FAILED! => {    
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
}

错误三:警告

[WARNING]: Platform linux on host *.*.*.* is using the discovered Python interpreter at
/usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.

二、问题分析

        由于统信、银河麒麟、centos 8 中都默认使用的是python3 同时服务器中还保留了python2的版本;加上ansible和ansible-playbook 是部署在centos7上,使用的是python2;所以在执行ansible和ansible-playbook时报错了。 

三、解决

1、解决错误一

        只需要在task中添加一个变量  ansible_python_interpreter  其值为python3的路径 /usr/bin/python3    

        作用是指定python解释器

方法一:playbook中指定局部变量(推荐)

- name: install 麒麟 rpm from a local file
  yum:
    name: "{
   
   { rpm_lists.stdout_lines|join(',') }}"
    state: present
####就是这个位置,是个局部变量
  vars:
    ansible_python_interpreter: /usr/bin/python3
####就是上面的位置,是个局部变量
  when: 
    - ansible_pkg_mgr in ["yum","dnf"] 

 方法二:playbook中指定全局变量(推荐)

        如果需要是全局变量的话,就在playbook中的全局vars中定义:

---
- hosts: "test_hosts"
  become: yes
### 没错就是这个地方,全局的
  vars:
     ansible_python_interpreter: /usr/bin/python3

方法三:运行时通过-e 参数添加

ansible test_host -m ping -e 'ansible_python_interpreter=/usr/bin/python3'
ansible-playbook test_playbook.yml -e 'ansible_python_interpreter=/usr/bin/python3'

方法四:在ansible的hosts文件中添加

[test_hosts]
192.168.200.222 ansible_ssh_user=root ansible_ssh_pass=123456 ansible_python_interpreter=/usr/bin/python3

2、解决错误二

        其实在解决错误一时,加的变量也解决了此错误,同样是因为没有找到python的解释器

另外在执行ansible命令是可以加上这样一个参数

3、解决错误三

        错误三其实是个警告,可以不用处理,也可以通过解决错误一的方法给解决掉。

当然还有一种方法,就是在ansible.cfg的[defaults]下添加配置(不推荐):

interpreter_python = auto_legacy_silent
[defaults]

# some basic default values...

#inventory      = /etc/ansible/hosts
#library        = /usr/share/my_modules/
#module_utils   = /usr/share/my_module_utils/
#remote_tmp     = ~/.ansible/tmp
#local_tmp      = ~/.ansible/tmp
#plugin_filters_cfg = /etc/ansible/plugin_filters.yml
#forks          = 5
#poll_interval  = 15
#sudo_user      = root
#ask_sudo_pass = True
#ask_pass      = True
#transport      = smart
#remote_port    = 22
#module_lang    = C
#module_set_locale = False
### 就是这里添加,也可以往上面放放,但是要在[defaults]中
interpreter_python = auto_legacy_silent

猜你喜欢

转载自blog.csdn.net/yy4545/article/details/125652470