繁体   English   中英

通过ansible使用args解析执行python脚本

[英]executing python script with args pars via ansible

我正在尝试通过 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 将每个值发送到正确的值?

如果您不引用整个脚本值,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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM