【问题标题】:Ansible does not appear to be loading the ansible_python_interpreter=/usr/bin/python3 variable from the inventory fileAnsible 似乎没有从清单文件中加载 ansible_python_interpreter=/usr/bin/python3 变量
【发布时间】:2019-07-16 13:42:47
【问题描述】:

当我将主机的 ansible_python_interpreter 的值设置为 /usr/bin/python3 时,ansible-playbook 似乎仍在使用指向 python2.7 的 /usr/bin/python 我从输出显示的 -vvv 得到这个

Using module file /usr/lib/python2.7/site-packages/ansible/modules/cloud/vmware/vmware_guest.py

我在我的 yml 文件中进行了调试,用于输出变量并返回:

"ansible_python_interpreter": "/usr/bin/python3"

ansible --version
ansible 2.8.2
   config file = /ansible/automation/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/local/bin/ansible
  python version = 3.6.8 (default, Apr 25 2019, 21:02:35) [GCC 4.8.5 
20150623 (Red Hat 4.8.5-36)]

我已将库存文件中的位置转移为第一个变量,并将其添加为组变量,但没有任何乐趣。

当我使用 -e ansible_python_interpreter=/usr/bin/python3 运行 ansible-playbook 命令时,脚本运行正确

库存文件

[control] <REDACTED HOST NAME> ansible_connection=local ansible_python_interpreter=/usr/bin/python3

yml 摘录

--- - hosts: control gather_facts: false tasks: - name: Debug a variable debug: var: ansible_python_intrepreter - name: Clone a virtual machine from Windows template and customize vmware_guest: hostname: "{{ hostname }}" username: "{{ username }}" password: "{{ password }}" validate_certs: no datacenter: some-datacenter cluster: some-cluster folder: "some-folder" name: some-host template: some-template datastore: "some-datastore" networks: - name: some-network-name ip: x.x.x.x netmask: 255.255.255.0 gateway: x.x.x.x mac: aa:bb:dd:aa:00:14 domain: domain.com dns_servers: - x.x.x.x - x.x.x.x

进程运行时出现此错误 "无法在 Python /usr/bin/python2 上导入所需的 Python 库(请求)"

【问题讨论】:

    标签: ansible ansible-2.x


    【解决方案1】:

    变量必须与主机在同一行。

    [control]
    REDACTED_HOST_NAME ansible_connection=local ansible_python_interpreter=/usr/bin/python3
    

    使用group_vars更方便

    [control]
    REDACTED_HOST_NAME
    [control:vars]
    ansible_connection=local
    ansible_python_interpreter=/usr/bin/python3
    

    主机

    localhost
    [control]
    REDACTED_HOST_NAME
    [control:vars]
    ansible_connection=local
    ansible_python_interpreter=/usr/bin/python3
    

    剧情

    - hosts: localhost
      gather_facts: no
      tasks:
        - debug:
            msg: "{{ hostvars['REDACTED_HOST_NAME'].ansible_python_interpreter }}"
    

    给予

    ok: [localhost] => {
        "msg": "/usr/bin/python3"
    }
    

    ,但下面有主机

    localhost
    [control]
    REDACTED_HOST_NAME ansible_connection=local
    ansible_python_interpreter=/usr/bin/python3
    

    同样的播放失败

    'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_python_interpreter'
    

    【讨论】:

    • 变量在一行。由于某种原因,当我将第二个变量放入帖子时,它掉到了第二行。调试确实按预期返回,它是 yml 的其余部分恢复到 python2。我只是通过从 /usr/local/bin 显式调用它来运行 ansible-playbook 命令,并且活动成功完成。 “which ansible-playbook”命令声称它位于 /usr/local/bin 中,因此它应该运行 python3.6。我运行 ansible-playbook --version 并使用 python2.7 返回,但如果我运行 /usr/local/bin/ansible-playbook 它返回 python3.6
    猜你喜欢
    • 2017-01-04
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 2019-12-11
    • 2020-01-22
    • 2020-04-30
    • 2020-06-13
    • 1970-01-01
    相关资源
    最近更新 更多