0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ansibleに関して(入門編)

Posted at

ansibleの概要

構成管理ツールのこと。ansibleの他にChef, Puppetなどがある。
構成を設定するのにpythonを使用。
設定の大まかな流れは、
ホスト側がSSHでターゲット側に接続しyaml形式のコードを用いて環境を構築する。

install方法

準備するもの

ホスト側:ansible
ターゲット側:ssh接続できる環境

方法

公式documentsを参考にしてインストールする。
方法はいくつかあるが、今回採用した方法はpipでのインストール

$ pip install ansible

使い方

インストールの確認

まずはansibleが入っているかを調べる。

$ ansible --version

ssh接続の確認

ターゲット側とssh接続ができているかを確認する。

$ ansible -i hosts <ip_address> -m ping

成功例

3.17.24.29 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

設定ファイルの書き方

ansibleを構成する要素として以下の3つがある。
*インベントリとプレイブックの二つは自分で作成する必要がある。(モジュールは書かなくてもいい)

  1. インベントリ(inventory)
    被管理ホストへの接続情報を記述するファイル
  2. モジュール(module)
    ユーザーを追加する、ファイルをコピーする、などの冪等性を持った操作。数百のモジュールがあらかじめ用意されている
  3. プレイブック(playbook)← メインの設定ファイル
    DBサーバを作る、デプロイを行う、などの目的のためにモジュールを組み合わせた作った手順書

inventoryファイル例

[targets]      # ターゲット側の"hosts"名
18.176.224.172 ansible_ssh_user=ubuntu ansible_ssh_private_key_file=~/.ssh/math-machine-2           # IPアドレス ターゲット側のユーザー名 ssh鍵のpath

playbookファイル例

- hosts: targets                  # 対象となる相手の名前(インベントリファイルで設定した名前)
  become: yes                     # sudo権限での実行か否か
  become_user: ubuntu             # 実行するユーザー名(デフォルトはrootになっている)
  tasks:                          # 行う具体的な設定(今回は文字列の表示)
    - name: Test ansible-playbook
      debug: msg="Hello world"

実行例

TAKE-PC-6:ansible take-pc-6$ ansible-playbook -i hosts test.yml

PLAY [server] ***********************************************************************

TASK [Gathering Facts] **************************************************************
ok: [3.17.24.29]

TASK [Test ansible-playbook] ********************************************************
ok: [3.17.24.29] => {
    "msg": "Hello world"
}

PLAY RECAP **************************************************************************
3.17.24.29                 : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

チュートリアル

今回はrubyとgitをansibleを用いてインストールする。
package.yml

- hosts: target
  user: ubuntu
  become: yes
  tasks:
    - name: install packages from apt
      apt: name={{ item }} state=latest
      with_items:
        - ruby
        - git

反応

TAKE-PC-6:ansible take-pc-6$ ansible-playbook -i hosts package.yml

PLAY [server] ***********************************************************************

TASK [Gathering Facts] **************************************************************
ok: [3.17.24.29]

TASK [install packages from apt] ****************************************************
[DEPRECATION WARNING]: Invoking "apt" only once while using a loop via
squash_actions is deprecated. Instead of using a loop to supply multiple items and
specifying `name: "{{ item }}"`, please use `name: ['ruby', 'git']` and remove the
loop. This feature will be removed in version 2.11. Deprecation warnings can be
disabled by setting deprecation_warnings=False in ansible.cfg.
changed: [3.17.24.29] => (item=['ruby', 'git'])
 [WARNING]: Could not find aptitude. Using apt-get instead


PLAY RECAP **************************************************************************
3.17.24.29                 : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

warningが出たが構成管理コード通りにインストールすることができた。

参考サイト

参考サイト:
[1] UbuntuでAnsiblefから動作確認まで
[2] ansibleで疎通確認とplaybookの実行
[3] Ansibleをはじめる人に。
[4] Ansibleを効果的に使うのに欠かせないPlaybookの基本的な書き方まとめ (1/2)
[5] Ansibleのドキュメント

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?