LoginSignup
78
69

More than 5 years have passed since last update.

Ansible statモジュールで取得できる内容

Posted at

Ansible statモジュールで取得できる内容

Ansibleのstatモジュールではドキュメント

Retrieves facts for a file similar to the linux/unix ‘stat’ command

と記載されていますが、具体的な値は記載されていません(2015-02-17現在)。
(issueには上がっているようです。stat module documentation · Issue #107)

ソースを見れば一発ですが、分かりやすいように一覧を作成しました。

尚、ansibleのv1.8.2を対象にしています。

statのプロパティ一覧

property type description
exists bool 存在すれば True / 存在しない場合は False (存在しない場合以下の全プロパティはundefined)
mode string "0777" 形式のアクセス権
isdir bool ディレクトリなら True
ischr bool キャラクタ型の特殊デバイスファイルの場合 True
isblk bool ブロック型の特殊デバイスファイルの場合 True
isreg bool 通常ファイルの場合 True
isfifo bool FIFO (名前つきパイプ) の場合 True
islnk bool シンボリックリンクの場合 True
issock bool ソケットの場合 True
uid number オーナーの uid
gid number オーナーの group id
size number ファイルのサイズ(単位: byte)
inode number inode 番号
dev number デバイス
nlink number ハードリンク数
atime number 最近にアクセスされた時間(Linuxの例: 1424174164.9125264)
mtime number 最近に内容を変更した時間(Linuxの例: 1424174164.9125264)
ctime number 最近のメタデータ変更時間(Linuxの例: 1424174164.9125264)
wusr bool ユーザーのwrite権があれば True
rusr bool ユーザーのread権があれば True
xusr bool ユーザーのexecute権があれば True
wgrp bool グループのwrite権があれば True
rgrp bool グループのread権があれば True
xgrp bool グループのexecute権があれば True
woth bool 他ユーザーのwrite権があれば True
roth bool 他ユーザーのread権があれば True
xoth bool 他ユーザーのexecute権があれば True
isuid bool セットUIDビットが立っていれば True
isgid bool セットgroup IDビットが立っていれば True
lnk_source string リンクの場合はリンク元の絶対パス、リンクでない場合はundefined
md5 string 通常ファイルでread権があればファイルのmd5
checksum string 通常ファイルでread権があればファイルのchecksum(sha1)
pw_name string ユーザー名

使用例

stat-test.yaml
---
- hosts: localhost
  tasks:
    - stat: path=test.txt
      register: f
    - debug: msg="File exists."
      when: f.stat.exists # ファイルが存在すれば実行
    - debug: var=f.stat # デバッグ用
    - stat: path=test_not_found.txt
      register: f
    - debug: msg="File not exists."
      when: not f.stat.exists # ファイルが存在しなければ実行
    - debug: var=f.stat # デバッグ用

ファイルが存在するかどうかはこのような感じで調べられます。
このplaybookとインベントリ

hosts
localhost

を使って、test.txtを用意して実行するとこんな感じになります。

$ ansible-playbook -i hosts stat-test.yaml

PLAY [localhost] **************************************************************

GATHERING FACTS ***************************************************************
ok: [localhost]

TASK: [stat path=test.txt] ****************************************************
ok: [localhost]

TASK: [debug msg="File exists."] **********************************************
ok: [localhost] => {
        "msg": "File exists."
}

TASK: [debug var=f.stat] ******************************************************
ok: [localhost] => {
        "f.stat": {
                "atime": 1424177381.4317896,
                "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
                "ctime": 1424177378.7864673,
                "dev": 2049,
                "exists": true,
                "gid": 1000,
                "inode": 919511,
                "isblk": false,
                "ischr": false,
                "isdir": false,
                "isfifo": false,
                "isgid": false,
                "islnk": false,
                "isreg": true,
                "issock": false,
                "isuid": false,
                "md5": "d41d8cd98f00b204e9800998ecf8427e",
                "mode": "0664",
                "mtime": 1424177378.7864673,
                "nlink": 1,
                "pw_name": "user",
                "rgrp": true,
                "roth": true,
                "rusr": true,
                "size": 0,
                "uid": 1000,
                "wgrp": true,
                "woth": false,
                "wusr": true,
                "xgrp": false,
                "xoth": false,
                "xusr": false
        }
}

TASK: [stat path=test_not_found.txt] ******************************************
ok: [localhost]

TASK: [debug msg="File not exists."] ******************************************
ok: [localhost] => {
        "msg": "File not exists."
}

TASK: [debug var=f.stat] ******************************************************
ok: [localhost] => {
        "f.stat": {
                "exists": false
        }
}

PLAY RECAP ********************************************************************
localhost                  : ok=7    changed=0    unreachable=0    failed=0

参考

statのソース
https://github.com/ansible/ansible-modules-core/blob/devel/files/stat.py

Python のドキュメント
http://docs.python.jp/2/library/os.html#os.stat
http://docs.python.jp/2/library/stat.html

78
69
1

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
78
69