Ansibleのインストール (RHEL 8 / CentOS 8)
Ansibleのインストール (RHEL 8 / CentOS 8)

Ansibleのインストール (RHEL 8 / CentOS 8)

RHEL 8 / CentOS 8におけるAnsibleのインストール手順を説明します。

前提条件

  • 作業は、root権限を持つユーザで実施します。
  • 以下のバージョンのソフトウェアで検証しています。
### OSバージョン # cat /etc/redhat-release CentOS Linux release 8.0.1905 (Core) # uname -r 4.18.0-80.el8.x86_64 ### Pythonバージョン # python3 -V Python 3.6.8 ### Ansibleバージョン $ ansible --version ansible 2.9.1

作業手順

Python関連パッケージのインストール

Ansibleは、Pythonパッケージ管理システムのpipを使用してインストールします。そのため、先にPythonとpipのRPMパッケージをインストールします。 SSH接続で使用するsshpassもここでインストールを行います。

# dnf -y install python3-pip epel-release (略) 完了しました! # dnf -y install sshpass (略) 完了しました! Ansible用のOSユーザ作成

Ansibleを使用するための専用ユーザansbileをOS上に作成します。

# useradd ansible # passwd ansible ユーザー ansible のパスワードを変更。 新しいパスワード: ←ansibleユーザのパスワードを入力 新しいパスワードを再入力してください: ←同様のパスワードを入力 passwd: すべての認証トークンが正しく更新できました。 Ansibleのインストール

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

# su - ansible $ pwd /home/ansible

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

$ pip3 install ansible --user (略) Successfully installed MarkupSafe-1.1.1 PyYAML-5.2 ansible-2.9.1 jinja2-2.10.3

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

$ ansible --version ansible 2.9.1 (略) SSH公開鍵認証の設定

Ansibleが動作するサーバ(以下、構成管理サーバ)と、Ansibleで構成管理を行う対象のサーバ(以下、管理対象サーバ)との間は、SSHで接続を行います。通常SSHの認証にはパスワードを使用しますが、公開鍵認証方式を使用することでパスワードの入力が不要となり、より安全に認証が行えます。

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

$ 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:nu0Ye71Qt6JvgTOlw0XJaNnoXD6DATnUtMQxxxxxxxx ansible@ansible The key's randomart image is: +---[RSA 2048]----+ | .o*oB =o | | o.X.@ | | =.O + | | + x E | | S. =.*. | | . oB... . | | + x=... | | =.oo. | | o.ooo. | +----[SHA256]-----+

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

$ ls -l /home/ansible/.ssh/ 合計 8 -rw-------. 1 ansible ansible 1823 12月 5 04:10 id_rsa -rw-r--r--. 1 ansible ansible 397 12月 5 04:10 id_rsa.pub

参考情報

Python関連パッケージ一覧

本作業でインストールするPython関連パッケージは以下の通りです。オフライン環境でインストールする場合は、リポジトリから取得したパッケージファイルをインストール対象サーバに転送してローカルインストールします。

# dnf install python3-pip (略) ================================================================================================= パッケージ アーキテクチャー バージョン リポジトリ サイズ ================================================================================================= Installing: python3-pip noarch 9.0.3-13.el8 AppStream 18 k 依存関係をインストール中: python36 x86_64 3.6.8-2.module_el8.0.0+33+0a10c0e1 AppStream 19 k python3-setuptools noarch 39.2.0-4.el8 BaseOS 162 k Enabling module streams: python36 3.6 トランザクションの概要 ================================================================================================= インストール 3 パッケージ ダウンロードサイズの合計: 200 k インストール済みのサイズ: 466 k Ansibleのインストール場所

Ansibleおよび関連するパッケージは、ansibleユーザのホームディレクトリに作成された.localディレクトリにPythonモジュールとしてインストールされます。

$ ls -l /home/ansible/.local/bin/ 合計 28 -rwxrwxr-x. 1 ansible ansible 5935 12月 4 18:54 ansible lrwxrwxrwx. 1 ansible ansible 7 12月 4 18:54 ansible-config -> ansible -rwxrwxr-x. 1 ansible ansible 12496 12月 4 18:54 ansible-connection lrwxrwxrwx. 1 ansible ansible 7 12月 4 18:54 ansible-console -> ansible lrwxrwxrwx. 1 ansible ansible 7 12月 4 18:54 ansible-doc -> ansible lrwxrwxrwx. 1 ansible ansible 7 12月 4 18:54 ansible-galaxy -> ansible lrwxrwxrwx. 1 ansible ansible 7 12月 4 18:54 ansible-inventory -> ansible lrwxrwxrwx. 1 ansible ansible 7 12月 4 18:54 ansible-playbook -> ansible lrwxrwxrwx. 1 ansible ansible 7 12月 4 18:54 ansible-pull -> ansible -rwxrwxr-x. 1 ansible ansible 795 12月 4 18:54 ansible-test lrwxrwxrwx. 1 ansible ansible 7 12月 4 18:54 ansible-vault -> ansible $ ls -l /home/ansible/.local/lib/python3.6/site-packages/ 合計 12 drwxrwxr-x. 2 ansible ansible 130 12月 4 18:54 Jinja2-2.10.3.dist-info drwxrwxr-x. 2 ansible ansible 106 12月 4 18:54 MarkupSafe-1.1.1.dist-info drwxrwxr-x. 2 ansible ansible 117 12月 4 18:54 PyYAML-5.2-py3.6.egg-info drwxrwxr-x. 18 ansible ansible 4096 12月 4 18:54 ansible drwxrwxr-x. 2 ansible ansible 157 12月 4 18:54 ansible-2.9.1-py3.6.egg-info drwxrwxr-x. 6 ansible ansible 88 12月 4 18:54 ansible_test drwxrwxr-x. 3 ansible ansible 4096 12月 4 18:54 jinja2 drwxrwxr-x. 3 ansible ansible 169 12月 4 18:54 markupsafe drwxrwxr-x. 3 ansible ansible 4096 12月 4 18:54 yaml
📎📎📎📎📎📎📎📎📎📎
BOT