Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

Ansible TowerとAWXのAPIユーザー認証方法まとめ

Ansible TowerとOSS版であるAWXのAPIユーザー認証方法が違ったのでまとめておきます。

環境

項目 バージョン
Ansible Tower 3.2.3
AWX 2018/3/21 時点での最新版

ユーザー認証について

対象 ユーザー認証
Ansible Towre JSONでユーザー情報を送信してTokenを発行し、そのTokenをヘッダに付与して処理を続ける
AWX Basic認証

Ansible Tower ユーザー認証

#!/usr/bin/env python3
import json
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

if __name__ == '__main__':
    # Ansible Towerログイン情報
    host = "Ansible Tower Host IP or FQDN"
    username = "admin"
    password = "redhat"

    # Token取得
    headers = {"Content-Type": "application/json"}
    url = "https://%s/api/v2/authtoken/" % host
    r = requests.post(url,
                      headers=headers,
                      data=json.dumps({"username": username, "password": password}),
                      verify=False)
    token = json.loads(r.text)['token']

    # ヘッダーにTokenをセット
    headers["Authorization"] = "Token " + token

    # Ansible Tower情報取得
    url = "https://%s/api" % host
    r = requests.get(url,
                     headers=headers,
                     verify=False)
    print(json.dumps(json.loads(r.text), indent=2))

結果

{
  "available_versions": {
    "v1": "/api/v1/",
    "v2": "/api/v2/"
  },
  "description": "AWX REST API",
  "current_version": "/api/v2/"
}

Ansible Towerの設定で HTTP BASIC認証の有効化オン になっていればBasic認証でも可能です。

スクリーンショット 2018-07-14 20.26.46.png

AWX ユーザー認証

#!/usr/bin/env python3
import json
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

if __name__ == '__main__':
    # AWXログイン情報
    host = "AWX Host IP or FQDN"
    username = "admin"
    password = "password"

    # AWX情報取得
    headers = {"Content-Type": "application/json"}
    url = "http://%s/api" % host
    r = requests.get(url,
                     headers=headers,
                     auth=(username, password),
                     verify=False)
    print(json.dumps(json.loads(r.text), indent=1))

結果

{
 "oauth2": "/api/o/",
 "description": "AWX REST API",
 "available_versions": {
  "v1": "/api/v1/",
  "v2": "/api/v2/"
 },
 "custom_login_info": "",
 "current_version": "/api/v2/",
 "custom_logo": ""
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
1
Help us understand the problem. What are the problem?