LoginSignup
2
1

More than 3 years have passed since last update.

オフライン環境でAnsible #1 ~サーバー構築編~

Last updated at Posted at 2021-01-19

はじめに

普段は、地道に最下層から仮想環境を作成しクライアントOSを導入するインフラエンジニアです。なので、専門用語はもちろん、自動化や効率化とは若干無縁。そんな人間がAnsible使うぞ!使えるようにするぞ!を書き下してる記事です。

背景

サーバーの自動構築を勉強中。構築自体はTerraformを利用するが、構築後の設定を行うAnsibleを組み合わせたい。よし、Ansibleサーバーを立てよう。

ここでは、2つのサーバーが出てきます。ここがこんがらかるとSSH鍵認証が面倒。

  • Ansibleサーバー
    • Ansibleモジュールがインストールされている(構築手順内でインストールする)
    • Ansibleクライアントに対してAnsibleを実行する
    • Ansibleで設定されることはない
    • 接続元のユーザーはroot
  • Ansibleクライアント
    • Ansibleモジュールはインストールされない(構築手順内でもインストールされない)
    • Ansibleを流される対象
    • 接続先になるユーザーはroot

やったこと

  • Ansibleサーバーの準備
    • Ansible パッケージのインストール
    • AnsibleサーバーとクライアントサーバーのSSH接続
    • Ansible playbook テスト

やらなかったこと

  • 既存イメージのクローンのため以下は今回の手順の対象外
    • OSの導入
    • SSHの導入/設定
    • ファイアーウォールの設定(オフ)
    • SELinux(オフ)

Ansibleサーバー構築手順

構築環境

  • IBM Power System S824 (Virtual I/O Client)
  • Red Hat Enterprise Linux
    • minimal install, sshでのrootログイン,Password認証は使用可能な状態

AnsibleサーバーへのAnsibleモジュールをインストール

必要なパッケージ(RPM)

オフラインでのansibleインストールでは、依存関係を解決した上で以下のファイルセットが必要でした。

Epel 7

  • libtommath-0.42.0-5.el7.ppc64le.rpm
  • libtomcrypt-1.17-25.el7.ppc64le.rpm
  • python2-crypto-2.6.1-16.el7.ppc64le.rpm
  • python-keyczar-0.71c-2.el7.noarch.rpm
  • python-paramiko-2.1.1-0.10.el7.noarch.rpm
  • python2-jmespath-0.9.0-1.el7.noarch.rpm
  • python-httplib2-0.9.2-0.2.el7.noarch.rpm
  • sshpass-1.06-1.el7.ppc64le.rpm
  • ansible-2.9.1-1.el7.noarch.rpm
  1. このファイルセットをlocalinstallで導入
# cd {ファイルをおいた場所}
# yum localinstall -y ./*.rpm
  1. Ansibleのバージョンを確認
# ansible --version  
ansible 2.9.1
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Sep 12 2018, 09:40:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]

これでAnsibleは使えそうということが確認できました。

SSH接続の準備

Qiita上に勉強になる記事は多いのですが、日本語の読み取りが下手なためAnsibleを実行するサーバー、Ansibleが流されるサーバー(クライアント)を理解するために書き下します。ここで難しいのは、sshサーバーはAnsibleが流される側(クライアント)であり、sshクライアント(アクセスする方)はAnsible実行サーバーということです……。

[Ansibleサーバー側作業]
接続のための鍵を作成します。パスフレーズは今回なしにします。

# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/id_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.

これでOK, 繋ぐための材料はできました。
id_rsaは秘密鍵(自分で保持する方), id_rsa.pubは公開鍵(接続する相手に渡す方)です。

それでは実際に繋ぐ工程へ。公開鍵を対象サーバー(Ansibleクライアント)に配布します。
まずは、鍵を置く場所の準備をします。アクセス権限が重要です。初回のみの作業です。
ssh公開鍵認証設定まとめ参照

[Ansibleクライアント側作業]

ssh ansible@ansible_client
mkdir ./.ssh/
chmod 700 ./.ssh/
touch .ssh/authorized_keys
chmod 600 .ssh/authorized_keys

クライアント側の受入体制(ディレクトリとファイルの作成)が整ったら、公開鍵をコピーしてあげます。
ssh公開鍵認証設定まとめの一行でコピーするやり方が簡単だった。

[Ansibleサーバー側作業]

cat ~/.ssh/id_rsa.pub | ssh ansible@ansible_client 'cat >> .ssh/authorized_keys'

公開鍵を閲覧してAnsibleクライアントサーバーにSSH接続しながら閲覧内容をssh先のauthorized_keysに出力……という流れです。確認したらこう見えるはず。

[Ansibleクライアント側作業]

$ cat /home/ansible/.ssh/authorized_keys
ssh-rsa (公開鍵/省略)(ホスト名/省略)

確認のため、接続

# ssh ansible@ansible_client
Last login: Fri Dec 13 15:47:21 2019 from 172.16.81.4
[ansible@ansible_client ~]$ 

できた。完璧。

オフライン環境でAnsible #2 ~Ansibleトラブル編~ に続く

参考

SSH接続

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1