LoginSignup
4
6

More than 5 years have passed since last update.

Ansibleの導入と簡単な使い方

Posted at

Ansibleのインストールと簡単な使い方。
前提条件としてpythonのバージョンが2.7以上であること。
また、easy_installが使用可能であること。

Ansibleのインストール

yumとpipで必要なライブラリをインストール。

sudo yum install -y epel-release
sudo yum install -y sshpass
sudo yum install -y python-devel
sudo easy_install pip
sudo pip install paramiko PyYAML Jinja2 httplib2

続いてAnsibleをインストール。

sudo pip install ansible

Ansibleコマンドでバージョンが表示できればインストール完了

ansible --version

PlayBookを使用する

接続情報を記述したhostsファイルを作成。
下記の例はtargetsという接続先グループにホスト名serverのホストを定義。

[targets]
server

続いてplaybookのファイルを作成。
下記の例はtargetsグループ全てのホストを対象にしてplaybookを実行。
ユーザーはec2-user、sudoしてrootでコマンドを実行。
タスクは/home/ec2-userの配下にtestディレクトリを作成する。

operation.yml
- hosts: targets
  user: ec2-user
  become: yes
  tasks:
  - file: path=/home/ec2-user/test state=directory owner=ec2-user group=ec2-user mode=0755

下記のコマンドでplaybookを実行するとディレクトリが作成される。
-Dオプションは変更した差分をコンソールに出力するものである。

ansible-playbook -i ./hosts ./operation.yml -D

差分だけを確認したいときは下記のように-Cオプションを使用する。

ansible-playbook -i ./hosts ./operation.yml -D -C
4
6
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
4
6