Ansible似乎没有从清单文件中加载ansible_python_interpreter = / usr / bin / python3变量

时间:2019-07-16 13:42:47

标签: ansible ansible-2.x

当我将主机的ansible_python_interpreter的值设置为/ usr / bin / python3时,ansible-playbook仍似乎使用/ usr / bin / python指向python2.7 我是从-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库(请求)”

1 个答案:

答案 0 :(得分:0)

变量必须与主机位于同一行。

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