てくなべ (tekunabe)

ansible / network automation / 学習メモ

[Ansible/AWX] AWX コマンドで git プロジェクトを更新する

はじめに

先日以下のツイートを拝見しました。煩わしさ同感です。

その後、以下の記事が公開され、GitLab 側から AWX の API を叩く方法が紹介されていました。

https://nnstt1.hatenablog.com/entry/2020/05/17/224049

GitLab に Playbook を push すると、自動で AWS 側のプロジェクトが更新されるので、実施漏れもなく良いと思いました。

一方で「そこまで仕込む程でもないけど、とにかく画面上で操作するのが手間」というレベル感の場合のあるかと思います。

この記事では、awx コマンドでプロジェクトを更新する方法(awx project update プロジェクト名)を紹介します。


awx コマンドのインストールと設定

インストールや認証情報の設定などは以下の記事を参照してください。

tekunabe.hatenablog.jp


動作検証

実際に試してみます。

Playbook の追加

まずリポジトリ上(今回は GitHub)に新しく Playbook を追加します。

ここでは show2.yml とします。

f:id:akira6592:20200518082122p:plain
show2.yml の追加

この時点では、該当プロジェクトを使用するジョブテンプレートでは、show2.yml は選択できません。

f:id:akira6592:20200518082245p:plain
更新前なのでまだ show2.yml を選択できない

awx コマンドでプロジェクト更新

ここで、awx コマンドでプロジェクト更新します。

書式は、awx project update プロジェクト名 です。

今回の該当のプロジェクト名は pj_tower-sample-nw です。

$ awx project update pj_tower-sample-nw
{
     "project_update": 1239,
     "id": 1239,
     "type": "project_update",
     "url": "/api/v2/project_updates/1239/",
     "summary_fields": {
          "organization": {
               "id": 2,
               "name": "test_org",
               "description": "test organization"
          },
          "project": {
               "id": 13,
               "name": "pj_tower-sample-nw",
               "description": "tower-sample-nw",
               "status": "pending",
               "scm_type": "git"
          },
          "unified_job_template": {
               "id": 13,
               "name": "pj_tower-sample-nw",
               "description": "tower-sample-nw",
               "unified_job_type": "project_update"
          },
          "created_by": {
               "id": 1,
               "username": "admin",
               "first_name": "",
               "last_name": ""
          },
          "modified_by": {
               "id": 1,
               "username": "admin",
               "first_name": "",
               "last_name": ""
          },
          "user_capabilities": {
               "delete": true,
               "start": true
          }
     },
     "created": "2020-05-17T23:23:07.364032Z",
     "modified": "2020-05-17T23:23:07.400366Z",
     "name": "pj_tower-sample-nw",
     "description": "tower-sample-nw",
     "local_path": "_13__pj_tower_sample_nw",
     "scm_type": "git",
     "scm_url": "https://github.com/akira6592/tower-sample-nw.git",
     "scm_branch": "master",
     "scm_refspec": "",
     "scm_clean": true,
     "scm_delete_on_update": true,
     "credential": null,
     "timeout": 0,
     "scm_revision": "",
     "unified_job_template": 13,
     "launch_type": "manual",
     "status": "pending",
     "failed": false,
     "started": null,
     "finished": null,
     "canceled_on": null,
     "elapsed": 0.0,
     "job_args": "",
     "job_cwd": "",
     "job_env": {},
     "job_explanation": "",
     "execution_node": "",
     "result_traceback": "",
     "event_processing_finished": false,
     "project": 13,
     "job_type": "check",
     "job_tags": "update_git,delete"
}

なお、 -f human` オプションを追加すると、出力がすっきりします。

$ awx project update pj_tower-sample-nw -f human
id   name               
==== ================== 
1240 pj_tower-sample-nw 

確認

もう一度、該当プロジェクトを使用するジョブテンプレートを確認すると、show2.yml が選択できるようになりました。

f:id:akira6592:20200518082437p:plain
show2.yml が選択できるようになった


おわりに

Playbook の開発段階などでは、繰り返し発生する作業は、GUI よりコマンドのほうが便利かと思います。

curlAPI 叩くのでもよいかもしれませんが、API だと対象オブジェクト(プロジェクトなど)を內部で管理している id で指定する必要があります。awx コマンドでは名前で指定できるので便利です。