Ansible Django模块替代的Python解释器

5 投票
2 回答
545 浏览
提问于 2025-04-18 14:06

我正在写一个 ansible 剧本,用来部署一个 django 应用。在这个过程中,我想要运行一个叫做 staticcollect 的命令。

我遇到的问题是,远程服务器上有两个 Python 解释器,一个是 Python2.6,另一个是 Python2.7,而默认的是 Python2.6。

当我运行这个剧本时,它是用 Python2.6 解释器来执行的,但我需要它用 Python2.7 解释器来运行。

有没有什么办法可以做到这一点呢?

我的剧本如下:

- hosts: xxxxxxxxx
  vars:
    hg_branch:  dmv2
    django_dir: /opt/app/xxxx
    conf_file:  /opt/app/conf/xx_uwsgi.ini
    django_env:
        STATIC_ROOT: /opt/app/serve/static
  remote_user: xxxxxx
  tasks:
  - name: Update the hg repo
    command: chdir=/opt/app/xxxxx  hg pull -u --rev {{hg_branch}}
  - name: Collect static resources
    environment: django_env
    django_manage: command=collectstatic app_path={{django_dir}}
  - name: Restart the django service
    command: touch {{conf_file}}
  - name: check nginx is running
    service: name=nginx state=started

2 个回答

0

我在工作中遇到过类似的情况,最后是通过设置 ansible_python_interpreter 这个主机/组变量来解决的。

http://docs.ansible.com/faq.html#how-do-i-handle-python-pathing-not-having-a-python-2-x-in-usr-bin-python-on-a-remote-machine

有趣的是,如果需要的话,你可以为每台主机单独定义一个不同的 Python 路径。

0

/usr/bin/python 其实只是一个指向其他地方的快捷方式,可能指向 /usr/bin/python2.6 或者 /usr/bin/python2.7。你可以用一个简单的命令来更新这个快捷方式。

- name: Remove python --> python2.6 symlink
  sudo: yes
  command: rm /usr/bin/python

- name: Add python --> python2.7 symlink
  sudo: yes
  command: ln -s /usr/bin/python2.7 /usr/bin/python

撰写回答