1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Ansible Tower】ジョブテンプレートをリモートから実行したい

Last updated at Posted at 2021-08-10

はじめに

  • バージョン
    • Ansible Tower:3.7.1
    • Ansible(実行サーバ側):2.9.7
  • 概要
    • Ansible Towerを他のサービスと連携させるために、job templateをリモートから実行したい場面が
      あります。今回はその方法を以下、2パターン紹介します。
      1. uriモジュールを使用する場合
      2. tower_job_launchモジュールを使用する場合
  • 条件
    • リモートから実行するjob template:jb_test
    • Ansible Towerのアドレス:1.1.1.1

1. uriモジュールを使用する場合

 以下に実際に作成したplaybookとポイントをまとめました。今回はPOSTメソッドを使うので、
body_format: jsonを入れたくなるのですが、ポイント4に書いたようにエラーの原因となるので
注意してください。
 ちなみに、uriモジュールの内容に対応するcurlコマンドは以下です。

curl -k -H 'Content-Type: application/json' -X POST \
--user admin:password \
https://1.1.1.1/api/v2/job_templates/10/launch/

1-1. playbook

  • ポイント
  1. 使用可能なAPIはdocumentで調べる(Tower API リファレンスガイド - v3.7.1
  2. job templateのidは、Ansible Towerでjob templeteの編集画面のURLを見ればわかる
    • 以下のように、job_template/{{ id }}となっている
    • https://{{ tower_addr }}/#/templates/job_template/10
  3. 正常終了したときのstatus codeが200ではない場合があるので注意。documentのResponsesを参照すること。
  4. uriモジュールのパラメータでbody_format: jsonを入れるとエラーになる
  • playbook
tower_exec_template_1.yml
---
- hosts: localhost
  gather_facts: false
  connection: local
  vars:
    tower_addr: "1.1.1.1"
    tower_user: "admin"
    tower_pass: "password"
  tasks:
    - name: post api
      uri:
        url: "https://{{ tower_addr }}/api/v2/job_templates/10/launch/" # ポイント(1),(2)
        method: "POST"
        headers:
          Content-Type: "application/json"
        user: "{{ tower_user }}"
        password: "{{ tower_pass }}"
        force_basic_auth: yes
        validate_certs: false
        status_code:
          - 201 # ポイント(3)
      register: result

    - name: debug
      debug:
        var: result.json.id

1-2. 実行結果

  • ジョブの確認

    • 実行されたjobの番号はresult.json.idに格納されています。
(venv) [centos@ip-<ip addr> ansible]$ ansible-playbook tower_exec_template_1.yml 

PLAY [localhost] *******************************************************************************************************

TASK [post api] ********************************************************************************************************
ok: [localhost]

TASK [debug] ***********************************************************************************************************
ok: [localhost] => 
  result.json.id: '90'

PLAY RECAP *************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 
  • Ansible Tower操作画面

    • 想定通り、90番のジョブが成功していることがわかります。

210810_tower_result.PNG

2. tower_job_launchモジュールを使用する場合

 tower_job_launchモジュールを使用する場合は、以下のようにansible-tower-cliのインストールが必要になります。

pip install ansible-tower-cli

2-1. playbook

  • ポイント

  • 特筆すべき点はないですが、collectionのawx.awx.job_launchとパラメータの内容が異なるので注意してください。

  • playbook

tower_exec_template_2.yml
---
- hosts: localhost
  gather_facts: false
  connection: local
  vars:
    tower_addr: "1.1.1.1"
    tower_user: "admin"
    tower_pass: "password"
  tasks:
    - name: launch job
      tower_job_launch:
        job_template: "jb_test"
        tower_host: "https://{{ tower_addr }}"
        tower_username: "{{ tower_user }}"
        tower_password: "{{ tower_pass }}"
        validate_certs: false
      register: result

    - name: debug
      debug:
        var: result.id

2-2. 実行結果

  • ジョブの確認

    • 実行されたjobの番号はresult.idに格納されています。
(venv) [centos@ip-<ip addr> ansible]$ ansible-playbook tower_exec_template_2.yml 

PLAY [localhost] *******************************************************************************************************

TASK [launch job] ******************************************************************************************************
changed: [localhost]

TASK [debug] ***********************************************************************************************************
ok: [localhost] => 
  result.id: '104'

PLAY RECAP *************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

参考記事

Tower API リファレンスガイド - v3.7.1
Ansible Documentation - uri
Ansible Documentation - awx.awx.job_launch
Ansible Documentation - tower_job_launch

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?