💨

ansibleでchmodするときのmode指定について

2022/07/11に公開

社内でansibleの質問があった際の内容を記事にしました。

質問

modeのパーミッション指定は、先頭に0を付けるのが慣例なんでしょうか?
というのも、modeの755で指定して、playbookを実行したところ、ディレクトリはできたのですが、
lsコマンドで見ようとしても、Permission denyが発生してしまいました。
※パーミッションも ?? で表示されてしまいました。

結論

下記の2パターンのどちらかで指定することがansibleの仕様上必要
・ゼロ付きで指定する
 例)mode: 0755
・ダブルクォーテーション(")で囲む
 例)mode: "755"

検証

work 配下のconfigure のパーミッションを変更する。
755のみ意図しないパーミッションとなることがわかる。

パターン1 0755

[user@localhost work]$ ls -l
total 4
drwxr-xr-x. 2 user user   6 Jun 24 08:45 configure
-rw-rw-r--. 1 user user 189 Jun 24 08:46 test.yml
[user@localhost work]$

パターン2 755

[user@localhost work]$ ls -l
total 4
d-wxrw--wt. 2 user user   6 Jun 24 08:45 configure
-rw-rw-r--. 1 user user 188 Jun 24 08:46 test.yml
[user@localhost work]$

パターン3 "755"

[user@localhost work]$ ls -l
total 4
drwxr-xr-x. 2 user user   6 Jun 24 08:45 configure
-rw-rw-r--. 1 user user 190 Jun 24 08:48 test.yml
[user@localhost work]$

参考サイト

file – Manage files and file properties
https://docs.ansible.com/ansible/2.9_ja/modules/file_module.html#file-module

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.
Giving Ansible a number without following one of these rules will end up with a decimal number which will have unexpected results.

日本語訳↓

AnsibleのYAMLパーサーが8進数(0644や01777など)であることを認識できるように先行ゼロを追加するか、Ansibleが文字列を受信して文字列から独自の変換を実行できるように引用符で囲む必要があります 数に。
これらのルールのいずれにも従わずにAnsibleに数値を指定すると、10進数になり、予期しない結果が発生します。

Discussion