似たようなモジュールでより汎用的なものとして uri があります。これはREST APIを叩いたり、WEBの動作確認など様々な使い方ができます。いわゆる curl コマンドのAnsibleモジュール版といっても良いでしょう。汎用HTTPリクエスト機能とも言いかえられるかもしれません。
このモジュールは shell などと同じく、冪等性を考慮していない汎用コマンド系なのでその点は注意が必要です。
公式のサンプルを少し見てみましょう。
指定URLへGETを送ります。応答がなければエラーです。とりあえず動いているか、動いていなかを確認できます。
- name: Check that you can connect (GET) to a page and it returns a status 200
uri:
url: http://www.example.com
指定のURLへGETを送りコンテンツを取得します。そのコンテンツ内に AWESOME という文字列がなければエラーになります。すごく便利です。
- uri:
url: http://www.example.com
return_content: yes
register: this
failed_when: "'AWESOME' not in this.content"
REST APIを叩く例です。bodyにデータを格納してPOSTしています。
- name: Create a JIRA issue
uri:
url: https://your.jira.example.com/rest/api/2/issue/
method: POST
user: your_username
password: your_pass
body: "{{ lookup('file','issue.json') }}"
force_basic_auth: yes
status_code: 201
body_format: json
ログインできるかを確認しています。最近はこんな単純な方法でログインできるサイトも少ないと思うので、あまり出番はないかと思います。
- uri:
url: https://your.form.based.auth.example.com/index.php
method: POST
body_format: form-urlencoded
body:
name: your_username
password: your_password
enter: Sign in
status_code: 302
register: login
- uri:
url: https://your.form.based.auth.example.com/index.php
method: POST
body_format: form-urlencoded
body:
- [ name, your_username ]
- [ password, your_password ]
- [ enter, Sign in ]
status_code: 302
register: login
その他にも、期待するリターンコードを指定して、それ以外はエラーにするなども可能で、とても汎用的なモジュールです。
0 件のコメント:
コメントを投稿