2016-12-14 6 views
1

私はPythonとMySQL DBとを統合しています。私のユースケースの一部は、Anabilitiesにグループ名が与えられたときに、そのグループ名をPythonに送信します。このPythonにはdb読み取りが行われ、そのグループ名に対応するIPのリストが返されます。 テストでは、返されたIPに対してpingを実行したいと思います。私は2番目のプレイのためのhosts:としてではなく任意の成功なしip_listに格納された値を設定しようとしていますPythonからAnabilitiesのホスト属性を動的に割り当てる

name: run a cmd 
hosts: localhost 
connection: local 
tasks: 
    name: runs a python script with a parameter 
     shell: python /pythonScripts/AnsibleDBRead.py <someGroupName> 
     register: py_ret 
    - set_fact: 
     ip_list: "{{py_ret.stdout}}" 
    - debug: var=hostvars['localhost']['ip_list'] # option to set messages here as well but not both together 


name: png the hosts returned 
hosts: hostvars['localhost']['ip_list'] #this does not work 
#hosts: [ "127.0.0.1", "54.147.177.9"] #this works same value but hardcoded 
tasks: 
    - debug: var=hostvars['localhost']['ip_list'] # able to print the value 

は、ここで同じことを達成するための私の脚本です。私が得るエラーは、一致したホストがないということです。ハードコーディングされた部分がコメントされた出力を出力します。スクリプトの書式を無視します。私はhostvarsを使用して別のプレイで1回のプレイで宣言された変数にアクセスすることができるはず読んだものから

PLAY [run a cmd] *************************************************************** 

TASK [setup] ******************************************************************* 
ok: [localhost] 

TASK [runs a python script with a parameter] *********************************** 
changed: [localhost] 

TASK [set_fact] **************************************************************** 
ok: [localhost] 

TASK [debug] ******************************************************************* 
ok: [localhost] => { 
    "hostvars['localhost']['ip_list']": [ 
     "127.0.0.1", 
     "54.147.177.9" 
    ] 
} 

PLAY [png the hosts returned] ************************************************** 
skipping: no hosts matched 

PLAY RECAP ********************************************************************* 
localhost     : ok=4 changed=1 unreachable=0 failed=0 

。どんな助けもありがとうございます。

答えて

1

いくつかの試行錯誤のテストの後、私はこれがあなたのために働くべきだと思う:

- name: ping the hosts returned 
    hosts: "{{ hostvars['localhost']['ip_list'] | join(',') }}" 
    tasks: 
    - debug: 

そして、それは既知の問題だと思われる:pass array as "hosts" in playbook #16051と回避策。

+0

ありがとうございます!それは私のために働いた。あなたは、pythonから返された値がすでにコンマで区切られているので、 "join"の意義を説明できますか? – shshnk

+0

'ip_list'の入力はカンマで区切られていましたが、リストオブジェクトに変換されていますので、' ip_list'はリストオブジェクトです。 join( '、') 'は文字列に変換します。 – techraf

関連する問題