こんにちわ! @ktoshi です!
先日、Ansible ではまってしまったことを共有しようと思います。
概要
Ansible でディレクトリを作ったが、権限設定が全然意図したものにならない。
事象
ディレクトリを作ろうとして下記のタスクを用意した。
- name: Create Test Directory.
file:
path: /opt/test
mode: 755
state: directory
よし、実行。
$ ansible-playbook -i hosts.yaml setup.yaml
PLAY [all] ***************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************
ok: [192.168.100.123]
TASK [Create Test Directory.] ********************************************************************************
changed: [192.168.100.123]
PLAY RECAP ***************************************************************************************************
192.168.100.123 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
うん、動いた。
$ ls -ld /opt/test/
d-wxrw--wt 2 root root 6 Feb 11 21:21 /opt/test/
バカな権限だなぁ!!(カミナリ風)
と、言う風に意図しない権限が設定されました。
原因
公式ドキュメントにちゃんと記載されている。
You must either add a leading zero so that Ansible's YAML parser knows it is an octal number (like 0644 or 01777) or quote it (like '644' or '1777') so Ansible receives a string and can do its own conversion from string into number.
file モジュールの「mode」要素で8進数を利用する際は頭に「0」を付けるか、「'」などで囲い、明示的に文字列で指定する必要がある。
そのため、下記のように指定する必要がある。
- name: Create Test Directory.
file:
path: /opt/test
mode: 0755
state: directory
もしくは
- name: Create Test Directory.
file:
path: /opt/test
mode: '755'
state: directory
まとめ
しっかり公式ドキュメントは読みましょう。