ab’s blog

インフラの呟きです。

ansible 時刻を変数にセットする

hahahahahaha

はじめに

時刻をセットするときの方法です。その1は変数呼び出されるたびに時刻を取得するのでプレイブック内で2回呼び出すと1回目と2回めで時刻が変わります。その2は一度取得した時刻は変わらないです。同じ時刻を複数のファイルに付与したいというときは、その2が有効です。

■その1

---
- hosts: all
  vars:
          datetime: "{{ lookup('pipe','date +%Y%m%d_%H%M%S') }}"
  tasks:
          - debug:
                  msg: "{{ datetime }}"

■その2(おすすめ)

---
- hosts: all
  tasks:
  - name: 時刻セット
    set_fact:
      datetime: "{{ lookup('pipe','date +%Y%m%d_%H%M%S') }}"
  - debug:
       msg: "{{ datetime }}"

以上