Ansibleチートシート


Ansibleでよく使う操作をまとめてみました。

🚜 シェル系

シェルコマンドの実行

- shell: シェルで実行したいコマンド
args:
chdir: 実行するディレクトリ(省略可)

🐞 ファイル操作系

ファイルのパーミッション変更

- name: Changing perm of "/foo/bar.sh", adding "+x"
file: path=/foo/bar.sh mode=a+x

空のファイルを設置

- name: create empty file
file: path=/tmp/empty state=touch owner=root group=sys mode=0555

テンプレートファイルを登録

Jinja2テンプレートを使うと変数を展開して、サーバに設置できます。ファイル内の変数は の形式で設定します。

Ansibleの設定は次のように記述します。

- name: place conf file
template: src=files/hoge.conf.j2 dest=/etc/hoge.conf mode=0644 owner=root group=root

ファイルの最終行に追記

tasks:
- name: set locale
lineinfile: >
dest=/etc/cloud/cloud.cfg
line='locale: ja_JP.UTF-8'

特定の行の記述を修正

tasks:
- name: disable repo_upgrade
lineinfile: >
dest=/etc/cloud/cloud.cfg
regexp='repo_upgrade\:'
line='repo_upgrade: none'

ユーザー作成

- name: create group
group: name={{ group_name }}

- name: add users
user: name={{ user_name }} state=present groups=wheel

- name: user join to other group
user: name={{usergroup.user.name}} append=yes groups={{usergroup.group.name}}

🎉 補足:hostファイル内で変数を設定できる

hostファイル内に変数を設定できます。

[app]
host1 do_something=true

site.ymlに次のように記述します。

- role: something
when: do_something

🐮 今後読む記事

🏀 参考リンク

🖥 VULTRおすすめ

VULTR」はVPSサーバのサービスです。日本にリージョンがあり、最安は512MBで2.5ドル/月($0.004/時間)で借りることができます。4GBメモリでも月20ドルです。 最近はVULTRのヘビーユーザーになので、「ここ」から会員登録してもらえるとサービス開発が捗ります!

📚 おすすめの書籍