LoginSignup
13
10

More than 3 years have passed since last update.

Ansibleのインストールとディレクトリの作成

Last updated at Posted at 2016-04-02

はじめに

Macにansibleをインストールして
AnsibleのBest Practicesに記載されているディレクトリを
Ansibleで作成します。

前提

OS X EL Capitan (バージョン 10.11.4 )
Python 2.7.10

前提の確認

Macのバージョン確認

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.11.4
BuildVersion:   15E65

Pythonのバージョン確認

$ python --version
Python 2.7.10

Ansibleのインストール

ファイルディスクリプタの上限値を変更

$ sudo launchctl limit maxfiles 1024 unlimited
Password:
$ sudo launchctl limit maxfiles
Password:
    maxfiles    1024           10240

パッケージ管理ソフトのインストール

$ sudo easy_install pip
Searching for pip
Reading https://pypi.python.org/simple/pip/
Best match: pip 8.1.1
Downloading https://pypi.python.org/packages/source/p/pip/pip-8.1.1.tar.gz#md5=6b86f11841e89c8241d689956ba99ed7
Processing pip-8.1.1.tar.gz
Writing /tmp/easy_install-dxgLnF/pip-8.1.1/setup.cfg
Running pip-8.1.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-dxgLnF/pip-8.1.1/egg-dist-tmp-oBGMUE
warning: no previously-included files found matching '.coveragerc'
warning: no previously-included files found matching '.mailmap'
warning: no previously-included files found matching '.travis.yml'
warning: no previously-included files found matching '.landscape.yml'
warning: no previously-included files found matching 'pip/_vendor/Makefile'
warning: no previously-included files found matching 'tox.ini'
warning: no previously-included files found matching 'dev-requirements.txt'
warning: no previously-included files found matching 'appveyor.yml'
no previously-included directories found matching '.github'
no previously-included directories found matching '.travis'
no previously-included directories found matching 'docs/_build'
no previously-included directories found matching 'contrib'
no previously-included directories found matching 'tasks'
no previously-included directories found matching 'tests'
Adding pip 8.1.1 to easy-install.pth file
Installing pip script to /usr/local/bin
Installing pip2.7 script to /usr/local/bin
Installing pip2 script to /usr/local/bin

Installed /Library/Python/2.7/site-packages/pip-8.1.1-py2.7.egg
Processing dependencies for pip
Finished processing dependencies for pip

Ansibleのインストール

$ sudo pip install ansible
Collecting ansible
  Downloading ansible-2.0.1.0.tar.gz (1.5MB)
    100% |████████████████████████████████| 1.5MB 373kB/s
Collecting paramiko (from ansible)
  Downloading paramiko-1.16.0-py2.py3-none-any.whl (169kB)
    100% |████████████████████████████████| 174kB 430kB/s
Collecting jinja2 (from ansible)
  Downloading Jinja2-2.8-py2.py3-none-any.whl (263kB)
    100% |████████████████████████████████| 266kB 621kB/s
Collecting PyYAML (from ansible)
  Downloading PyYAML-3.11.tar.gz (248kB)
    100% |████████████████████████████████| 256kB 2.5MB/s
Requirement already satisfied (use --upgrade to upgrade): setuptools in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from ansible)
Collecting pycrypto>=2.6 (from ansible)
  Downloading pycrypto-2.6.1.tar.gz (446kB)
    100% |████████████████████████████████| 450kB 1.4MB/s
Collecting ecdsa>=0.11 (from paramiko->ansible)
  Downloading ecdsa-0.13-py2.py3-none-any.whl (86kB)
    100% |████████████████████████████████| 92kB 3.2MB/s
Collecting MarkupSafe (from jinja2->ansible)
  Downloading MarkupSafe-0.23.tar.gz
Installing collected packages: ecdsa, pycrypto, paramiko, MarkupSafe, jinja2, PyYAML, ansible
  Running setup.py install for pycrypto ... done
  Running setup.py install for MarkupSafe ... done
  Running setup.py install for PyYAML ... done
  Running setup.py install for ansible ... done
Successfully installed MarkupSafe-0.23 PyYAML-3.11 ansible-2.0.1.0 ecdsa-0.13 jinja2-2.8 paramiko-1.16.0 pycrypto-2.6.1

Ansibleのバージョン確認

$ ansible --version
ansible 2.0.1.0
  config file =
  configured module search path = Default w/o overrides

ローカルにディレクトリの作成

Best Practicesを元にansibleでローカルにwith_itemsに記載しているディレクトリを作成

init_setup.ymlを適当なディレクトリに作成

init_setup.yml
- hosts: localhost
  connection: local
  tasks:
    - name: "Create Directory"
      file:
        path={{item.path}}
        state=directory
      with_items:
        - path: production
        - path: staging
        - path: group_vars/group1
        - path: host_vars/group1
        - path: library/
        - path: filter_plugins/
        - path: roles/common/tasks/
        - path: roles/common/handlers/
        - path: roles/common/templates/
        - path: roles/common/files/
        - path: roles/common/vars/
        - path: roles/common/defaults/
        - path: roles/common/meta/

Fileモジュールを利用してディレクトリを作成します。
stateオプションをdirectoryと指定
pathオプションに{{item.path}}に記載し、with_items以下に作成するディレクトリ名を変数pathに記載
こう書くことによってwith_itemsに記載されている変数pathの値が、fileモジュールで繰り返し処理されます。

ansible-playbookコマンドでディレクトリの作成

$ ansible-playbook init_setup.yml
 [WARNING]: provided hosts list is empty, only localhost is available


PLAY ***************************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [Create Directory] ********************************************************
changed: [localhost] => (item={u'path': u'production'})
changed: [localhost] => (item={u'path': u'staging'})
changed: [localhost] => (item={u'path': u'group_vars/group1'})
changed: [localhost] => (item={u'path': u'host_vars/group1'})
changed: [localhost] => (item={u'path': u'library/'})
changed: [localhost] => (item={u'path': u'filter_plugins/'})
changed: [localhost] => (item={u'path': u'roles/common/tasks/'})
changed: [localhost] => (item={u'path': u'roles/common/handlers/'})
changed: [localhost] => (item={u'path': u'roles/common/templates/'})
changed: [localhost] => (item={u'path': u'roles/common/files/'})
changed: [localhost] => (item={u'path': u'roles/common/vars/'})
changed: [localhost] => (item={u'path': u'roles/common/defaults/'})
changed: [localhost] => (item={u'path': u'roles/common/meta/'})

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0

正常だと文字が緑色になります。
変更があった箇所が黄色い文字になります。
エラーだと赤くなります。

「PLAY RECAP」では、taskごとに変更の有無が記載されます。
もう一回、ansible-playbookコマンド実行すると「changed=0」になります。

まとめ

これで、ansibleで遊ぶ前準備ができました。
次は、EC2インスタンスを作成するコードを書いていきたいと思います。

13
10
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
13
10