LoginSignup
0
0

More than 3 years have passed since last update.

【Ansible】 file モジュールで権限がバグってはまった話

Last updated at Posted at 2021-02-11

こんにちわ! @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

まとめ

しっかり公式ドキュメントは読みましょう。

0
0
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
0
0