【问题标题】:Vagrant + Ansible + Python3流浪者 + Ansible + Python3
【发布时间】:2017-11-21 22:01:28
【问题描述】:

我有一个Vagrantfile,它被简化为:

Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu/xenial64"
  config.vm.boot_timeout = 900

  config.vm.define 'srv' do |srv|
    srv.vm.provision 'ansible' do |ansible|
      ansible.compatibility_mode = '2.0'
      ansible.playbook = 'playbook.yml'
    end
  end
end

当我在Gathering Facts 阶段运行vagrant provision 时,我得到/usr/bin/python: not found,因为默认情况下Ubuntu 16.04 只安装了python3 而不是Python 2.x python

我看到了几篇关于这个的旧帖子。似乎最新版本的 Ansible 支持使用 Python 3,但必须通过主机文件中的 ansible_python_interpreter=/usr/bin/python3 或在 ansible 命令行上进行配置。有什么方法可以在我的Vagrantfileplaybook.yml 文件中指定此选项?我目前没有使用 hosts 文件,我没有通过命令行运行 ansible-playbook,而是通过 Vagrant 集成运行 Ansible。

仅供参考,我正在使用 Ansible 2.4.1.0 和 Vagrant 2.0.1,这是撰写本文时的最新版本。

【问题讨论】:

    标签: python vagrant ansible vagrantfile


    【解决方案1】:

    据我所知,您可以在 Vagrant 文件中使用 extra_vars 确保将其放在 ansible 范围内。

    Vagrant.configure(2) do |config|
      config.vm.box = "ubuntu/xenial64"
      config.vm.boot_timeout = 900
    
      config.vm.define 'srv' do |srv|
        srv.vm.provision 'ansible' do |ansible|
          ansible.compatibility_mode = '2.0'
          ansible.playbook = 'playbook.yml'
          ansible.extra_vars = { ansible_python_interpreter:"/usr/bin/python2" }
        end
      end
    end
    

    在上面的块中extra_vars 被设置为ansible_python_interpreter 或者你可以像这样使用host_vars

    ansible.host_vars = {
            "default" => {
                "ansible_python_interpreter" => "/usr/bin/python2.7"
            }
        }
    

    【讨论】:

    • ansible_python_interpreter: '/usr/bin/python3'extra_vars 部分工作得很好,谢谢!
    • 出于某种奇怪的原因,Ansible 开始在我使用 Vagrant (/bin/sh: 1: /usr/local/opt/python@2/bin/python2.7: not found) 管理的本地集群上寻找我的本地 Python(通过 Homebrew 安装在 macOS 上),因此使用 ansible.extra_vars 明确定义它需要修复连接问题。我猜我错误地将解释器设置在其他地方:/
    • 啊,我才知道原因——这是我自己的愚蠢角色覆盖了ansible_python_interpreter(参见:github.com/geerlingguy/ansible-role-k8s_manifests/issues/3)。
    • 哈,一年来我第三次回到这个答案。再次感谢@julian Salas!
    猜你喜欢
    • 1970-01-01
    • 2022-01-26
    • 2014-07-04
    • 2015-02-17
    • 1970-01-01
    • 2016-12-06
    • 1970-01-01
    • 1970-01-01
    • 2012-11-03
    相关资源
    最近更新 更多