Ansible 環境構築
YAML 形式の Playbook を作成して、構築が行える Ansible。
Ansible を使って、環境を構築できるように Ansible 環境を構築します~
環境の構成としては、とりあえず、以下。
【Ansible サーバ】
192.168.200.13
【対象端末】
192.168.200.21
Ansible インストール
まずは、RPM のパッケージをインストールして、Ansible サーバを構築します。
# dnf -y install epel-release # dnf -y install ansible
# ansible --version ansible 2.9.14 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.6/site-packages/ansible executable location = /usr/bin/ansible python version = 3.6.8 (default, Apr 16 2020, 01:36:27) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
パスワードなしで、対象端末を操作できるように鍵を生成する。
# ssh-keygen -t rsa -f $HOME/.ssh/id_rsa -N ""
Ansible で制御する端末を準備
Ansible で制御するには、対象の端末に以下の準備が必要になります。
作業は、対象端末上で行います。
- Ansible 専用のアカウントの追加
- パスワードなしで、root 権限でコマンドを実行できるように、設定
# useradd ansible # passwd ansible
sudoers に ansible アカウントをパスワードなしで、実行できるように、設定を追加します。
# visudo ansible ALL=(ALL) NOPASSWD: ALL
Ansible サーバで、動作確認実施。
[root@ansible]$ ssh ansible@192.168.200.21 Last login: Xxx Xxx xx xx:xx:xx xxxx from 192.168.200.13 [ansible@target]$ sudo su - Last login: Xxx Xxx xx xx:xx:xx xxxx from tty1 [root@target]$
Ansible サーバに対象を登録して、動作確認
制御端末のKeyを登録して、パスワード入力なしに制御できるようにします。
Ansible サーバで行います。
[root@ansible]$ ssh-copy-id ansible@192.168.200.21 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys ansible@192.168.250.21's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'ansible@192.168.200.21'" and check to make sure that only the key(s) you wanted were added.
設定を作成して、Ansible の ping モジュールを使って、動作するか確認します。
$ vi inventory/hosts [test] 192.168.200.21 ansible_user=ansible
$ ansible -i inventory/hosts test -m ping 192.168.200.21 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": false, "ping": "pong" }
ここまでくれば、Ansible の Playbook を作成して、実行することで、Ansible を使った構築ができる!