無能が苦悩したメモ

無能な著者が学んだことを忘れないための覚え書きです...

Ansible:fileモジュールによるディレクトリの作成

*ansible 2.9.6での設定を想定しています

ansibleでディレクトリを作成する場合、fileモジュールを使います。
fileモジュールのstateオプションにdirectoryを指定することでディレクトリが作成できます。

オプション 説明
path 作成するディレクトリのパス
owner 所有者
group 所有グループ
mode ディレクトリのパーミッション

以下は、/tmpディレクトリの下にtest1というディレクトリを作るプレイブックです。

---
- hosts: all
  become: yes
  tasks:
    - name: test directory created
      file:
        path: "/tmp/test1"
        state: directory
        owner: "root"
        group: "root"
        mode: "775"

複数のディレクトリを作成

loopを使えば複数のディレクトリを1つのtaskで作ることができます。

---
- hosts: all
  become: yes
  tasks:
    - name: test directory created
      file:
        path: "{{ item.path }}"
        state: directory
        owner: "{{ item.owner }}"
        group: "{{ item.group }}"
        mode: "{{ item.mode }}"
      loop:
        - path: "/tmp/test1"
          owner: "root"
          group: "root"
          mode: "775"
        - path: "/tmp/test2"
          owner: "root"
          group: "root"
          mode: "775"


リモートホスト(CentOS7)に対して実行した結果が以下になります。
test1、test2が作成されています。

$ ansible-playbook -i hosts make_dir/make_multi_dir.yml

PLAY [all] *********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [CentOS7.6]

TASK [test directory created] **************************************************
changed: [CentOS7.6] => (item={u'owner': u'root', u'path': u'/tmp/test1', u'group': u'root', u'mode': u'775'})
changed: [CentOS7.6] => (item={u'owner': u'root', u'path': u'/tmp/test2', u'group': u'root', u'mode': u'775'})

PLAY RECAP *********************************************************************
CentOS7.6                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

[root@centos7 tmp]# ll /tmp
合計 0
drwxrwxr-x. 2 root root 6  11 04:58 test1
drwxrwxr-x. 2 root root 6  11 04:58 test2

再帰的なディレクトリ作成

recurseオプションにyesを指定することで再帰的にディレクトリを作成することができます。

---
- hosts: all
  become: yes
  tasks:
    - name: test directory created
      file:
        path: "/tmp/test1/test1_1/test1_1_1"
        state: directory
        owner: "root"
        group: "root"
        mode: "775"
        recurse: yes


リモートホストに対して実行した結果が以下になります。
test1、test1_1、test1_1_1が作成されていることが確認できました。

$ ansible-playbook -i hosts make_dir/make_dir_recurse.yml

PLAY [all] *********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [CentOS7.6]

TASK [test directory created] **************************************************
changed: [CentOS7.6]

PLAY RECAP *********************************************************************
CentOS7.6                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

[root@centos7 tmp]# ll /tmp/
合計 0
drwxrwxr-x. 3 root root 21  11 04:50 test1
[root@centos7 tmp]#
[root@centos7 tmp]# ll /tmp/test1/
合計 0
drwxrwxr-x. 3 root root 23  11 04:50 test1_1
[root@centos7 tmp]#
[root@centos7 tmp]# ll /tmp/test1/test1_1/
合計 0
drwxrwxr-x. 2 root root 6  11 04:50 test1_1_1
[root@centos7 tmp]#


以下のようなプレイブックをつくりました。
プレイブックでは/tmp/test1/test1_1、/tmp/test1/test1_2を再帰的に作成します。

この時、ディレクトリはどのような順番で作成されるのでしょうか?
また、test1の所有者・権限はどうなるのでしょうか?

---
- hosts: all
  become: yes
  tasks:
    - name: test directory created
      file:
        path: "{{ item.path }}"
        state: directory
        owner: "{{ item.owner }}"
        group: "{{ item.group }}"
        mode: "{{ item.mode }}"
      loop:
        - path: "/tmp/test1/test1_1"
          owner: "root"
          group: "root"
          mode: "775"
        - path: "/tmp/test1/test1_2"
          owner: "muknow"
          group: "muknow"
          mode: "644"

$ ansible-playbook -i hosts make_dir/make_dir_recurse2.yml -v
Using /home/muknow/.ansible.cfg as config file

PLAY [all] *********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [CentOS7.6]

TASK [test directory created] **************************************************
changed: [CentOS7.6] => (item={u'owner': u'root', u'path': u'/tmp/test1/test1_1', u'group': u'root', u'mode': u'775'}) => {"ansible_loop_var": "item", "changed": true, "gid": 0, "group": "root", "item": {"group": "root", "mode": "775", "owner": "root", "path": "/tmp/test1/test1_1"}, "mode": "0775", "owner": "root", "path": "/tmp/test1/test1_1", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 6, "state": "directory", "uid": 0}
changed: [CentOS7.6] => (item={u'owner': u'muknow', u'path': u'/tmp/test1/test1_2', u'group': u'muknow', u'mode': u'644'}) => {"ansible_loop_var": "item", "changed": true, "gid": 1001, "group": "muknow", "item": {"group": "muknow", "mode": "644", "owner": "muknow", "path": "/tmp/test1/test1_2"}, "mode": "0644", "owner": "muknow", "path": "/tmp/test1/test1_2", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 6, "state": "directory", "uid": 1001}

PLAY RECAP *********************************************************************
CentOS7.6                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

[root@centos7 tmp]# ll /tmp
合計 0
drwxrwxr-x. 4 root root 36  11 05:16 test1
[root@centos7 tmp]# ll /tmp/test1/
合計 0
drwxrwxr-x. 2 root   root   6  11 05:16 test1_1
drw-r--r--. 2 muknow muknow 6  11 05:16 test1_2

/tmp/test1の所有者・所有グループはrootとなりました。

実行ログを見ると、1回目の実行(1回目のchanged)で/tmp/test1、/tmp/test1_1が作成され、2回目の実行(2回目のchanged)で/tmp/test1/test1_2が作成されます。所有者やパーミッションディレクトリの作成時に設定されるようです。2回目の実行時は/tmp/test1は既に存在するため作成されず、その結果、/tmp/test1の所有者・所有グループはrootとなったようです。