LoginSignup
7
11

More than 1 year has passed since last update.

Ansible AWXとGitLabを組合わせて構成管理の仕組みを構築

Last updated at Posted at 2022-02-20

背景

下記の様な構成管理情報を管理したいがExcelでは管理したくない
自動的に各種情報を取得して、バージョン管理を実施
変更があった時のみ管理者に通知する仕組みを作りたい

  • オンプレのネットワーク機器のConfig情報(FWのルール情報なども)
  • オンプレのネットワーク機器のポートステータス情報
  • ZABBIXに登録されているホスト情報など
  • vCenterに登録されてるホスト情報など
  • その他導入済み運用ツール(Pleasanterなど)の変更履歴情報など

作成した環境

AWX1.png

GitLab上に取得される情報の例

  • 各種情報毎にプロジェクトを分けている

AWX4.png

  • プロジェクトの中でネットワーク機器であればホスト毎にConfigが取得される

AWX5.png

通知の例

AWX2.png

  • git pushでGitLabに取得情報がアップロードされる
  • 差分(変更)があった場合のみメール通知する設定
  • 追加箇所は緑、削除箇所は赤で表示される

構築概要

Ansible AWXのインストール

Dockerイメージが公開されている
Playbookの実行はtaskコンテナ内で実行されるため注意
例えば対象ホストの名前解決が必要な場合はホスト側ではなくコンテナ側で名前解決出来る必要がある

GitLabのインストール

手順に従ってインストールする。今回はAWXと同様のサーバ上に構築

PlayBookの作成

各種PlayBookを作成する。Ansibleのモジュールがあるものについてはそれを使用
vsphereの情報取得ならvmware_vm_info、vmware_guest_infoをCiscoスイッチのConfig取得ならios_commandを使用
ZABBIXのモジュールもあるが、取得したい情報が取得出来なかったためZABBIX APIで取得
(あまり推奨されていないが)Shellモジュールを使用してshellをキック。shellの中でZABBIX APIを呼ぶ構成とする

Ansible AWXコンソールから各種設定

一例として、下記の様な形で設定
プロジェクトでPlaybook保存先を指定するが、GitLabのリポジトリを指定
ジョブテンプレートがジョブの実行単位となりこれにプロジェクトやインベントリ(どのホストを対象にするか)などを紐づける

AWX3.png

構築手順

1.Ansible AWXのインストール

  • パッケージインストール
yum update -y
yum -y install epel-release
yum install -y ansible python3 docker-compose yum-utils libselinux-python3 git
  • Dockerインストール
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install -y docker-ce docker-ce-cli containerd.io
  • インストール確認
ansible --version
python3 -V
docker --version
git --version
  • Dockerデーモン設定

社内プロキシ使用環境のため、下記設定が必要であった

/usr/lib/systemd/system/docker.service
Environment="HTTP_PROXY=xx.xx.xx.xx:xxxx"
Environment="HTTPS_PROXY=xx.xx.xx.xx:xxxx"
systemctl daemon-reload
systemctl restart docker
docker info
  • AWXインストール
git clone https://github.com/ansible/awx.git -b 15.0.1 --depth 1
cd awx/installer/
inventory
http_proxy=xx.xx.xx.xx:xxxx
https_proxy=xx.xx.xx.xx:xxxx
project_data_dir=/var/lib/awx/projects
postgres_data_dir=/var/lib/awx/pgdocker
docker_compose_dir=/var/lib/awx/awxcompose
ansible-playbook -i inventory install.yml

エラーなく完了すればOK

  • AWX接続

http://xx.xx.xx.xx(admin/password)

  • docker-compose設定ファイル修正

環境にあわせてDNS、DNSサフィックス設定、プロキシ設定、ローカルディレクトリのマウント設定など

2.GitLabのインストール

  • GitLabインストール

同一のサーバ上でAWXが80ポートで稼働しているため8081ポートで設定

yum install -y policycoreutils-python curl openssh-server openssh-clients
systemctl enable postfix
systemctl start postfix
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
EXTERNAL_URL="http://xx.xx.xx.xx:8081" yum install -y gitlab-ce
  • GitLab設定
/etc/gitlab/gitlab.rb
external_url 'http://xx.xx.xx.xx:8081'
unicorn['listen'] = 'xx.xx.xx.xx'
unicorn['port'] = 8082
nginx['enable'] = true
nginx['listen_port'] = 8081
sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart
sudo gitlab-ctl status
  • 接続確認

http://xx.xx.xx.xx:8081

  • 秘密鍵作成

ローカルで秘密鍵を作成し公開鍵をGitLab側に登録する
秘密鍵はコンテナ側からもアクセス出来る様にしておく
コンテナ側からGitLabにアクセスする際の鍵認証で使用するため

cd ~/.ssh
ssh-keygen -t rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
  • 公開鍵登録

http://xx.xx.xx.xx:8081/profile/keysにアクセスし下記入力

titleに公開鍵名(id_rsa.pub)
Expire atに20xx/xx/xx
keyに公開鍵(id rsa.pubの内容)

3.PlayBookの作成

  • hostsにAWXで作成するグループ指定
  • 認証パラメータはAWX側で管理するため不要に
  • Inventory hostnameにホスト名が入る

4.PlayBookの作成例

  • Cisco Config取得のPlaybook作成例
- hosts: Cisco_All
  gather_facts: no
  connection: local
  tasks:
    - name: Config Backup
      ios_command:
        commands:
          - show run
        authorize: true
        auth_pass: **********
        timeout: 30
      register: result
    - name: save config to file
      copy:
        content: "{{ result.stdout[0]}}"
        dest: "/var/lib/awx/projects/xxxxxx/xxxxxx/{{ inventory_hostname }}.txt"

5.Ansible AWXコンソールから各種設定

  • コンソールログイン後、各種設定実施

AWX6.png

7
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
11