ansibleのfact_cachingはansible_date_timeもcacheする

ansible高速化のためにansible.cfgに色々設定していましたが、fact_cachingの機能を使うと {{ ansible_date_time.date }}_{{ ansible_date_time.hour}}{{ ansible_date_time.minute }}{{ ansible_date_time.second }} のような変数に使われているdate_timeまでキャッシュされるという仕様だったので備忘録として書きます。

ansibleのバージョンは1.9.4です。v2以降の仕様については今回は調べていません。

ansible.cfgは以下のような感じ。

[defaults]
gathering = smart
fact_caching = jsonfile
fact_caching_connection = /tmp/facts_cache
fact_caching_timeout = 7200

この状態で以下の様なtest.ymlを実行して実験してみます。

- hosts: localhost
  tasks:
    - debug: var=ansible_date_time.epoch

結果は以下のようになります。

# cache有り
## 一回目
$ ansible-playbook -i /dev/null test.yml
 [WARNING]: provided hosts list is empty, only localhost is available


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

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

TASK: [debug var=ansible_date_time.epoch] *************************************
ok: [localhost] => {
    "var": {
        "ansible_date_time.epoch": "1450760636"
    }
}

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

epochが変化していないのが分かります。 ansible.cfgでfact_cachingの設定を行わない場合についても見てみます。

## 二回目
$ ansible-playbook -i /dev/null test.yml
 [WARNING]: provided hosts list is empty, only localhost is available


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

TASK: [debug var=ansible_date_time.epoch] *************************************
ok: [localhost] => {
    "var": {
        "ansible_date_time.epoch": "1450760636"
    }
}

PLAY RECAP ********************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0
# cache無し
## 一回目
$ ansible-playbook -i /dev/null test.yml
[WARNING]: provided hosts list is empty, only localhost is available


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

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

TASK: [debug var=ansible_date_time.epoch] *************************************
ok: [localhost] => {
    "var": {
        "ansible_date_time.epoch": "1450760756"
    }
}

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

## 二回目
$ ansible-playbook -i /dev/null test.yml
 [WARNING]: provided hosts list is empty, only localhost is available


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

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

TASK: [debug var=ansible_date_time.epoch] *************************************
ok: [localhost] => {
    "var": {
        "ansible_date_time.epoch": "1450760827"
    }
}

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

キャッシュ無しの場合はepochが変化していることがわかると思います。 fact_cachingを利用してキャッシュする場合はdate_timeを使わないか、十分に注意することをおすすめします。

参考

fact - Ansible date variable - Stack Overflow