2
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を使ってみる

Last updated at Posted at 2019-11-04

概要

Ansibleを触る機会があったので、あまり詳しくないため調べたことをメモする。

Ansibleとは

Ansibleとは、エージェントレスの構成管理ツールとのことです。
操作対象サーバにエージェント(ツール)をインストールしなくても操作できるツールらしい。
似たようなツールでPuppetChefなどもあるがそちらはエージェントが必要らしい。

まとめると以下のような違いがあるようです。

  • エージェントレス型 ( Ansible )
    ※ プル型:
     操作するサーバから操作対象サーバへログインして操作する

  • エージェント型 ( Puppet, Chef )
    ※ プッシュ型:
     操作対象サーバのエージェントが中央サーバから必要な情報を取得して操作する

Ansibleの構成

以下の3つの機能で構成されているようです。

  • Inventory
    ※ 操作対象サーバの情報を定義

  • Module
    ※ Ansibleの実行コマンド

  • Playbook
    ※ Ansibleで実行する処理内容を定義

今回の構成

vagrantでCentOS7を起動し、Ansibleで操作してみる。

structure.png

ファイル構成

今回は、以下のようになる予定です。

├ ansible.cfg      <- ansibleの設定
├ hosts            <- Inventryファイル
├ site.yml         <- Playbookファイル
└ Vagrantfile

前提条件

$ brew cask install virtualbox vagrant
  • ansible インストール済み
$ curl -fsSL https://bootstrap.pypa.io/get-pip.py | sudo python2
$ sudo pip2 install ansible

vagrant準備

下記のvagrantイメージを利用することにします。
https://app.vagrantup.com/bento/boxes/centos-7

$ vagrant init bento/centos-7

起動して、サーバ情報を確認する。

$ vagrant up
$ vagrant ssh-config

■ Inventoryファイルの作成

hostsファイルで、どのマシンに接続するかの定義を行います。

hosts
vagrant-machine ansible_host=127.0.0.1 ansible_port=2222 ansible_user=vagrant ansible_ssh_private_key_file=.vagrant/machines/default/virtualhost/private_key

「~/.ssh/known_hosts」ファイルのチェックを無効化しておく。

ansible.cfg
[defaults]
host_key_checking = False

操作対象のサーバへ接続を確認してみる。

$ ansible all -i hosts -m ping

■ Playbookの作成 (基本形)

Playbookの基本形のYAMLファイルを作成する。

site.yml
---
- name: Playbookチュートリアル
  hosts: all
  tasks:

Playbookを実行してみる。

$ ansible-playbook -i hosts site.yml 

setupにより自動収集したサーバ情報を確認してみる。

$ ansible all -i hosts -m setup

■ Playbookに管理者権限を付与

管理者権限でPlaybookを実行するようにします。
become 属性を追加する

site.yml
---
- name: Playbook Sample
  hosts: all
  become: true
  tasks:

SeLinux対応

CentOS 7系のSELinuxをPythonから扱えるようにする。

site.yml
---
- name: Playbook Sample
  hosts: all
  become: true
  tasks:
    - name: install libselinux-python
      yum:
        name: libselinux-python
        state: present

※ state属性について
 present: インストール状態(installed)
 absent: 非インストール状態(removed)
 latest: 最新版インストール状態

EPELリポジトリ

EPELリポジトリをインストールします。

site.yml
---
- name: Playbook Sample
  hosts: all
  become: true
  tasks:
    - name: install libselinux-python
      yum:
        name: libselinux-python
        state: present

    - name: install EPEL Repository
      yum:
        name: epel-release
        state: present

Nginxのインストール

PlaybookでNginxをインストールして起動してみる。

site.yml
---
- name: Playbook Sample
  hosts: all
  become: true
  tasks:
    - name: install libselinux-python
      yum:
        name: libselinux-python
        state: present

    - name: install EPEL Repository
      yum:
        name: epel-release
        state: present

    - name: install Nginx
      yum:
        name: nginx
        state: present

    - name: start nginx & auto start setting
      service:
        name: nginx
        state: started
        enabled: true

※ state属性について
 started: 起動状態
 stopped: 停止状態
 restarted: 再起動実施(停止+起動)
 reloaded: リロード実行 (ワーカープロセスのみ再起動)

Playbookを実行してみる。

$ ansible-playbook -i hosts site.yml 

ブラウザで確認してみる。

$ open http://192.168.33.10

nginx.png

モジュールの使い方について

下記のようにansible-docコマンドにてモジュールのマニュアルが確認できるようです。

$ ansible-doc yum

参考文献

以上

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?