【问题标题】:Is it possible to create python virtual environment with ansible playbook?是否可以使用 ansible playbook 创建 python 虚拟环境?
【发布时间】:2016-09-23 16:31:01
【问题描述】:

我曾尝试使用 ansible-local 在 vagrant VM 中创建虚拟环境,但失败了。

这是我的 Vagrant 文件:

Vagrant.configure(2) do |config|
  config.vm.provider "virtualbox" do |v|
    v.memory = 4096
    v.cpus = 2
  end

  config.vm.define "shortener" do |shortener|
    shortener.vm.box = "ubuntu/trusty64"
    # database 
    shortener.vm.network :forwarded_port, host: 3307, guest: 3306
    # browser
    shortener.vm.network :forwarded_port, host: 4568, guest: 4568
    shortener.vm.provision :ansible_local do |ansible|
        ansible.playbook = "playbook.yml"
    end
  end

  config.ssh.forward_agent = true

end

这是“playbook.yml”:

- name: Deploy shortener
  hosts: all
  become: true
  become_method: sudo

  tasks:
    - name: Install packages
      apt: update_cache=yes name={{ item }} state=present
      with_items:
    - git
    - python-pip
    - nginx-full
    - vim
    - python-virtualenv
    - virtualenvwrapper
    - python3.4
    - python3.4-doc
    - python3.4-dev
    - software-properties-common
    - python-software-properties
    - postgresql
    - postgresql-client

- name: Load virtualenvwrapper
  shell: source /etc/bash_completion.d/virtualenvwrapper

- name:  Create virtual environment               
  shell: mkvirtualenv shortener --python=/usr/bin/python3

- name: Install requirements
  pip: requirements='/vagrant/configs/requirements.txt'

这是'vagrant up'的输出:

hedin@home:~/url_shortener$ vagrant provision
==> shortener: Running provisioner: ansible_local...
    shortener: Running ansible-playbook...

PLAY [Deploy shortener]     
**************************

TASK [setup]
**************************
ok: [shortener]
**************************
TASK [Install packages]  
ok: [shortener] => (item=[u'git', u'python-pip', u'nginx-full', u'vim',   u'python-virtualenv', u'virtualenvwrapper', u'python3.4', u'python3.4-doc', u'python3.4-dev', u'software-properties-common', u'python-software-properties', u'postgresql', u'postgresql-client'])

TASK [Load virtualenvwrapper] 
**************************
fatal: [shortener]: FAILED! => {"changed": true, "cmd": "source  /etc/bash_completion.d/virtualenvwrapper", "delta": "0:00:00.003591", "end": "2016-09-23 16:06:43.169513", "failed": true, "rc": 127, "start": "2016-09-23 16:06:43.165922", "stderr": "/bin/sh: 1: source: not found", "stdout": "", "stdout_lines": [], "warnings": []}

NO MORE HOSTS LEFT
**************************
[WARNING]: Could not create retry file 'playbook.retry'.  [Errno 2] No such file or directory: ''


PLAY RECAP 
**************************
shortener                  : ok=2    changed=0    unreachable=0    failed=1

Ansible failed to complete successfully. Any error output should be visible above. Please fix these errors and try again.

我还尝试使用“command”而不是“shell”,结果相同。

我想我可以使用创建虚拟环境的 shell 脚本,但是是否可以通过 ansible 方法修复该错误?

【问题讨论】:

  • pip 模块支持 virutalenv 参数,因此您不需要在单独的任务中执行此操作 (docs)。
  • 我认为你的问题的根源可能是当你获取你的 python virtualenv 时:)。我通常在我的剧本中使用来自 virtualenv 的 python 二进制文件的完整路径,而不是采购 virtualenv。也许如果您使用mkvirtualenv 的完整路径,它会起作用。
  • @notorious.no 我不确定“mkvirtualenv 的完整路径”是什么意思,因为它只是“virtualenvwrapper.sh”中的一个函数。

标签: python vagrant ansible virtualenv


【解决方案1】:

我找到了解决办法。这是我的“playbook.yml”文件:

- name: Deploy shortener
  hosts: all
  remote_user: vagrant

 tasks:
    - name: Install packages
      become: true
      become_method: sudo        
      apt: update_cache=yes name={{ item }} state=present
      with_items:
        - git
        - python-pip
        - nginx-full
        - vim
        - python-virtualenv
        - virtualenvwrapper
        - python3.4
        - python3.4-doc
        - python3.4-dev
        - software-properties-common
        - python-software-properties
        - postgresql
        - postgresql-client

    - name: Install requirements
      become: true
      become_method: sudo        
      pip: 
        requirements: /vagrant/configs/requirements.txt
        virtualenv: /home/vagrant/.virtualenvs/shortener
        virtualenv_python: python3.4

我为此使用了标准 pip 模块。感谢提供帮助的 cmets!

【讨论】:

  • 很高兴我能帮上忙。
猜你喜欢
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 2019-10-13
  • 1970-01-01
  • 1970-01-01
  • 2020-09-03
  • 2014-01-24
相关资源
最近更新 更多