Automation Hub接続に使用するオフライントークン期限切れ防止のPlaybook作成

Automation Hub接続に使用するオフライントークンは30日間アクティブでない状態が続くと期限切れとなってしまう。
公式ではcurlコマンドを実行すると記載があるがどうせならPlaybookにしてみる

概要

  • 公式で提供されるcurlコマンドをもとにuriモジュールを使用したPlaybookサンプルを作成

環境

  • ansible
    • 2.14.2

オフライントークン期限切れ防止のPlaybook作成

Red Hat公式サイトで提供されるcurlコマンド

  • Private Automation HubからAutomation Hubにアクセスするためのトークン発行画面にて期限切れ防止のためのcurlコマンドが紹介されている

  • curlコマンド

  curl https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token -d grant_type=refresh_token -d client_id="cloud-services" -d refresh_token="{{ user_token }}" --fail --silent --show-error --output /dev/null
  • 実行コマンド
    • レスポンスは欲しいので後半のオプションを省く
$ curl https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token -d grant_type=refresh_token -d client_id="cloud-services" -d refresh_token="{{ user_token }}" | python -m json.tool
  • 実行ログ
    • オフライントークンと別のトークンが出力されていたが公開していいのか分からないので伏せてますmm
{
    "access_token": "よくわからないトークン",
    "expires_in": 900,
    "refresh_expires_in": 0,
    "refresh_token": "よくわからないトークン",
    "token_type": "Bearer",
    "id_token": "よくわからないトークン",
    "not-before-policy": 0,
    "session_state": "beb141b6-1b55-4586-9ac7-a0090a201359",
    "scope": "openid api.iam.service_accounts api.iam.organization offline_access"
}

オフライントークン期限切れ防止のサンプルPlaybook作成

  • Playbook
  - hosts: localhost
    gather_facts: false
    connection: local
    vars:
      ah_offline_token: <オフライントークン書いてね>
    tasks:
      - name: "Automation Hub Offline Token Expiration Prevention API"
        ansible.builtin.uri:
          url: "https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token"
          method: POST
          body_format: "form-urlencoded"
          body:
            grant_type: "refresh_token"
            client_id: "cloud-services"
            refresh_token: "{{ ah_offline_token }}"
        register: response

      - name: "Debug response"
        ansible.builtin.debug:
          var: response
  • 実行ログ
  PLAY [localhost] ***********************************************************************************************************************************************************************************************************

  TASK [Automation Hub Offline Token Expiration Prevention API] **************************************************************************************************************************************************************
  ok: [localhost]

  TASK [Debug response] ******************************************************************************************************************************************************************************************************
  ok: [localhost] => {
      "response": {
          "cache_control": "no-store",
          "changed": false,
          "connection": "close",
          "content_length": "5660",
          "content_type": "application/json",
          "cookies": {
              "2a4bcc483fa585dc0ad94b51550d0923": "29857b1cb4903eae4c4774926bc13f28"
          },
          "cookies_string": "2a4bcc483fa585dc0ad94b51550d0923=29857b1cb4903eae4c4774926bc13f28",
          "date": "Thu, 06 Apr 2023 12:21:19 GMT",
          "elapsed": 0,
          "failed": false,
          "json": {
              "access_token": "よくわからないトークン",
              "expires_in": 900,
              "id_token": "よくわからないトークン",
              "not-before-policy": 0,
              "refresh_expires_in": 0,
              "refresh_token": "よくわからないトークン",
              "scope": "openid api.iam.service_accounts api.iam.organization offline_access",
              "session_state": "beb141b6-1b55-4586-9ac7-a0090a201359",
              "token_type": "Bearer"
          },
          "keep_alive": "timeout=300",
          "msg": "OK (5660 bytes)",
          "pragma": "no-cache",
          "redirected": false,
          "referrer_policy": "strict-origin",
          "set_cookie": "2a4bcc483fa585dc0ad94b51550d0923=29857b1cb4903eae4c4774926bc13f28; path=/; HttpOnly; Secure; SameSite=None",
          "status": 200,
          "strict_transport_security": "max-age=31536000; includeSubDomains",
          "url": "https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token",
          "x_content_type_options": "nosniff",
          "x_frame_options": "SAMEORIGIN",
          "x_rh_edge_cache_status": "Miss from child, Miss from parent",
          "x_rh_edge_reference_id": "0.2f9e3a17.1680783679.40bfc1bd",
          "x_rh_edge_request_id": "40bfc1bd",
          "x_site": "prod-spoke-aws-us-east-1",
          "x_xss_protection": "1; mode=block"
      }
  }
  • レスポンスがおおむね同じなので成功っぽい(ステータスも200)