LoginSignup
30
27

More than 5 years have passed since last update.

AnsibleのRoleディレクトリレイアウトを一発で作る方法

Posted at

Roleのディレクトリレイアウトとは、ドキュメントで説明されているDirectory Layoutのことです。

rolesの箇所を抜粋

roles/
    common/               # this hierarchy represents a "role"
        tasks/            #
            main.yml      #  <-- tasks file can include smaller files if warranted
        handlers/         #
            main.yml      #  <-- handlers file
        templates/        #  <-- files for use with the template resource
            ntp.conf.j2   #  <------- templates end in .j2
        files/            #
            bar.txt       #  <-- files for use with the copy resource
            foo.sh        #  <-- script files for use with the script resource
        vars/             #
            main.yml      #  <-- variables associated with this role
        defaults/         #
            main.yml      #  <-- default lower priority variables for this role
        meta/             #
            main.yml      #  <-- role dependencies

    webtier/              # same kind of structure as "common" was above, done for the webtier role
    monitoring/           # ""
    fooapp/  

tasks,handlers,templates... とフォルダ構成となっていて、一つのyamlファイルに書いていくよりも
このディレクトリ構成のほうが整理しやすくて良いです。

ただ、久しぶりにroleを作る時とか構成を忘れてしまいますよね。
毎回手打ちで作るのは面倒だし、typoしたらみっともないし、このディレクトリ作るためだけにshell作るのもなーと。

これ、実はansible-galaxyコマンドのinitサブコマンドを実行することで一通りつくってくれます。

ansible-galaxy init (your_role_name)

$ ansible-galaxy init myrole
$ tree myrole/
myrole/
├── README.md
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml

testsmetaも作られますが、不要なら削除してしまえばいいです。

ansible-galaxy自体はAnsible Galaxyの操作を行うコマンドですが、
initが結構便利で、私は適当なroleを作る時でもこのコマンドで毎回作ってしまします。

良きAnsibleライフを!

30
27
1

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
30
27