【问题标题】:run local python script inside virtual environment using yaml file使用 yaml 文件在虚拟环境中运行本地 python 脚本
【发布时间】:2023-03-31 19:13:02
【问题描述】:

我想在虚拟环境中运行我的本地 python 脚本。这是我尝试过的代码。 我已经创建了虚拟环境并安装了 Cassandra-driver

---
- hosts: localhost
  gather_facts: no
  connection: local
  name: install cassendra-driver
  become: true
  become_user: root
  vars:
    PROJECT_HOME: "/home/hari/Desktop/venvs"
  tasks:
          - name: Run a script using an executable in virtual environment
                    script: "/space/code_base/add_new_column_to_table.py"
                    virtualenv: "{{ PROJECT_HOME }}/myenv"
                    args:
                        executable: python2

但我遇到了错误

  • 错误!加载 YAML 时出现语法错误。 在这种情况下不允许映射值

错误似乎在“/home/hari/Desktop/cassendra-driver_2.yml”中:第 30 行,第 27 列,但可能 根据确切的语法问题,位于文件中的其他位置。

违规行似乎是:

      - name: Run a script using an executable in virtual environment
                script: "/space/code_base/add_new_column_to_table.py"
                      ^ here

*

有人可以帮我吗?

我的要求是在虚拟环境中运行本地 python 脚本。

【问题讨论】:

  • 我建议您阅读"Learn Yaml in Y minutes",在那里您将了解到 yaml 是“JSON 的严格超集,添加了 在语法上重要的换行符和缩进,就像 Python”。这应该可以帮助你修复你的剧本和这个 yaml 语法错误。
  • 谢谢。更新。 ERROR! 'virtualenv' is not a valid attribute for a Task The error appears to be in '/home/hari/Desktop/cassendra-driver_2.yml': line 29, column 13, but may be elsewhere in the file depending on the exact syntax problem. The offending line appears to be: virtualenv: "{{ PROJECT_HOME }}/myenv" - name: Run a script using an executable in virtual environment ^ here This error can be suppressed as a warning using the "invalid_task_attribute_failed" configuration 但想在虚拟环境中执行我的脚本。
  • 用于安装 Cassandra-driver,m 属性可以正常工作。但是对于运行脚本会抛出错误。是否有任何其他属性可以用来代替 virtualenv.

标签: python ansible yaml


【解决方案1】:

我无权访问您的环境,但这样的事情应该可以工作:

tasks:
- command: {{ PROJECT_HOME }}/myenv/bin/python /space/code_base/add_new_column_to_table.py

【讨论】:

    猜你喜欢
    • 2019-03-06
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    • 1970-01-01
    • 2020-07-05
    • 2022-01-11
    • 1970-01-01
    • 2020-02-10
    相关资源
    最近更新 更多