2017-06-10 118 views
0

我有一個項目,需要我從遠程服務器獲取多個文件的內容並傳入文件。通過web服務器上的多個文件進行交互

這裏是任務,我創建了用於實現該目標:

- name: check content 
    uri: 
    url: http://172.16.2.3/file1 
    method: GET 
    return_content: yes 
    register: file1 

    - name: print content 
    debug: msg={{file1.content}} 

調試內容顯示以下

ok: [172.16.2.4] => { 
"changed": false, 
"msg": "testcontent11111\n" 

但是因爲我必須通過多個文件循環,我試過如下:

- name: check content 
    uri: 
    url: http://172.16.2.3/{{item.url}} 
    method: GET 
    return_content: yes 
    register: {{item.register}} 
    with_items: 
    - { url: 'file1', register: 'file1' } 

    - name: print content 
    debug: msg={{file1.content}} 

不幸的是調試上面的內容在m y輸出

FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'file1' is undefined\ 

任何想法如何我可以寫第二個任務來完成第一個目標?

+1

的可能的複製[註冊變量與\ _items在Ansible劇本環路(https://stackoverflow.com/questions/29512443/register-variables-in-with-items-loop -in-ansible-playbook) - 並且無法在循環中註冊具有任意名稱的變量。 – techraf

回答

0

我不認爲有可能使用動態名稱註冊var。

http://docs.ansible.com/ansible/playbooks_loops.html#using-register-with-a-loop

- name: check content 
    uri: 
    url: http://172.16.2.3/{{item.url}} 
    method: GET 
    return_content: yes 
    register: files 
    with_items: 
    - { url: 'file1', register: 'file1' } 
    - { url: 'file2', register: 'file2' } 

    - name: print content 
    debug: msg={{ files.results }} # First item = content of file1 and so on 
相關問題