構成

ansible-runner実行環境

pythonのインストール

  • asdfとは パッケージ管理ツールです。 今まで開発言語はanyenvを使ってきましたが、同僚のすすめでasdfを試したところ、開発言語以外のアプリケーションも管理できることを知り、 一元管理ができて便利だと思い、現在も使っています。

  • pythonのbuild実行のためにパッケージをインストール

私のPCはUbuntuなので、以下のパッケージをインストールします。

sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev
  • asdfでpythonのインストール

まず、asdfで管理できる対象か確認します。

asdf plugin list all | grep python

管理対象にあるので、pluginを追加します。

asdf plugin-add python

pythonの最新バージョンをインストールします。

asdf install python latest

使用するバージョンを指定して完了です。 globalにしておくことで特に私は問題ないので、globalにしています。 systemのpythonに影響を及ぼしたくない等あれば、開発したいディレクトリに移動しlocalを指定すると良いと思います。

asdf global python 3.10.1

ansible & ansible-runnerのインストール

pipでansibleとansible-runnerをインストールします。

pip install ansible ansible-runner

ansible-runnerリモート実行環境

ansible-runnerのリモート実行の動作を確認するために、以下の環境を準備します。

  1. 素のOSのサーバ環境
  2. AWXの実行環境

それぞれの詳細は以下に記載します。

virtual machine

virtual machineを作成するのに、viratulboxとvagrantを使用します。 ここでは詳細は省略します。 これらがインストールされていることを前提として進めていきます。

vagrantでubuntu serverを準備する

  1. virtual machineを管理するフォルダを作成して、そこに"Vagrant"ファイルを作成。
mkdir vm && cd vm 
vim Vagrant

Vagrantファイルの中身

Vagrant.configure(2) do |config|
  config.vm.box = ubuntu/focal64
  config.vm.network public_network, ip: 192.168.1.100
end
  1. virtual machineの起動
vagrant up
  1. 接続するためにssh_configに情報を追加
  • ssh-configオプションで出力結果を確認
$ vagrant ssh-config 
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /home/t451/ghq/github.com/t-451/vagrant/project/ansible-runner-demo/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL
  • 自身のsshのconfigに設定を追加
$ vagrant ssh-config >> ~/.ssh/config 
  • 192.168.1.100で呼び出せるように、defaultを変更
$ sed -e 's/default/192.168.1.100' ~/.ssh/config
  1. ansibleで動作確認
  • 今回ansible-runnerのdemo環境を使用するのでリポジトリをcloneします。
ghq get https://github.com/ansible/ansible-runner.git
  • cloneしたディレクトリに移動して、inventoryに"192.168.1.100"を追加します。
cd ansible-runner
echo 192.168.1.100 >> demo/inventory/hosts
  • ansibleを実行します。
$ ansible 192.168.1.100 -i demo/inventory -m command -a "cat /etc/os-release"
192.168.1.100 | CHANGED | rc=0 >>
NAME="Ubuntu"
VERSION="20.04.3 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.3 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

vmの情報が取得できればOKです。

container

AWXの環境構築を手早く準備するのに良かったのと、 docker環境でのansible-runnerの動作も確認したかったので、dockerの環境を準備します。

docker環境の構築

私のPCはUbuntuで、環境分けても良いのですが、 そのまま使えたほうが便利なので、PC自身にDockerをインストールしていきます。

  1. build関連のパッケージインストール
sudo apt install apt-transport-https ca-certificates curl software-properties-common
  1. Dockerのリポジトリを追加
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
sudo apt update
  1. Dockerをインストール
sudo apt install docker-ce

念の為確認

sudo systemctl status docker
sudo docker run hello-world

以下のメッセージが出ればOKです。

Hello from Docker!
This message shows that your installation appears to be working correctly.
  1. sudo なしでdockerコマンド実行できるようにしておく
sudo usermod -aG docker ${USER}

コマンド実行後はログアウトして再度ログインする必要がある 適用できているかは以下のコマンドで確認

id -nG
user1 adm cdrom sudo dip plugdev lpadmin lxd sambashare docker

AWXサーバの構築

こちらの記事を参考に、本家のドキュメントも確認しつつ進めていきます。

  • docker-composeのインストール
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod 0755 /usr/local/bin/docker-compose
  • 確認
docker-compose -v

以下のようにバージョンが表示されればOKです。

docker-compose version 1.29.2, build 5becea4c
  • pythonのmoduleをインストール

追加するのが、docker, docker-composeと私は普段使用しないものなので、 systemのpythonから分離するために、poetryを使用します。

curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
  • AWX用の環境を作成します。
poetry new awx
cd awx
poetry init
poetry install
poetry add docker
poetry add docker-compose
  • pythonの環境をawx用の環境に切り替える
poetry shell

pip listを実行してみて、docker,docker-composeがインストールされていることを確認

pip list
Package            Version
------------------ ---------
attrs              21.2.0
awx                0.1.0
bcrypt             3.2.0
certifi            2021.10.8
cffi               1.15.0
charset-normalizer 2.0.9
cryptography       36.0.1
distro             1.6.0
docker             5.0.3
docker-compose     1.29.2
dockerpty          0.4.1
docopt             0.6.2
idna               3.3
jsonschema         3.2.0
more-itertools     8.12.0
packaging          21.3
paramiko           2.8.1
pip                21.3.1
pluggy             0.13.1
py                 1.11.0
pycparser          2.21
PyNaCl             1.4.0
pyparsing          3.0.6
pyrsistent         0.18.0
pytest             5.4.3
python-dotenv      0.19.2
PyYAML             5.4.1
requests           2.26.0
setuptools         58.3.0
six                1.16.0
texttable          1.6.4
urllib3            1.26.7
wcwidth            0.2.5
websocket-client   0.59.0
wheel              0.37.0

awxのリポジトリをcloneします。 poetryが作成したawxという名前と競合するので、以下のように名前を変えてcloneしました。 また、最新は18.0ですが、参考サイトの状況等踏まえ17.0.1を使用します。

git clone -b https://github.com/ansible/awx github-awx

Inventoryファイルを変更する必要があるようなので、変更します。 変更内容は、#project_data_dir =/var/lib/awx/projectsの先頭のコメントと admin_passwordの先頭のコメントを外すだけです。

cd github-awx
grep ^#project installer/inventory
sed -i 's/^#project/project/g' installer/inventory
sed -i 's/^# admin_pass/admin_pass/g' installer/inventory

awxをansibleでインストール

cd installer
ansible-playbook -i inventory install.yml

長いので抜粋ですが、成功すると最終行に以下のようなログがでます。

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

docker psの結果は以下のとおりでした。

docker ps
CONTAINER ID   IMAGE                COMMAND                  CREATED              STATUS              PORTS                                   NAMES
cdff7b19fbb4   ansible/awx:17.0.1   "/usr/bin/tini -- /u…"   About a minute ago   Up About a minute   8052/tcp                                awx_task
201f16bc70c5   ansible/awx:17.0.1   "/usr/bin/tini -- /b…"   About a minute ago   Up About a minute   0.0.0.0:80->8052/tcp, :::80->8052/tcp   awx_web
51b22522512e   redis                "docker-entrypoint.s…"   About a minute ago   Up About a minute   6379/tcp                                awx_redis
a9a0ef7c7803   postgres:12          "docker-entrypoint.s…"   About a minute ago   Up About a minute   5432/tcp                                awx_postgres

http://localhostに接続すると以下の画面が出ます。 Inventoryに設定されている情報のとおり、admin/passwordでログインできます。

ログイン画面 ログイン後画面

ひとまず、これでテスト環境の構築は終了です。