Ansible Tower またはAWXにCLIツールをインストールし、基本操作について説明します。
インストール方法
CLIツールはpython3 moduleとして提供されています。以下のコマンドでpipインストールを実施するとコマンドラインツールawxがインストールされます。
# pip3 install awxkit
以下のように引数なしで実行するとヘルプを見る事ができます。
# awx
usage: awx [--help] [--version] [--conf.host https://example.awx.org]
[--conf.token TEXT] [--conf.username TEXT] [--conf.password TEXT]
[-k] [-f {json,yaml,jq,human}] [--filter TEXT]
[--conf.color BOOLEAN] [-v]
optional arguments:
--help prints usage information for the awx tool
--version display awx CLI version
authentication:
--conf.host https://example.awx.org
--conf.token TEXT an OAuth2.0 token (get one by using `awx login`)
--conf.username TEXT
--conf.password TEXT
-k, --conf.insecure Allow insecure server connections when using SSL
<omitted>
疎通確認
AWX(Ansible Works)の場合
AWXへの接続先や認証情報などを引数に与えて、以下のように実行すると疎通確認ができます。
# awx \
--conf.host http://localhost/ \
--conf.username admin \
--conf.password P@ssw0rd \
hosts list
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"id": 1,
"type": "host",
"url": "/api/v2/hosts/1/",
"related": {
"created_by": "/api/v2/users/1/",
"modified_by": "/api/v2/users/1/",
<omitted>
Ansible Towerの場合
Ansible Towerを使用する場合も、AWXと殆ど同じように動作確認できます。違いは、httpsを使用することと証明書エラーを無視する”-k”が必要な事です。
# awx \
--conf.host https://localhost/ \
--conf.username admin \
--conf.password P@ssw0rd \
-k \
hosts list
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"id": 1,
"type": "host",
"url": "/api/v2/hosts/1/",
"related": {
"created_by": "/api/v2/users/1/",
"modified_by": "/api/v2/users/1/",
<omitted>
認証情報の操作
環境変数として定義する方法
AWX CLIに与える認証情報は環境変数として定義する事もできます。
export TOWER_HOST=http://localhost/ export TOWER_USERNAME=admin export TOWER_PASSWORD=P@ssw0rd export TOWER_VERIFY_SSL=False
設定が反映されているかどうかはawx configコマンドで確認する事もできます。
# awx config
{
"base_url": "http://localhost/",
"token": "",
"use_sessions": false,
"credentials": {
"default": {
"username": "admin",
"password": "P@ssw0rd"
}
}
}
確かに設定が反映されているかを調べるために、Ansible Towerからの情報取得が可能な事を確認します。
# awx hosts list
{
"count": 1,
"next": null,
"previous": null,
"results": [
<omitted>
認証トークンの発行
OAUTHトークンを発行して、トークンを元にログインする事もできます。以下のような操作でトークンを発行できます。
デフォルトではjson形式の出力となりますが、-f humanオプションを付与すると「export TOWER_OAUTH_TOKEN=xxxxxxxxx」というコピペしやすい形式で出力されます。
# TOWER_HOST=http://localhost/ \ # TOWER_USERNAME=admin \ # TOWER_PASSWORD=P@ssw0rd \ # awx login -f human export TOWER_OAUTH_TOKEN=5cHeahgFtIRwJfrNo6OOWY7P62DzeB
トークンを環境変数に設定し、Ansible Towerからの情報取得が可能な事を確認します。
# export TOWER_OAUTH_TOKEN=5cHeahgFtIRwJfrNo6OOWY7P62DzeB # awx --conf.host http://localhost/ hosts list
表示形式
出力フォーマット
デフォルトではjson形式の出力ですが、yaml形式やテーブル(Tabular)形式で出力する事もできます。
# awx inventory list -f yaml count: 1 next: null previous: null results: - created: '2020-08-15T12:45:31.490023Z' description: '' has_active_failures: false has_inventory_sources: false host_filter: null hosts_with_active_failures: 0 id: 1 insights_credential: null inventory_sources_with_failures: 0 kind: '' modified: '2020-08-15T12:45:31.490049Z' name: Demo Inventory organization: 1 pending_deletion: false <omitted> # awx inventory list -f human id name == ============== 1 Demo Inventory #
列選択
テーブル形式の場合は列選択の機能があります。どちらかと言えば、デフォルトで出力される列が少ないので、「列を選択」ではなく「列を追加」するために使う用途の方が多いと予想されます。
# awx projects list -f human id name == ============ 6 Demo Project # awx projects list -f human --filter id,name,description,scm_type,scm_url id name description scm_type scm_url == ============ =========== ======== ================================================ 6 Demo Project git https://github.com/ansible/ansible-tower-samples
jqフィルター
jq形式で出力し、さらにfilterオプションを与えると、jqコマンドと同じ書式での整形が可能です。
# awx users list -f jq --filter '.results[] | {id,username,emal,is_superuser}'
{"id": 1, "username": "admin", "emal": null, "is_superuser": true}
ファイル指定
秘密鍵のようなファイルを指定する場合は、以下のような書式でファイルパスを指定する事もできます。
# awx credentials create \
--credential_type 'Machine' \
--organization 'Default' \
--name 'Public Key Authentication4' \
--inputs '{"username": "root", "ssh_key_data": "@~/.ssh/id_rsa"}'
公式情報引用元
トップページ
「Ansible Docs」および「AWX ProjectのREADME.md」が公式情報です。
AWXコマンドのインストール方法
AWXコマンドのインストールは「AWX ProjectのINSTALL.md」を参照ください。
認証方法の設定
Ansible Docsのトップページから、Ansible Tower Docs, Authenticationの順に画面遷移ください。
