Ansible実行環境のセットアップ

Ansibleを使ってNGINXの環境をセットアップします

1. Ansibleとは?

  • ../../_images/ansible_explanation.jpg

NGINXのセットアップはAnsible Galaxyというサイトを通じて、NGINXが提供するモジュールを利用します

NGINXは nginx_core というコレクションを提供しています。詳細は以下URLを参照してください

2. 環境セットアップ

Ansibleをインストールします。ラボ環境ではUbuntuを利用しておりますので手順に従ってインストールします。
(その他のLinuxディストリビューションをご利用の場合は、各ディストリビューションの項目を参照してください)
sudo apt update
sudo apt install software-properties-common -y
sudo apt-add-repository --yes --update ppa:ansible/ansible
sudo apt install ansible -y

Ansibleが正しくインストールされたことを確認します

ansible --version
実行結果サンプル
1ansible [core 2.12.8]
2  config file = /etc/ansible/ansible.cfg
3  configured module search path = ['/home/ubuntu/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
4  ansible python module location = /usr/lib/python3/dist-packages/ansible
5  ansible collection location = /home/ubuntu/.ansible/collections:/usr/share/ansible/collections
6  executable location = /usr/bin/ansible
7  python version = 3.8.10 (default, Jun 22 2022, 20:18:18) [GCC 9.4.0]
8  jinja version = 2.10.1
9  libyaml = True

Note

Ansibleでjinja templateを利用し、NGINXの設定を行う場合、エラーとなる場合があります。
エラーを回避するためには、Pythonのパッケージマネージャである pip をインストールし、 jinja2 のパッケージをアップデートする必要があります。
詳細は Jinja Templateが正しく動作しない場合 を参照してください。

AnsibleのNGINX Coreコレクションを取得します

ansible-galaxy collection install nginxinc.nginx_core
実行結果サンプル
1Starting galaxy collection install process
2Process install dependency map
3Starting collection install process
4Downloading https://galaxy.ansible.com/download/nginxinc-nginx_core-0.6.0.tar.gz to /home/ubuntu/.ansible/tmp/ansible-local-3312k9xqsukx/tmp5639w5je/nginxinc-nginx_core-0.6.0-ctsyccb0
5Installing 'nginxinc.nginx_core:0.6.0' to '/home/ubuntu/.ansible/collections/ansible_collections/nginxinc/nginx_core'
6nginxinc.nginx_core:0.6.0 was installed successfully

コマンドを実行することにより以下のロールがインストールされます。

Name

Description

Version

nginxinc.nginx

Install NGINX

0.23.1

nginxinc.nginx_config

Configure NGINX

0.5.1

nginxinc.nginx_app_protect

Install and configure NGINX App Protect

0.8.0

以下のパスにファイルが取得されていることを確認します

ls ~/.ansible/collections/ansible_collections/nginxinc/nginx_core/roles/*
実行結果サンプル
 1.ansible/collections/ansible_collections/nginxinc/nginx_core/roles/nginx:
 2CHANGELOG.md        LICENSE    files     molecule   vars
 3CODE_OF_CONDUCT.md  README.md  handlers  tasks
 4CONTRIBUTING.md     defaults   meta      templates
 5
 6.ansible/collections/ansible_collections/nginxinc/nginx_core/roles/nginx_app_protect:
 7CHANGELOG.md        LICENSE    files     meta      templates
 8CODE_OF_CONDUCT.md  README.md  handlers  molecule  vars
 9CONTRIBUTING.md     defaults   images    tasks
10
11.ansible/collections/ansible_collections/nginxinc/nginx_core/roles/nginx_config:
12CHANGELOG.md        LICENSE    files     molecule   vars
13CODE_OF_CONDUCT.md  README.md  handlers  tasks
14CONTRIBUTING.md     defaults   meta      templates

Tips1. Ansible Galaxy 各種コマンド

コレクションの一覧表示

ansible-galaxy collection list nginxinc.nginx_core
実行結果サンプル
1# /home/ubuntu/.ansible/collections/ansible_collections
2Collection          Version
3------------------- -------
4nginxinc.nginx_core 0.8.0

Authorを指定したロールの検索

ansible-galaxy search --author nginx
実行結果サンプル
 1Found 20 roles matching your search:
 2
 3 Name                                            Description
 4 ----                                            -----------
 5 nginxinc.nginx                                  Official Ansible role for NGINX
 6 nginxinc.nginx_app_protect                      Official Ansible role for NGINX App Protect WAF and DoS
 7 nginxinc.nginx_config                           Official Ansible role for configuring NGINX
 8 nginxinc.nginx_controller_agent                 A role to install, configure, and upgrade the NGINX Controller agen>
 9 nginxinc.nginx_controller_api_definition_import A role to import Open API definitions to NGINX Controller
10 nginxinc.nginx_controller_application           A role to define applications (apps) with NGINX Controller.
11 nginxinc.nginx_controller_certificate           A role to upsert (create and update) certificates to NGINX Controll>
12 nginxinc.nginx_controller_component             A role to define application components with NGINX Controller.
13 nginxinc.nginx_controller_environment           A role to define environments within NGINX Controller.
14 nginxinc.nginx_controller_forwarder             A role to define / update data forwarders within NGINX Controller.
15 nginxinc.nginx_controller_gateway               A role to upsert (create and update) gateways in NGINX Controller t>
16 nginxinc.nginx_controller_generate_token        A role to generate an NGINX Controller authentication token.
17 nginxinc.nginx_controller_install               Official Ansible role for installing NGINX Controller
18 nginxinc.nginx_controller_integration           A role to define / update integrations within NGINX Controller.
19 nginxinc.nginx_controller_license               A role to push an NGINX Controller license to your NGINX Controller>
20 nginxinc.nginx_controller_location              A role to define locations within NGINX Controller.
21 nginxinc.nginx_controller_publish_api           A role to upsert (create and update) the configurations to publish >
22 nginxinc.nginx_controller_user                  A role to define users within NGINX Controller.
23 nginxinc.nginx_controller_user_role             A role to define user roles within NGINX Controller.
24 nginxinc.nginx_unit                             Official Ansible role for NGINX Unit

Tips2. Inventory情報確認コマンド

NGINXは Ansible Playbookの設定サンプルをGitHubで公開しています。ファイルを取得します。

## cd ~/
git clone https://github.com/BeF5/f5j-nginx-ansible-lab.git
cd ~/f5j-nginx-ansible-lab

Graph形式でのInventory情報の表示

ansible-inventory -i inventories/hosts --graph
実行結果サンプル
1@all:
2  |--@nginx1:
3  |  |--10.1.1.7
4  |--@nginx2:
5  |  |--10.1.1.6
6  |--@ungrouped:

List形式でのInventory情報の表示

ansible-inventory -i inventories/hosts --list
実行結果サンプル
 1{
 2    "_meta": {
 3        "hostvars": {}
 4    },
 5    "all": {
 6        "children": [
 7            "nginx1",
 8            "nginx2",
 9            "ungrouped"
10        ]
11    },
12    "nginx1": {
13        "hosts": [
14            "10.1.1.7"
15        ]
16    },
17    "nginx2": {
18        "hosts": [
19            "10.1.1.6"
20        ]
21    }
22}