已搜索該站點,但無法找到答案。有誰知道如何將變數從 Ansible 劇本傳遞到 python。
這是我的基本測驗代碼
---
- hosts: localhost
gather_facts: false
vars:
numbers:
- 1
- 2
tasks:
- name: Run Python Script
script: pythontest.py
register: output
- debug:
var: output
和我的基本 python 腳本
#!/usr/bin/python3.6
print('hello world')
print(numbers)
嘗試了多種方法將變數“數字”移植到 python,但沒有運氣,有人有什么想法嗎?
uj5u.com熱心網友回復:
當你在 python 腳本中使用 arg 時,你只能使用string
args,所以如果你想使用串列或字典,我建議你使用 json 字串:
#!/usr/bin/python3.6
import sys
import json
numbers=json.loads(sys.argv[1])
print('hello world')
print(numbers)
print(numbers[0])
劇本:
- hosts: localhost
gather_facts: false
vars:
numbers: >-
[1,2]
tasks:
- name: Run Python Script
script: pythontest.py {{numbers}}
register: output
- debug:
var: output
輸出:
ok: [localhost] => {
"output": {
"changed": true,
"failed": false,
"rc": 0,
"stderr": "",
"stderr_lines": [],
"stdout": "hello world\n[1, 2]\n1\n",
"stdout_lines": [
"hello world",
"[1, 2]",
"1"
]
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/494954.html
上一篇:具有多個要使用的值的變數