0. はじめに
Ansible2.10から従来のAnsible2.9とは異なり、モジュールの大半がansible-galaxyに集約され最低限のモジュールが利用できるansible-baseの提供が開始される。
ただし、Ansible2.10のインストール形式には以下の2種類がある。
①Ansible-base
Ansible-baseのみの提供。最低限のモジュールとpluginのみ包含されるため、必要なモジュールはansible-galaxyから取得する。
②Ansible
従来の配布形態と同様、Ansible-baseにモジュールやpluginがバインドされた形式。
Ansible-baseとAnsibleをインストールし、それぞれの動作の違いを確認する。
1. Ansible
1-1. バージョン確認
Ansible 2.10がインストールされていることを確認する。
# ansible --version
ansible 2.10.4
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.6.8 (default, Apr 16 2020, 01:36:27) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
Ansible-baseとは別パッケージ。
# pip3 freeze
ansible==2.10.4
ansible-base==2.10.4
・・・
1-2. playbook実行
以下playbookを実行する。
※管理サーバ(Ansible)のユーザ:tedと管理対象サーバのユーザ:rootとの間で鍵交換をするもの
---
- hosts: ap_servers
gather_facts: false
tasks:
- name: Set authorized key took from file
authorized_key:
user: root
state: present
key: "{{ lookup('file', '/home/ted/.ssh/id_rsa.pub') }}"
実行結果
実行結果は以下。
$ ansible-playbook -i inventory playbook.v2.yml
PLAY [ap_servers] **************************************************************
TASK [Set authorized key took from file] ***************************************
ok: [192.168.11.21]
PLAY RECAP *********************************************************************
192.168.11.21 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
2. Ansible-base
2-1. バージョン確認
Ansible-baseが導入されていることを確認する。
# ansible --version
ansible 2.10.3
config file = None
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/python390/lib/python3.9/site-packages/ansible
executable location = /usr/bin/ansible
python version = 3.9.0 (default, Nov 30 2020, 16:14:32) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
ansible-baseパッケージのみ
# pip3.9 freeze
ansible-base==2.10.3
・・・
2-2. playbook実行
以下playbookを実行する。
※管理サーバ(Ansible)のユーザ:tedと管理対象サーバのユーザ:rootとの間で鍵交換をするもの
---
- hosts: ap_servers
gather_facts: false
tasks:
- name: Set authorized key took from file
authorized_key:
user: root
state: present
key: "{{ lookup('file', '/home/ted/.ssh/id_rsa.pub') }}"
実行結果
実行結果は以下。モジュールが不足しており、実行不可。
$ ansible-playbook -i inventory playbook.v2.yml
ERROR! couldn't resolve module/action 'authorized_key'. This often indicates a misspelling, missing collection, or incorrect module path.
The error appears to be in '/home/ted/playbook.v2.yml': line 6, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
tasks:
- name: Set authorized key took from file
^ here
2-3. 不足したモジュールのインストール(オフライン)
Ansible-galaxyから不足したモジュールをインストールするための資源をインストール
# ansible-galaxy collection download ansible.posix
Process install dependency map
Starting collection download process to '/root/collections'
Downloading collection 'ansible.posix' to '/root/collections/ansible-posix-1.1.1.tar.gz'
Downloading https://galaxy.ansible.com/download/ansible-posix-1.1.1.tar.gz to /root/.ansible/tmp/ansible-local-2115uc8975m5/tmp0h82jv5x
ansible.posix (1.1.1) was downloaded successfully
Writing requirements.yml file of downloaded collections to '/root/collections/requirements.yml'
ディレクトリ確認し、インストールされていることを確認する。
# cd collections/
[root@CentOS8-2 collections]# ls -ltr
合計 140
-rw-------. 1 root root 138226 12月 13 20:51 ansible-posix-1.1.1.tar.gz
-rw-r--r--. 1 root root 65 12月 13 20:51 requirements.yml
[root@CentOS8-2 collections]# cat requirements.yml
collections:
- name: ansible-posix-1.1.1.tar.gz
version: 1.1.1
オフラインで不足していたモジュールをインストールするためには、以下のコマンドを実行する。
$ansible-galaxy collection install -r requirements.yml
Starting galaxy collection install process
Process install dependency map
Starting collection install process
Installing 'ansible.posix:1.1.1' to '/root/.ansible/collections/ansible_collections/ansible/posix'
ansible.posix (1.1.1) was installed successfully
不足していたモジュールは、以下ディレクトリに保存される。
~/.ansible/collections/ansible_collections/
確認結果は以下。
# ls -ltr ~/.ansible/collections/ansible_collections/ansible/posix/
.github/ README.md requirements.txt
.gitignore changelogs/ shippable.yml
CHANGELOG.rst docs/ test-requirements.txt
COPYING hacking/ tests/
FILES.json meta/
MANIFEST.json plugins/
2-4 playbook再実行
不足していたモジュールをインストールしたところで再びplaybookを実行。
今回はplaybookの実行に成功。
# ansible-playbook -i inventory playbook.v2.yml --check
PLAY [ap_servers] **************************************************************
TASK [Set authorized key took from file] ***************************************
changed: [192.168.11.19]
PLAY RECAP *********************************************************************
192.168.11.19 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0