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/"
}

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": ""
}
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account log in.