1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Docker】 Ansibleを使ってDockrtホストを構築する

1
Posted at

1. Ansibleとは

Ansible(アンシブル)とは:
サーバー構成管理・デプロイ・自動化を行うオープンソースの構成管理ツール
SSHを利用してリモートサーバーを制御し、「Playbook」と呼ばれるYAML形式の手順書に従いパッケージインストールや設定変更を自動的に実行できる。


2. Ansibleを使ってDockrtホストを構築する

1. SSH設定

1.1 鍵ペアを生成(Control Node側)

ssh-keygen -t ed25519 -C "ansible-key"

生成場所:~/.ssh/id_ed25519(秘密鍵)
    :~/.ssh/id_ed25519.pub(公開鍵)


1.2 公開鍵をリモートホストへコピー

ssh-copy-id root@192.168.10.64

テスト接続:

ssh root@192.168.10.64

→パスワード無しで接続できるか確認する


2. インベントリ(ホスト一覧)を作成

hosts.ini

[web]
192.168.10.64 ansible_user=root
  • [web] :グループ名
  • ansible_user :接続ユーザーを指定

3. Playbook作成

install_apache.yml

- name: Install Apache on Remote Server     # 全体の名前
  hosts: web                                # 対象ホストのグループ名
  become: true                              # 管理者権限で実行

  tasks:                                    # *** 以降にタスクを記述 ***
    - name: Install Apache package          # タスク1の名前
      ansible.builtin.dnf:                  # dnf モジュール
        name: httpd                         # インストール対象パッケージ名
        state: present                      # 無ければ入れる/あれば何もしない

    - name: Enable and start Apache         # タスク2の名前
      ansible.builtin.service:              # service モジュール
        name: httpd                         # 対象サービス名
        state: started                      # 起動する/起動済みなら変更なし
        enabled: true                       # 自動起動を有効化/既に有効なら変更なし

    - name: Deploy index.html               # タスク3の名前
      ansible.builtin.copy:                 # copy モジュール
        dest: /var/www/html/index.html      # 配置先パス
        content: |                          # ファイル内容(HTML本文)
          <h1>Apache Installed via Ansible (Remote)</h1>  
        mode: '0644'                        # パーミッション

4. PlayBookをコマンドで実行

ansible-playbook -i hosts.ini install_apache.yml

出力例:

PLAY [Install Apache on Remote Server] ******************************************

TASK [Install Apache package] ***************************************************
changed: [192.168.10.64]

TASK [Enable and start Apache] **************************************************
changed: [192.168.10.64]

TASK [Deploy index.html] ********************************************************
changed: [192.168.10.64]

PLAY RECAP **********************************************************************
192.168.10.64             : ok=3    changed=3    unreachable=0    failed=0

5. 動作確認(リモート側)

ssh root@192.168.10.64
systemctl status httpd

→ ブラウザで確認 「Apache Installed via Ansible (Remote)」と表示されれば成功

補足 Ansible設定ファイルについて

もし毎回 -i hosts.ini-u root を書くのが面倒な場合は、
設定ファイル /etc/ansible/ansible.cfg(または ~/.ansible.cfg)に追記:

[defaults]
inventory = ./hosts.ini
remote_user = root
host_key_checking = False

→ 以後は単純に:

ansible-playbook install_apache.yml

参考

Ansibleとは何か 構成管理ツールの目的〜Ansible導入まで最速で理解する

伊藤裕一著『たった1日で基本が身に付く Docker/Kubernetes超入門』技術評論社
p78~p84 chapter2 sestion4 Ansibleを使ってDockrtホストを構築しよう

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?