2017-12-04 2 views
0

私は2つのリストを返す/印刷するpythonスクリプトを持っています。今不可能 - 複数の変数を登録することは可能ですか?

 -task: 
     name: get the hosts 
     command: /test.py 
     register: result 

、それが登録した変数の結果からプール1と別々にPOOL2をフェッチすることが可能である:

test.py

def getHosts(): 
     Pool1=[x.x.x.x, x.x.x.x] 
     Pool2=[x.x.x.x, x.x.x.x] 
     Print pool1,pool2 
     Return pool1,pool2 

getHosts() 

私の脚本は次のようになりますか?はいの場合は、私に例を示してください。

ご意見やご提案は高く評価されます。

+0

なぜJSONオブジェクトを作成してみませんか? – techraf

+0

@techrafあなたはPythonからJSONを返すと言っていますか?はい、私はそれを行うことができますが、どのようにJSONオブジェクトを変数から読み込むか。 (非常に新しくなってしまいました.Plsはいくつかのコードを共有しています) – Shibankar

+0

「読む方法」とはどういう意味ですか? – techraf

答えて

2

JSONをプロデュースしてAnsibleにフィードします。これは、自動的に適切なデータ構造を作成します。

--- 
- hosts: localhost 
    gather_facts: no 
    connection: local 
    tasks: 
    - command: 'echo { \"Pool1\": \"[x.x.x.x, x.x.x.x]\", \"Pool2\": \"[x.x.x.x, x.x.x.x]\" }' 
     register: my_output 
    - set_fact: 
     my_variable: "{{ my_output.stdout | from_json }}" 
    - debug: 
     msg: "Pool1 is {{ my_variable.Pool1 }} and Pool2 is {{ my_variable.Pool2 }}" 

結果:

ok: [localhost] => { 
    "msg": "Pool1 is [x.x.x.x, x.x.x.x] and Pool2 is [x.x.x.x, x.x.x.x]" 
} 

後で変数を使用する方法に応じて、あなたは/ from_jsonフィルタに必要ではない場合がありますが(this参照)。

+0

ありがとう。それはうまく動作します – Shibankar

関連する問題