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

Note

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

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

ansible-galaxy collection install nginxinc.nginx_core
実行結果サンプル
1
2
3
4
5
6
Starting galaxy collection install process
Process install dependency map
Starting collection install process
Downloading 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
Installing 'nginxinc.nginx_core:0.6.0' to '/home/ubuntu/.ansible/collections/ansible_collections/nginxinc/nginx_core'
nginxinc.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
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
.ansible/collections/ansible_collections/nginxinc/nginx_core/roles/nginx:
CHANGELOG.md        LICENSE    files     molecule   vars
CODE_OF_CONDUCT.md  README.md  handlers  tasks
CONTRIBUTING.md     defaults   meta      templates

.ansible/collections/ansible_collections/nginxinc/nginx_core/roles/nginx_app_protect:
CHANGELOG.md        LICENSE    files     meta      templates
CODE_OF_CONDUCT.md  README.md  handlers  molecule  vars
CONTRIBUTING.md     defaults   images    tasks

.ansible/collections/ansible_collections/nginxinc/nginx_core/roles/nginx_config:
CHANGELOG.md        LICENSE    files     molecule   vars
CODE_OF_CONDUCT.md  README.md  handlers  tasks
CONTRIBUTING.md     defaults   meta      templates

Tips1. Ansible Galaxy 各種コマンド

コレクションの一覧表示

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

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

ansible-galaxy search --author nginx
実行結果サンプル
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Found 20 roles matching your search:

 Name                                            Description
 ----                                            -----------
 nginxinc.nginx                                  Official Ansible role for NGINX
 nginxinc.nginx_app_protect                      Official Ansible role for NGINX App Protect WAF and DoS
 nginxinc.nginx_config                           Official Ansible role for configuring NGINX
 nginxinc.nginx_controller_agent                 A role to install, configure, and upgrade the NGINX Controller agen>
 nginxinc.nginx_controller_api_definition_import A role to import Open API definitions to NGINX Controller
 nginxinc.nginx_controller_application           A role to define applications (apps) with NGINX Controller.
 nginxinc.nginx_controller_certificate           A role to upsert (create and update) certificates to NGINX Controll>
 nginxinc.nginx_controller_component             A role to define application components with NGINX Controller.
 nginxinc.nginx_controller_environment           A role to define environments within NGINX Controller.
 nginxinc.nginx_controller_forwarder             A role to define / update data forwarders within NGINX Controller.
 nginxinc.nginx_controller_gateway               A role to upsert (create and update) gateways in NGINX Controller t>
 nginxinc.nginx_controller_generate_token        A role to generate an NGINX Controller authentication token.
 nginxinc.nginx_controller_install               Official Ansible role for installing NGINX Controller
 nginxinc.nginx_controller_integration           A role to define / update integrations within NGINX Controller.
 nginxinc.nginx_controller_license               A role to push an NGINX Controller license to your NGINX Controller>
 nginxinc.nginx_controller_location              A role to define locations within NGINX Controller.
 nginxinc.nginx_controller_publish_api           A role to upsert (create and update) the configurations to publish >
 nginxinc.nginx_controller_user                  A role to define users within NGINX Controller.
 nginxinc.nginx_controller_user_role             A role to define user roles within NGINX Controller.
 nginxinc.nginx_unit                             Official Ansible role for NGINX Unit

キーワード、Authorを指定したロールの検索

ansible-galaxy search development --author nginx
実行結果サンプル
1
2
3
4
5
6
7
8
9
Found 5 roles matching your search:

 Name                                  Description
 ----                                  -----------
 nginxinc.nginx                        Official Ansible role for NGINX
 nginxinc.nginx_app_protect            Official Ansible role for NGINX App Protect WAF and DoS
 nginxinc.nginx_config                 Official Ansible role for configuring NGINX
 nginxinc.nginx_controller_environment A role to define environments within NGINX Controller.
 nginxinc.nginx_unit                   Official Ansible role for NGINX Unit

Tips2. Inventory情報確認コマンド

Graph形式でのInventory情報の表示

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

List形式でのInventory情報の表示

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