AWXをDockerコンテナで起動

## はじめに
CICD環境を自力で構築しようと思い、前回はGitLabをDockerで起動してみました。
前回の記事は[こちら](https://qiita.com/Yoshihito-Fukushima/items/6ed77482e1cd0b86396b)
今回は同様にAWXをDockerで起動してみようと思います。

## 事前作業
AWXを構築する際に、Github/awxリポジトリをクローンし、Ansibleを使用して構築を行います。
そのため事前にAnsibleをインストールする必要があります。
下記コマンドでAnsibleをインストールします。

```
$ sudo apt update
$ sudo apt install software-properties-common
$ sudo apt-add-repository --yes --update ppa:ansible/ansible
$ sudo apt install ansible
```
続いて必要なライブラリを下記コマンドでインストールします。

```
$ sudo pip3 install docker
$ sudo pip3 install docker-compose
```

## AWX起動
Github/awxリポジトリをローカルにクローンします。

```
$ git clone https://github.com/ansible/awx -b 17.0.1
Cloning into 'awx'...
remote: Enumerating objects: 291225, done.
remote: Counting objects: 100% (120/120), done.
remote: Compressing objects: 100% (90/90), done.
remote: Total 291225 (delta 52), reused 51 (delta 23), pack-reused 291105
Receiving objects: 100% (291225/291225), 247.48 MiB | 5.20 MiB/s, done.
Resolving deltas: 100% (224644/224644), done.
Note: switching to 'aa4ca300f51781028517397b259319293cd664f8'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false
```
各種設定パラメータがawx/installer/inventoryに記載されているので必要に応じて編集を行います。

今回は下記のパラメータのコメントアウトを削除しました。

```
# admin_password=password
#project_data_dir=/var/lib/awx/projects
```

クローンしたPlaybookを実行します。

```
$ sudo ansible-playbook -i inventory install.yml

PLAY [Build and deploy AWX] **************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************
ok: [localhost]

TASK [check_vars : admin_password should be defined] *************************************************************************
ok: [localhost] => {
    "changed": false,
    "msg": "All assertions passed"
}

TASK [check_vars : include_tasks] ********************************************************************************************
skipping: [localhost]

TASK [check_vars : include_tasks] ********************************************************************************************
included: /home/fuku/awx/installer/roles/check_vars/tasks/check_docker.yml for localhost

TASK [check_vars : postgres_data_dir should be defined] **********************************************************************
ok: [localhost] => {
    "changed": false,
    "msg": "All assertions passed"
}

TASK [image_build : Set global version if not provided] **********************************************************************
skipping: [localhost]

TASK [image_build : Verify awx-logos directory exists for official install] **************************************************
skipping: [localhost]

TASK [image_build : Copy logos for inclusion in sdist] ***********************************************************************
skipping: [localhost]

TASK [image_build : Set awx image name] **************************************************************************************
skipping: [localhost]

TASK [image_build : Render Dockerfile] ***************************************************************************************
skipping: [localhost]

TASK [image_build : Build AWX image] *****************************************************************************************
skipping: [localhost]

TASK [image_build : Tag awx images as latest] ********************************************************************************
skipping: [localhost] => (item=awx)

TASK [image_push : Authenticate with Docker registry if registry password given] *********************************************
skipping: [localhost]

TASK [image_push : Remove awx image] *****************************************************************************************
skipping: [localhost]

TASK [image_push : Tag and push awx image to registry] ***********************************************************************
skipping: [localhost]

TASK [image_push : Set full image path for Registry] *************************************************************************
skipping: [localhost]

TASK [kubernetes : Generate broadcast websocket secret] **********************************************************************
skipping: [localhost]

TASK [kubernetes : fail] *****************************************************************************************************
skipping: [localhost]

TASK [kubernetes : include_tasks] ********************************************************************************************
skipping: [localhost] => (item=openshift_auth.yml)
skipping: [localhost] => (item=openshift.yml)

TASK [kubernetes : include_tasks] ********************************************************************************************
skipping: [localhost] => (item=kubernetes_auth.yml)
skipping: [localhost] => (item=kubernetes.yml)

TASK [kubernetes : Use kubectl or oc] ****************************************************************************************
skipping: [localhost]

TASK [kubernetes : set_fact] *************************************************************************************************
skipping: [localhost]

TASK [kubernetes : Record deployment size] ***********************************************************************************
skipping: [localhost]

TASK [kubernetes : Set expected post-deployment Replicas value] **************************************************************
skipping: [localhost]

TASK [kubernetes : Delete existing Deployment (or StatefulSet)] **************************************************************
skipping: [localhost]

TASK [kubernetes : Get Postgres Service Detail] ******************************************************************************
skipping: [localhost]

TASK [kubernetes : Template PostgreSQL Deployment (OpenShift)] ***************************************************************
skipping: [localhost]

TASK [kubernetes : Deploy and Activate Postgres (OpenShift)] *****************************************************************
skipping: [localhost]

TASK [kubernetes : Create Temporary Values File (Kubernetes)] ****************************************************************
skipping: [localhost]

TASK [kubernetes : Populate Temporary Values File (Kubernetes)] **************************************************************
skipping: [localhost]

TASK [kubernetes : Deploy and Activate Postgres (Kubernetes)] ****************************************************************
skipping: [localhost]

TASK [kubernetes : Remove tempfile] ******************************************************************************************
skipping: [localhost]

TASK [kubernetes : Set postgresql hostname to helm package service (Kubernetes)] *********************************************
skipping: [localhost]

TASK [kubernetes : Wait for Postgres to activate] ****************************************************************************
skipping: [localhost]

TASK [kubernetes : Check if Postgres 10 is being used] ***********************************************************************
skipping: [localhost]

TASK [kubernetes : Set new pg image] *****************************************************************************************
skipping: [localhost]

TASK [kubernetes : Wait for change to take affect] ***************************************************************************
skipping: [localhost]

TASK [kubernetes : Set env var for pg upgrade] *******************************************************************************
skipping: [localhost]

TASK [kubernetes : Wait for change to take affect] ***************************************************************************
skipping: [localhost]

TASK [kubernetes : Set env var for new pg version] ***************************************************************************
skipping: [localhost]

TASK [kubernetes : Wait for Postgres to redeploy] ****************************************************************************
skipping: [localhost]

TASK [kubernetes : Wait for Postgres to finish upgrading] ********************************************************************
skipping: [localhost]

TASK [kubernetes : Unset upgrade env var] ************************************************************************************
skipping: [localhost]

TASK [kubernetes : Wait for Postgres to redeploy] ****************************************************************************
skipping: [localhost]

TASK [kubernetes : Set awx image name] ***************************************************************************************
skipping: [localhost]

TASK [kubernetes : Determine Deployment api version] *************************************************************************
skipping: [localhost]

TASK [kubernetes : Get Root CA file contents] ********************************************************************************
skipping: [localhost]

TASK [kubernetes : Render Root CA template] **********************************************************************************
skipping: [localhost]

TASK [kubernetes : Apply Root CA template] ***********************************************************************************
skipping: [localhost]

TASK [kubernetes : Set Root CA file name] ************************************************************************************
skipping: [localhost]

TASK [kubernetes : Set Root CA file location] ********************************************************************************
skipping: [localhost]

TASK [kubernetes : Render deployment templates] ******************************************************************************
skipping: [localhost] => (item=None)
skipping: [localhost] => (item=None)
skipping: [localhost] => (item=None)
skipping: [localhost] => (item=None)
skipping: [localhost]

TASK [kubernetes : Apply Deployment] *****************************************************************************************
skipping: [localhost]

TASK [kubernetes : Delete any existing management pod] ***********************************************************************
skipping: [localhost]

TASK [kubernetes : Template management pod] **********************************************************************************
skipping: [localhost]

TASK [kubernetes : Create management pod] ************************************************************************************
skipping: [localhost]

TASK [kubernetes : Wait for management pod to start] *************************************************************************
skipping: [localhost]

TASK [kubernetes : Migrate database] *****************************************************************************************
skipping: [localhost]

TASK [kubernetes : Check for Tower Super users] ******************************************************************************
skipping: [localhost]

TASK [kubernetes : create django super user if it does not exist] ************************************************************
skipping: [localhost]

TASK [kubernetes : update django super user password] ************************************************************************
skipping: [localhost]

TASK [kubernetes : Create the default organization if it is needed.] *********************************************************
skipping: [localhost]

TASK [kubernetes : Delete management pod] ************************************************************************************
skipping: [localhost]

TASK [kubernetes : Scale up deployment] **************************************************************************************
skipping: [localhost]

TASK [local_docker : Generate broadcast websocket secret] ********************************************************************
ok: [localhost]

TASK [local_docker : Create ~/.awx/pgdocker directory] ***********************************************************************
ok: [localhost]

TASK [local_docker : Get full path of postgres data dir] *********************************************************************
changed: [localhost]

TASK [local_docker : Register temporary docker container] ********************************************************************
ok: [localhost]

TASK [local_docker : Check for existing Postgres data (run from inside the container for access to file)] ********************
fatal: [localhost]: FAILED! => {"changed": true, "cmd": "docker run --rm -v '/root/.awx/pgdocker:/var/lib/postgresql' centos:8 bash -c  \"[[ -f /var/lib/postgresql/10/data/PG_VERSION ]] && echo 'exists'\"\n", "delta": "0:00:00.932061", "end": "2021-09-30 00:36:14.111352", "msg": "non-zero return code", "rc": 1, "start": "2021-09-30 00:36:13.179291", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
...ignoring

TASK [local_docker : Record Postgres version] ********************************************************************************
skipping: [localhost]

TASK [local_docker : Determine whether to upgrade postgres] ******************************************************************
skipping: [localhost]

TASK [local_docker : Set up new postgres paths pre-upgrade] ******************************************************************
skipping: [localhost]

TASK [local_docker : Stop AWX before upgrading postgres] *********************************************************************
skipping: [localhost]

TASK [local_docker : Upgrade Postgres] ***************************************************************************************
skipping: [localhost]

TASK [local_docker : Copy old pg_hba.conf] ***********************************************************************************
skipping: [localhost]

TASK [local_docker : Remove old data directory] ******************************************************************************
skipping: [localhost]

TASK [local_docker : Export Docker awx image if it isnt local and there isnt a registry defined] *****************************
skipping: [localhost]

TASK [local_docker : Set docker base path] ***********************************************************************************
skipping: [localhost]

TASK [local_docker : Ensure directory exists] ********************************************************************************
skipping: [localhost]

TASK [local_docker : Copy awx image to docker execution] *********************************************************************
skipping: [localhost]

TASK [local_docker : Load awx image] *****************************************************************************************
skipping: [localhost]

TASK [local_docker : Set full image path for local install] ******************************************************************
skipping: [localhost]

TASK [local_docker : Set DockerHub Image Paths] ******************************************************************************
ok: [localhost]

TASK [local_docker : Create ~/.awx/awxcompose directory] *********************************************************************
ok: [localhost]

TASK [local_docker : Create Redis socket directory] **************************************************************************
ok: [localhost]

TASK [local_docker : Create Docker Compose Configuration] ********************************************************************
ok: [localhost] => (item={'file': 'environment.sh', 'mode': '0600'})
changed: [localhost] => (item={'file': 'credentials.py', 'mode': '0600'})
ok: [localhost] => (item={'file': 'docker-compose.yml', 'mode': '0600'})
ok: [localhost] => (item={'file': 'nginx.conf', 'mode': '0600'})
ok: [localhost] => (item={'file': 'redis.conf', 'mode': '0664'})

TASK [local_docker : Render SECRET_KEY file] *********************************************************************************
ok: [localhost]

TASK [local_docker : Remove AWX containers before migrating postgres so that the old postgres container does not get used] ***
changed: [localhost]

TASK [local_docker : Run migrations in task container] ***********************************************************************
changed: [localhost]

TASK [local_docker : Start the containers] ***********************************************************************************
changed: [localhost]

TASK [local_docker : Update CA trust in awx_web container] *******************************************************************
changed: [localhost]

TASK [local_docker : Update CA trust in awx_task container] ******************************************************************
changed: [localhost]

TASK [local_docker : Wait for launch script to create user] ******************************************************************
ok: [localhost -> localhost]

TASK [local_docker : Create Preload data] ************************************************************************************
changed: [localhost]

PLAY RECAP *******************************************************************************************************************
localhost                  : ok=21   changed=9    unreachable=0    failed=0    skipped=73   rescued=0    ignored=1
```

Dockerのプロセスを確認します。

```
$ sudo docker ps
CONTAINER ID   IMAGE                COMMAND                  CREATED         STATUS         PORTS                                   NAMES
01e837a975e2   ansible/awx:17.0.1   "/usr/bin/tini -- /u…"   3 minutes ago   Up 3 minutes   8052/tcp                                awx_task
fe969c40a978   ansible/awx:17.0.1   "/usr/bin/tini -- /b…"   5 minutes ago   Up 3 minutes   0.0.0.0:80->8052/tcp, :::80->8052/tcp   awx_web
963ecfe82b3a   postgres:12          "docker-entrypoint.s…"   5 minutes ago   Up 3 minutes   5432/tcp                                awx_postgres
277bb5ded009   redis                "docker-entrypoint.s…"   5 minutes ago   Up 3 minutes   6379/tcp                                awx_redis
```
無事起動ができました。
![image-20210930004249542.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1215301/fcbb731f-66d3-d395-a6e2-e68af4eabce6.png)
アクセスしてみるとサインイン画面が表示されるので試しにサインインしてみます。
![image-20210930004409283.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1215301/4024dfa6-abef-7e07-e53c-653c22edfe41.png)
無事サインインできました。

## おわりに
GitLabとAWXが無事起動できたので、次はGitLabにコミットしたPlaybookをAWXから実行してみようと思います。