RHEL 9(CentOS 9)におけるAnsibleのインストール手順

前提条件
OSバージョンを確認する

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# cat /etc/redhat-release
Red Hat Enterprise Linux release 9.2 (Plow)
# cat /etc/redhat-release Red Hat Enterprise Linux release 9.2 (Plow)
# cat /etc/redhat-release
Red Hat Enterprise Linux release 9.2 (Plow)

# uname -r
5.14.0-284.11.1.el9_2.x86_64

pythonバージョンを確認する

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$python3 -V
Python 3.9.16
$python3 -V Python 3.9.16
$python3 -V
Python 3.9.16

Ansibleバージョンを確認する

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# ansible --version
ansible [core 2.14.2]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.11/site-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /bin/ansible
python version = 3.11.2 (main, May 24 2023, 00:00:00) [GCC 11.3.1 20221121 (Red Hat 11.3.1-4)] (/usr/bin/python3.11)
jinja version = 3.1.2
libyaml = True
# ansible --version ansible [core 2.14.2] config file = /etc/ansible/ansible.cfg configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python3.11/site-packages/ansible ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections executable location = /bin/ansible python version = 3.11.2 (main, May 24 2023, 00:00:00) [GCC 11.3.1 20221121 (Red Hat 11.3.1-4)] (/usr/bin/python3.11) jinja version = 3.1.2 libyaml = True
# ansible --version
ansible [core 2.14.2]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.11/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /bin/ansible
  python version = 3.11.2 (main, May 24 2023, 00:00:00) [GCC 11.3.1 20221121 (Red Hat 11.3.1-4)] (/usr/bin/python3.11)
  jinja version = 3.1.2
  libyaml = True

操作方法
1.Python関連パッケージのインストール
Ansibleは、Pythonパッケージ管理システムのpipを使用してインストールします。そのため、
先にPythonとpipのRPMパッケージをインストールします。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# dnf -y install python3-pip
# dnf -y install sshpass
# dnf -y install python3-pip # dnf -y install sshpass
# dnf -y install python3-pip
# dnf -y install sshpass

2.Ansible用のOSユーザ作成

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# useradd ansible
# passwd ansible
ユーザー ansible のパスワードを変更。
新しい パスワード:
新しい パスワードを再入力してください:
passwd: すべての認証トークンが正しく更新できました。
# useradd ansible # passwd ansible ユーザー ansible のパスワードを変更。 新しい パスワード: 新しい パスワードを再入力してください: passwd: すべての認証トークンが正しく更新できました。
# useradd ansible
# passwd ansible
ユーザー ansible のパスワードを変更。
新しい パスワード:
新しい パスワードを再入力してください:
passwd: すべての認証トークンが正しく更新できました。

3.Ansibleをインストール
ansibleユーザのホームディレクトリにAnsibleをインストールするため、ユーザをrootからansibleへ変更します。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# su - ansible
[ansible@localhost ~]$ pwd
/home/ansible
# su - ansible [ansible@localhost ~]$ pwd /home/ansible
# su - ansible
[ansible@localhost ~]$ pwd
/home/ansible

pipを使用して、PythonパッケージとしてAnsibleをインストールします。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ pip3 install ansible --user
Successfully installed MarkupSafe-2.1.3 ansible-8.4.0 ansible-core-2.15.4 cffi-1.15.1 cryptography-41.0.4 importlib-resources-5.0.7 jinja2-3.1.2 packaging-23.1 pycparser-2.21 resolvelib-1.0.1
$ pip3 install ansible --user 略 Successfully installed MarkupSafe-2.1.3 ansible-8.4.0 ansible-core-2.15.4 cffi-1.15.1 cryptography-41.0.4 importlib-resources-5.0.7 jinja2-3.1.2 packaging-23.1 pycparser-2.21 resolvelib-1.0.1
$ pip3 install ansible --user
略
Successfully installed MarkupSafe-2.1.3 ansible-8.4.0 ansible-core-2.15.4 cffi-1.15.1 cryptography-41.0.4 importlib-resources-5.0.7 jinja2-3.1.2 packaging-23.1 pycparser-2.21 resolvelib-1.0.1

Ansibleのバージョンが表示できれば、インストールは完了です。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ ansible --version
ansible [core 2.15.4]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/ansible/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/ansible/.local/lib/python3.9/site-packages/ansible
ansible collection location = /home/ansible/.ansible/collections:/usr/share/ansible/collections
executable location = /home/ansible/.local/bin/ansible
python version = 3.9.16 (main, Dec 8 2022, 00:00:00) [GCC 11.3.1 20221121 (Red Hat 11.3.1-4)] (/usr/bin/python3)
jinja version = 3.1.2
libyaml = True
$ ansible --version ansible [core 2.15.4] config file = /etc/ansible/ansible.cfg configured module search path = ['/home/ansible/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /home/ansible/.local/lib/python3.9/site-packages/ansible ansible collection location = /home/ansible/.ansible/collections:/usr/share/ansible/collections executable location = /home/ansible/.local/bin/ansible python version = 3.9.16 (main, Dec 8 2022, 00:00:00) [GCC 11.3.1 20221121 (Red Hat 11.3.1-4)] (/usr/bin/python3) jinja version = 3.1.2 libyaml = True
$ ansible --version
ansible [core 2.15.4]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/ansible/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/ansible/.local/lib/python3.9/site-packages/ansible
  ansible collection location = /home/ansible/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/ansible/.local/bin/ansible
  python version = 3.9.16 (main, Dec  8 2022, 00:00:00) [GCC 11.3.1 20221121 (Red Hat 11.3.1-4)] (/usr/bin/python3)
  jinja version = 3.1.2
  libyaml = True

4.SSH公開鍵認証の設定
ansibleOSユーザのリモートログインに公開鍵認証方式を使用するために、SSHキーペアを作成します。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ansible/.ssh/id_rsa): #Enterキーを入力
Created directory '/home/ansible/.ssh'.
Enter passphrase (empty for no passphrase): #Enterキーを入力
Enter same passphrase again: #Enterキーを入力
Your identification has been saved in /home/ansible/.ssh/id_rsa
Your public key has been saved in /home/ansible/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:1XOpXLFeZD3SaRcP8r7DfXvq8gsYVu8Pd/mVvBfa2aY ansible@localhost.localdomain
The key's randomart image is:
+---[RSA 3072]----+
| . +.*|
| . + %+|
| . + O =|
| . o O . |
| S o o + |
| . o o.+o|
| . . O+X|
| .o B%|
| +EB=|
+----[SHA256]-----+
$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/ansible/.ssh/id_rsa): #Enterキーを入力 Created directory '/home/ansible/.ssh'. Enter passphrase (empty for no passphrase): #Enterキーを入力 Enter same passphrase again: #Enterキーを入力 Your identification has been saved in /home/ansible/.ssh/id_rsa Your public key has been saved in /home/ansible/.ssh/id_rsa.pub The key fingerprint is: SHA256:1XOpXLFeZD3SaRcP8r7DfXvq8gsYVu8Pd/mVvBfa2aY ansible@localhost.localdomain The key's randomart image is: +---[RSA 3072]----+ | . +.*| | . + %+| | . + O =| | . o O . | | S o o + | | . o o.+o| | . . O+X| | .o B%| | +EB=| +----[SHA256]-----+
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ansible/.ssh/id_rsa): #Enterキーを入力
Created directory '/home/ansible/.ssh'.
Enter passphrase (empty for no passphrase):                      #Enterキーを入力
Enter same passphrase again:                                     #Enterキーを入力
Your identification has been saved in /home/ansible/.ssh/id_rsa
Your public key has been saved in /home/ansible/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:1XOpXLFeZD3SaRcP8r7DfXvq8gsYVu8Pd/mVvBfa2aY ansible@localhost.localdomain
The key's randomart image is:
+---[RSA 3072]----+
|            . +.*|
|           . + %+|
|          . + O =|
|         . o O . |
|        S o o +  |
|         . o o.+o|
|          . . O+X|
|            .o B%|
|             +EB=|
+----[SHA256]-----+

秘密鍵ファイル(id_rsa)と公開鍵ファイル(id_rsa.pub)は、以下の場所に作成されます。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ ls -l /home/ansible/.ssh/
合計 8
-rw-------. 1 ansible ansible 2622 925 10:29 id_rsa
-rw-r--r--. 1 ansible ansible 583 925 10:29 id_rsa.pub
$ ls -l /home/ansible/.ssh/ 合計 8 -rw-------. 1 ansible ansible 2622 9月 25 10:29 id_rsa -rw-r--r--. 1 ansible ansible 583 9月 25 10:29 id_rsa.pub
$ ls -l /home/ansible/.ssh/
合計 8
-rw-------. 1 ansible ansible 2622  9月 25 10:29 id_rsa
-rw-r--r--. 1 ansible ansible  583  9月 25 10:29 id_rsa.pub

 

Ansible

Posted by arkgame