LoginSignup
17
14

More than 5 years have passed since last update.

Ansibleの構築

Last updated at Posted at 2016-04-17

環境

CentOS6.7 64bit

インストール

CentOS6.7の標準リポジトリにはAnsibleがない為、
EPELリポジトリをインストールします。

yum install epel-release

EPELリポジトリを他のライブラリのインストールに使用しない場合は以下のコマンドを実行します。

sed -i -e 's/enabled *= *1/enabled=0/g' /etc/yum.repos.d/epel.repo

Ansibleをインストールする。

yum install ansible
or
yum --enablerepo=epel install ansible <- EPELリポジトリのenabledを0にした場合

設定ファイル

Ansibleをインストールすると/etc/ansibleに以下のファイル、フォルダが作成されます。

hosts <- 管理ホストのインベントリファイル
ansible.cfg <- Ansible設定ファイル
role <- ルールフォルダ

hosts

Ansibleの対象ホストを記載するファイルです。
環境変数(ANSIBLE_HOSTS)やAnsible設定ファイル等で
他の場所のhostsファイルを読み込む事もできます。
hostsファイルは以下の様に記載します。

test.example.com
# グループで指定する場合
[webservers]
test1.example.com
test2.example.com
test[1:2].example.com <- test1.example.com, test2.example.comと同様

# IPアドレスでも可能
[databases]
192.168.0.1
192.168.0.2

Ansible設定ファイル

Ansibleの設定ファイルは他の場所に設置する事も可能です。
Ansibleの設定ファイルは以下の順番で読み込みます。
(各場所に設定ファイルが存在しても優先順位が高い設定ファイルの設定が適用されます。)

[優先順位:高]
ANSIBLE_CONFIG
ansible.cfg <- カレントディレクトリ
.ansible.cfg <- ホームディレクトリ
/etc/ansible/ansible.cfg
[優先順位:低]

実行

ansibleかansible-playbookを使用して実行します。

ansible

ansibleで実行する場合は以下のようにします。
[例]httpdをスタートする。

ansible localhost -m service -a "name=httpd state=started"

ansible-playbook

実行したいタスクのPlaybooksを作成します。
[Playbooks(httpd.yml)]

---
- hosts: localhost
  tasks:
  - name: Start httpd
    action: service name=httpd state=started

ansible-playbookの引数にPlaybooksを選んで実行します。
[例]httpdをスタートする。

ansible-playbook httpd.yml

17
14
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
17
14