2017-03-28 4 views
0

私はいくつかのREST APIを呼び出し、いくつかのPOSTリクエストをAnsibleでサービスにしようとしています。ボディ(JSON)が変更されて以来、私はいくつかのファイルをループしようとしています。ここ脚本は次のとおりです。可能性:POSTリクエストを行うファイルのループ


- hosts: 127.0.0.1 
    any_errors_fatal: true 

    tasks: 
    - name: do post requests            
     uri: 
     url: "https://XXXX.com" 
     method: POST 
     return_content: yes 
     body_format: json 
     headers: 
      Content-Type: "application/json" 
      X-Auth-Token: "XXXXXX" 
     body: "{{ lookup('file', "{{ item }}") }}" 
     with_file: 
      - server1.json 
      - server2.json 
      - proxy.json 

しかし、私は脚本を実行すると、私はこのエラーを取得する:

the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'item' is undefined

どこに問題がありますか?

答えて

1

主な問題は、with_ディレクティブがタスクディクショナリに属していることです(インデントレベルが1つ上がる)。

第二の問題は、あなたがwith_filesでファイル検索とwith_items、または単に"{{ item }}"のいずれかを使用する必要があるということです。

- name: do post requests            
    uri: 
    url: "https://XXXX.com" 
    method: POST 
    return_content: yes 
    body_format: json 
    headers: 
     Content-Type: "application/json" 
     X-Auth-Token: "XXXXXX" 
    body: "{{ item }}" 
    with_files: 
    - server1.json 
    - server2.json 
    - proxy.json 

または

- name: do post requests            
    uri: 
    url: "https://XXXX.com" 
    method: POST 
    return_content: yes 
    body_format: json 
    headers: 
     Content-Type: "application/json" 
     X-Auth-Token: "XXXXXX" 
    body: "{{ lookup('file', item) }}" 
    with_items: 
    - server1.json 
    - server2.json 
    - proxy.json 

また、{{ ... }}コンストラクトは必須ではありません各変数を参照する方法 - それはあなたがvaを使用するJinja2式を開くコンストラクトですライバル。 {{ variable }}が、あなたがそれを開くと、あなたは再びそれを行う必要はありませんので、それは書くために完全に罰金です:単一の変数のために、それは確かになり

body: "{{ lookup('file', item) }}" 
+0

もう一つの問題。あなたが提供した解決策はうまくいきましたが、今私はこのエラーが発生します:予想されるパスで(... content of server1.json ...)を見つけることができません。 jsonファイルは、プレイブックの同じディレクトリにありますが、ここで何が問題になるでしょうか? – SegFault

+0

更新された回答を参照してください。 – techraf

+0

ありがとうございます。今それははっきりしています。 – SegFault

関連する問題