Ansibleの導入
複数のVMを起動したが、各VMのOSやアプリの更新作業をどうするのか?これら一連の更新作業を自動化するツールがAnsibleです。
1台ずつログインしてコマンドを打つ という手間を、1つの操作にまとめられる便利なツールです。
SSH接続によるリモート操作なので、Ansibleをインストールするマシンは何でも構いません。
また、メンテナンス対象の各VMには特別なソフトを入れる必要がありません。SSH接続さえできれば操作可能です。
AnsibleをインストールしたマシンをControl Node、対象となる各VMをManaged Nodeと定義します。
インストール(Ubuntu24.04)
$ sudo apt update
$ sudo apt install software-properties-common
$ sudo add-apt-repository --yes --update ppa:ansible/ansible
$ sudo apt install ansible
動作試験フロー
$ mkdir ansible
$ cd ansible
inventory登録(対象VMを指定)ファイル
hosts.ini
[vms]
vm-01 ansible_host=vm-01 ansible_user=ansible
vm-02 ansible_host=vm-02 ansible_user=ansible
初回アクセスのため各VMの公開鍵を取得
ssh-keyscan -H vm-02-ubuntu24 vm-02-debian13 >> ~/.ssh/known_hosts
通信確認
$ ansible -i hosts.ini vms -m ping
またはSSHパスワードを入力する場合: -k
$ ansible -i hosts.ini vms -m ping -k
vm-01 SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3.13"
},
"changed": false,
"ping": "pong"
}
vm-02 SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3.12"
},
"changed": false,
"ping": "pong"
}
Playbookの作成:VM内での具体的な実行命令
Nginxのインストール
setup.yaml
- hosts: vms
become: true
tasks:
- name: Install nginx
apt:
name: nginx
state: present
update_cache: true
qemu-guest-agentのインストール
qemu-ga.yaml
- hosts: vms
become: true
tasks:
- name: Install qemu-guest-agent
apt:
name: qemu-guest-agent
state: present
update_cache: true
- name: Enable qemu-guest-agent service
systemd:
name: qemu-guest-agent
enabled: true
- name: Start qemu-guest-agent service
systemd:
name: qemu-guest-agent
state: started
Playbook適用
$ ansible-playbook -i hosts.ini setup.yaml
$ ansible-playbook -i hosts.ini qwmu-ga.yaml
InventoryとPlaybookの組合せで、複数のVMをグループ分けし、アプリをインストールすることも可能です。
コレクション
名前空間付きの命令セットなどの機能拡張プラグイン。Playbookではapt,systemdなどの一般的な命令(これらもモジュールというプラグイン)は名前空間ansible.builtinが省略されています。
コレクション一覧
Ansible.Builtin
コマンドで確認
$ ansible-galaxy collection list
コレクションリストから特定の名前空間のプラグインをリストアップ
$ ansible-doc -l | grep ansible.builtin
ansible.builtin.add_host Add a host (and alternativel...
ansible.builtin.apt ...
ansible.builtin.apt_key ...
ansible.builtin.apt_repository ...
ansible.builtin.assemble ...
ansible.builtin.assert ...
ansible.builtin.async_status ...
ansible.builtin.blockinfile Insert...
ansible.builtin.command ...
ansible.builtin.copy ...
ansible.builtin.cron ...
ansible.builtin.deb822_repository ...
ansible.builtin.debconf ...
ansible.builtin.debug ...
ansible.builtin.dnf ...
ansible.builtin.dnf5 ...
ansible.builtin.dpkg_selections ...
ansible.builtin.expect ...
ansible.builtin.fail ...
ansible.builtin.fetch ...
ansible.builtin.file ...
ansible.builtin.find ...
ansible.builtin.gather_facts ...
ansible.builtin.get_url ...
ansible.builtin.getent ...
ansible.builtin.git ...
ansible.builtin.group ...
ansible.builtin.group_by ...
ansible.builtin.hostname
.....
.....
.....
プラグイン(apt)の詳細
$ ansible-doc ansible.builtin.apt
> MODULE ansible.builtin.apt (/usr/lib/python3/dist-packages/ansible/modules/apt.py)
Manages apt packages (such as for Debian/Ubuntu).
OPTIONS (red indicates it is required):
allow_change_held_packages Allows changing the version of a package which is on the apt hold list.
default: 'no'
type: bool
allow_downgrade Corresponds to the `--allow-downgrades' option for apt.
This option enables the named package and version to replace an already installed higher version of that package.
Note that setting `allow_downgrade=true' can make this module behave in a non-idempotent way.
(The task could end up with a set of packages that does not match the complete list of specified packages to install).
`allow_downgrade' is only supported by `apt' and will be ignored if `aptitude' is detected or specified.
aliases: [allow-downgrade, allow_downgrades, allow-downgrades]
default: 'no'
type: bool
.....
.....
.....