本記事について
本記事では、Red Hat から提供されている構成管理ツールである Ansible を CentOS Stream 9 にインストールする方法について説明します。
動作確認環境
本記事は以下の環境にて検証した結果に基づき作成されています。
- CentOS Stream release 9
- Python 3.12.6
- pip 24.2
- ansible-core 2.17.4
- Ansible community version 10.4.0
pyenv による Python のインストール
Ansible を使用するためには ansible-core のバージョンによってサポート範囲内のバージョンの Python が必要なため、必要に応じてまず Python をインストールします。
例えば現時点で最新バージョンの ansible-core 2.17 の場合は Python 3.10 – 3.12 が必要です。その他のバージョンについてはこちらの Ansible ドキュメントで確認してください。
ここでは pyenv を使用してPython をユーザのホームディレクトリ配下にインストールするため、Python をインストールしたいユーザでログインした状態で以下手順を実行します。本記事では root ユーザのホームディレクトリ配下に Python をインストールします。
まず以下コマンドで必要パッケージをインストールします。
dnf install -y gcc zlib-devel bzip2 bzip2-devel readline readline-devel sqlite sqlite-devel openssl openssl-devel git libffi-devel tar make xz-devel
- gcc
- zlib-devel
- bzip2
- bzip2-devel
- readline
- readline-devel
- sqlite
- sqlite-devel
- openssl
- openssl-devel
- git
- libffi-devel
- tar
- make
- xz-devel
この後 Python をインストールしますが、その際以下のような Warning が表示された場合、上記パッケージの中のxz-devel
が未インストールの可能性があります。
Installing Python-3.12.6...
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/root/.pyenv/versions/3.12.6/lib/python3.12/lzma.py", line 27, in <module>
from _lzma import *
ModuleNotFoundError: No module named '_lzma'
WARNING: The Python lzma extension was not compiled. Missing the lzma lib?
Installed Python-3.12.6 to /root/.pyenv/versions/3.12.6
以下のコマンドを実行して現在のユーザのホームディレクトリ配下に pyenv を github からダウンロードします。
※本記事では root ユーザで作業しているため /root 配下にダウンロードされます
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
以下の 2 つのコマンドを実行して ~/.bash_profile に pyenv のパスを追加します。
echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
※~/.bash_profile は当該ユーザにログインした際に読み込まれる設定ファイルです。
その後、以下コマンドで設定ファイルを読み込みます。
source ~/.bash_profile
pyenv --version
コマンドで pyenv のバージョンが表示されることを確認します。
# pyenv --version
pyenv 2.4.13
以上で pyenv のインストールは完了です。
pyenv install --list
コマンドを実行して pyenv でインストール可能な Python バージョンを確認します。
[root@CentOSST9 ~]# pyenv install --list
Available versions:
2.1.3
2.2.3
(略)
3.12.4
3.12.5
3.12.6
3.13.0rc2
3.13.0rc2t
(略)
本記事では 3.12.6 をインストールすることにします。
以下のコマンドを実行して Python をインストールします。
- pyenv install <バージョン>
# pyenv install 3.12.6
Downloading Python-3.12.6.tar.xz...
-> https://www.python.org/ftp/python/3.12.6/Python-3.12.6.tar.xz
Installing Python-3.12.6...
Installed Python-3.12.6 to /root/.pyenv/versions/3.12.6
Python インストール後に指定バージョンの Python をアンインストールしたくなった場合は以下コマンドでアンインストールできます。
- pyenv uninstall <バージョン>
pyenv uninstall 3.12.6
Python がインストールできたら以下2つのコマンドを順に実行して Global に使用する Python のバージョンを設定します。
- pyenv global <バージョン>
- pyenv rehash
pyenv global 3.12.6
pyenv rehash
その後python --version
コマンドを実行して現在の Python バージョンを確認します。
# python --version
Python 3.12.6
Ansible の2つのパッケージ
Ansible には以下の2つのパッケージが存在します。
- ansible-core
- Ansible.Builtinのセットを含む最小限の言語およびランタイムパッケージ
- ansible
- ansible-core に対していくつかの Ansible コレクションを追加したパッケージ
Ansible コレクションとは、複数のモジュール、プラグイン等を対象 OS 別等の分類でまとめたものです。個別の Ansible コレクションを追加的にインストールすることができます。
Ansible のインストール手順
ここでは Python の venv を使用して仮想環境に Ansible をインストールします。
ここでは /root 配下に venv という名前の仮想環境を作成します。venv の代わりに任意の名前を使用することもできます。
- python -m venv <仮想環境名>
cd /root
python -m venv venv
コマンド実行後、指定した仮想環境名のフォルダが作成されます。
# ls -l
合計 4
-rw-------. 1 root root 1119 6月 18 23:13 anaconda-ks.cfg
drwxr-xr-x 5 root root 74 9月 22 12:12 venv
仮想環境を作成できたら以下コマンドで仮想環境を活性化(アクティブ化)します。
- source <仮想環境名>/bin/activate
活性化後、以下のようにプロンプトの先頭に(仮想環境名)
が表示されます。
[root@CentOSST9 ~]# source venv/bin/activate
(venv) [root@CentOSST9 ~]#
活性化した仮想環境を非活性化したい場合はdeactivate
コマンドを実行します。
仮想環境内に Ansible をインストールします。パッケージとして ansible-core か ansible を選択することができますが、ここでは ansible をpip3
を使用してインストールします。
- pip3 install ansible
(venv) [root@CentOSST9 ~]# pip3 install ansible
Collecting ansible
(略)
Successfully installed MarkupSafe-2.1.5 PyYAML-6.0.2 ansible-10.4.0 ansible-core-2.17.4 cffi-1.17.1 cryptography-43.0.1 jinja2-3.1.4 packaging-24.1 pycparser-2.22 resolvelib-1.0.1
ansible-core のバージョンと ansible パッケージのバージョンがあり、それぞれ以下コマンドで確認できます。
- ansible –version
- ansible-core 関連パッケージのバージョンを表示
- ansible-community –version
- ansible パッケージバージョンを表示
(venv) [root@CentOSST9 ~]# ansible --version
ansible [core 2.17.4]
config file = None
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /root/venv/lib/python3.12/site-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /root/venv/bin/ansible
python version = 3.12.6 (main, Sep 22 2024, 11:16:27) [GCC 11.5.0 20240719 (Red Hat 11.5.0-2)] (/root/venv/bin/python)
jinja version = 3.1.4
libyaml = True
(venv) [root@CentOSST9 ~]# ansible-community --version
Ansible community version 10.4.0
上記から、ansible-core のバージョンは 2.17.4、ansible パッケージのバージョンは 10.4.0 であることが分かります。
また、pip list
でもそれぞれのバージョンを確認できます。
(venv) [root@CentOSST9 ~]# pip list
Package Version
------------ -------
ansible 10.4.0
ansible-core 2.17.4
(以下略)
Ansible を最新バージョンにアップグレードしたい場合は以下コマンドを実行します。
- pip3 install -U ansible
(venv) [root@CentOSST9 ~]# pip3 install -U ansible
Ansible コレクションの確認
インストールされている Ansible コレクションのリスト及び各コレクションのバージョンは以下のコマンドで確認できます。
- ansible-galaxy collection list
(venv) [root@CentOSST9 ~]# ansible-galaxy collection list
# /root/venv/lib/python3.12/site-packages/ansible_collections
Collection Version
---------------------------------------- -------
amazon.aws 8.2.1
ansible.netcommon 6.1.3
ansible.posix 1.5.4
ansible.utils 4.1.0
ansible.windows 2.5.0
arista.eos 9.0.0
awx.awx 24.6.1
azure.azcollection 2.7.0
check_point.mgmt 5.2.3
chocolatey.chocolatey 1.5.1
cisco.aci 2.10.1
cisco.asa 5.0.1
cisco.dnac 6.18.0
cisco.intersight 2.0.17
cisco.ios 8.0.0
cisco.iosxr 9.0.0
cisco.ise 2.9.3
cisco.meraki 2.18.1
cisco.mso 2.9.0
cisco.nxos 8.1.0
cisco.ucs 1.11.0
cloud.common 3.0.0
cloudscale_ch.cloud 2.4.0
community.aws 8.0.0
(以下略)
上記は ansible インストール直後での実行結果ですが、cisco 関連のコレクションはデフォルトでインストールされていることが分かります。
SSH 接続のための準備
Ansible からターゲット機器に SSH 接続するために必要な準備を行います。
必要パッケージのインストール
ターゲット機器が Linux サーバ等の場合はsshpass
が必要になるため以下コマンドでインストールします。
- dnf install -y sshpass
ターゲット機器が Cisco 等のネットワーク機器の場合は SSH クライアントライブラリのansible-pylibssh
またはparamiko
が必要になります。こちらはpip3
でインストールします。
- pip3 install ansible-pylibssh
- pip3 install paramiko
ansible-pylibssh
とparamiko
はどちらかが入っていればとりあえず動きました。ansible-pylibssh
の方がより新しいもので、Ansible のデフォルトではansible-pylibssh
を優先して使用するようです。paramiko
のみインストールしている場合、Playbook 実行時に以下のような Warning が表示さます。
[WARNING]: ansible-pylibssh not installed, falling back to paramiko
SSH フィンガープリントエラーの回避策
known_hosts に保存されていない機器に Ansible で SSH 接続すると以下のようなエラーになり Playbook 実行に失敗します。
[0;31mfatal: [CiscoRT]: FAILED! => {"changed": false, "msg": "\nlibssh: The authenticity of host '10.1.1.1' can't be established due to 'Host is unknown: df:d4:93:8c:5e:ff:fe:0f:f8:d9:69:55:fc:05:4b:13:5e:fc:db:ba'.\nThe ssh-rsa key fingerprint is SHA1:39STjF7//g/42WlV/AVLE17827o."}[0m
一度ターゲット機器に手動で SSH 接続して known_hosts に登録すればこのエラーは無くなりますが、そうする代わりに Ansible のコンフィグファイルを作成して設定することでフィンガープリントエラーを無視することができます。
デフォルトでは Ansible のコンフィグファイルは設定されていません。ansible --version
のconfig file =
の部分でコンフィグファイルを確認できますが、デフォルトではNone
になっています。
(venv) [root@CentOSST9 ~]# ansible --version
ansible [core 2.17.4]
config file = None
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /root/venv/lib/python3.12/site-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /root/venv/bin/ansible
python version = 3.12.6 (main, Sep 22 2024, 11:16:27) [GCC 11.5.0 20240719 (Red Hat 11.5.0-2)] (/root/venv/bin/python)
jinja version = 3.1.4
libyaml = True
以下のコマンドを実行することでカレントディレクトリにコンフィグファイルを作成すると同時に Ansible のコンフィグファイルとして設定することができます。
- ansible-config init –disabled > ansible.cfg
コマンド実行後、以下のようにconfig file = /root/ansible/ansible.cfg
に変わります。
(venv) [root@CentOSST9 ansible]# ansible-config init --disabled > ansible.cfg
(venv) [root@CentOSST9 ansible]# ansible --version
ansible [core 2.17.4]
config file = /root/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /root/venv/lib/python3.12/site-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /root/venv/bin/ansible
python version = 3.12.6 (main, Sep 22 2024, 11:16:27) [GCC 11.5.0 20240719 (Red Hat 11.5.0-2)] (/root/venv/bin/python)
jinja version = 3.1.4
libyaml = True
作成されたコンフィグファイルansible.cfg
の中身はデフォルトではすべてコメントアウトされています。この中で;host_key_checking=True
を探し、その下にhost_key_checking=False
を追記します。
# (boolean) Set this to "False" if you want to avoid host key checking by the underlying connection plugin Ansible us
es to connect to the host.
# Please read the documentation of the specific connection plugin used for details.
;host_key_checking=True
host_key_checking=False
これで SSH フィンガープリントのチェックがされなくなりエラーを回避できます。
アルゴリズムの不一致への対応【Cisco 機器等】
古い Cisco 機器の場合、アルゴリズムの不一致により SSH 接続ができませんでした。例えば CentOS Stream 9 と C891FJ-K9 では アルゴリズムの不一致により SSH 接続できませんでした。
これを回避するためには、Ansible マシン側で~/.ssh/config
を作成して以下のように設定します。
Host 10.1.1.1
KexAlgorithms +diffie-hellman-group14-sha1
HostKeyAlgorithms +ssh-rsa
PubkeyAcceptedAlgorithms +ssh-rsa
10.1.1.1の部分はターゲット機器のアドレスを指定します。「*」でワイルドカード指定も可能です。
必要な設定内容はターゲット機器によって異なる可能性があります。SSH 接続時のエラーログなどを見て確認してください。
Catalyst 2960-8TC-L の場合、更に Ciphers にaes256-ctr、aes128-gcm@openssh.com、aes256-gcm@openssh.comの何れかを追加する必要がありました。
Host 10.1.1.*
KexAlgorithms +diffie-hellman-group14-sha1
HostKeyAlgorithms +ssh-rsa
PubkeyAcceptedAlgorithms +ssh-rsa
Ciphers +aes256-ctr
Ciphers +aes128-gcm@openssh.com
Ciphers +aes256-gcm@openssh.com
Playbook 実行テスト
ここでは Cisco ルータの C891FJ-K9 をターゲット機器として、showコマンドを実行してその出力をターミナル上に表示してみます。
インベントリと Playbook を以下の内容で作成します。
[ios]
10.1.1.1 ansible_user=admin ansible_password=admin
[ios:vars]
ansible_connection=network_cli
ansible_network_os=cisco.ios.ios
- [ios]
- iosという名前のターゲット機器グループを定義し、メンバーとして10.1.1.1を設定
- ansible_user:SSH ログインユーザ名、ターゲット機器固有の変数として定義
- ansible_password:SSH ログインユーザパスワード、ターゲット機器固有の変数として定義
- iosという名前のターゲット機器グループを定義し、メンバーとして10.1.1.1を設定
- [ios:vars]
- iosグループ内の共通変数を定義
- ansible_connection
- Ansible は ansible-connection 設定を使用して、リモート デバイスへの接続方法を決定します
- とりあえずnetwork_cliを指定すれば良さそうです
- ansible_network_os
- このホストがどのネットワーク プラットフォームに対応しているかを Ansible に通知します
- ターゲット機器が Cisco IOS ルータのため cisco.ios.ios を指定
- name: Playbook Test
hosts: ios
gather_facts: no
tasks:
- name: command
cisco.ios.ios_command:
commands: "show clock"
register: command_log
- name: debug
ansible.builtin.debug:
msg: "{{ command_log.stdout_lines[0] }}"
- cisco.ios.ios_command
- IOS でコマンドを実行するモジュール
register
を指定して出力を保存する
- ansible.builtin.debug
- デバッグメッセージを表示するモジュール
msg
で出力する内容としてregister
で保存した内容を表示
以下コマンドで Playbook を実行します。
- ansible-playbook -i <インベントリ> <Playbook>
本記事で説明しているように Ansible を venv を使用して仮想環境にインストールした場合は、仮想環境を活性化した状態で Playbook を実行する必要があります。
以下は実行例です。
(venv) [root@CentOSST9 ansible]# ansible-playbook -i hosts test.yml
PLAY [Playbook Test] ************************************************************************************************
TASK [command] ******************************************************************************************************
ok: [10.1.1.1]
TASK [debug] ********************************************************************************************************
ok: [10.1.1.1] => {
"msg": [
"*14:29:52.107 JST Sun Sep 22 2024"
]
}
PLAY RECAP **********************************************************************************************************
10.1.1.1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
コマンドの出力内容が表示できており、一番下に ok=2 と表示されているので問題なく実行できています。
参考資料
Ansible 関連記事一覧
- Ansible インストール
- Cisco IOS 操作
- Cisco Nexus スイッチ操作
Amazon アフィリエイトリンク
以下は Amazon アフィリエイトリンクです。インフラエンジニアにそこそこおすすめなアイテムです。
note メンバーシップへの参加もお待ちしています!

【アフィリエイト】おすすめ WordPress テーマ【SWELL】
当サイトでは WordPress テーマとして SWELL を使用しています。以前は無料・高機能テーマとして知られる Cocoon を使用していて Cocoon も使いやすかったのですが、SWELL を使い始めてからは SWELL のほうが圧倒的に使いやすいなと思いました。そして何より読み込み速度が速い。SWELL を使い始めてから、過去の Cocoon のブログを見直したときに「あれ、こんなに表示遅かったっけ?」という感覚になりました。
また SWELL はデフォルトでもオシャレなデザインですが柔軟にカスタマイズすることもでき個性のあるサイトを作成できます。さらにブログパーツや広告タグといった再利用可能なブログの「部品」も作成することができ、ブログ作成効率も高いです。
技術ブログやアフィリエイト等での収益化を見据えたブログの作成に SWELL は最適です。初見では価格が高いなと思うと思いますが、私としては SWELL を採用して良かったしそれ以上の価値があると感じています。
ブログの新設やテーマ変更を考えている人は一度 SWELL を検討してみてください。
以下の画像リンクから詳細な情報を確認できます。
レンタルサーバーを探している人には安定性に定評のあるエックスサーバーをお勧めします。
当サイトもエックスサーバーを使用しています。WordPress のインストールも簡単にできます。
コメント