【问题标题】:executing python script with args pars via ansible通过ansible执行带有args pars的python脚本
【发布时间】:2019-12-10 14:57:34
【问题描述】:

我正在尝试通过 ansible 使用 args 解析器执行 python 脚本 我想把我所有的论点放在一个 extra_var 中,但我错过了一些东西

假设我的python脚本可以获得用户名-u和密码-p 我的 ansible 脚本有一个 var my_args

script: /tmp/args.py "{{ my_args }}"

当我像这样运行我的剧本时:

ansible-playbook my_ansible_playbook.yml -e "my_args='-u my_username -p my_password'"

我得到的结果是:

用户名 = my_username -p my_password

密码 = default_password

我错过了什么?

如何使用单个 extra_var 将每个值发送到正确的值?

【问题讨论】:

  • 试试这个方法,看看它是否改变了什么:script: "/tmp/args.py {{ my_args }}"
  • 感谢@Zeitounator,我尝试了很多选择,我不敢相信我错过了这个

标签: python ansible yaml


【解决方案1】:

如果你不引用整个脚本值,yaml 将它视为一个包含双引号的字符串,其中包含一个值(稍后由 jinja2 解释)。最后,您的脚本被调用,其中一个参数是您的完整模板字符串。

要在尝试时传递所有参数,您需要引用整个字符串。

请参见以下示例:

---
- hosts: localhost
  gather_facts: false

  tasks:
    - name: Show the extra var itself
      debug:
        var: myvar

    - name: Quotes in command (wrong)
      debug:
        msg: myscript.py "{{ myvar }}"

    - name: No quotes (good) - simple command with params
      debug:
        msg: "myscript.py {{ myvar }}"

结果:

$ ansible-playbook tmp.yml -e "myvar='-u toto -p bingo'"

PLAY [localhost] **************************************************************************************************************************************************************************************************

TASK [Show the extra var itself] **********************************************************************************************************************************************************************************
ok: [localhost] => {
    "myvar": "-u toto -p bingo"
}

TASK [Quotes in command (wrong)] **********************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "myscript.py \"-u toto -p bingo\""
}

TASK [No quotes (good) - simple command with params] **************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "myscript.py -u toto -p bingo"
}

PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

【讨论】:

    猜你喜欢
    • 2016-05-10
    • 1970-01-01
    • 2012-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-18
    相关资源
    最近更新 更多