
lineinfileを使用して条件が満たされた場合に端末に出力を印刷する方法
- lineinfile:
path: /home/pc/date.txt
state: present
line: 'yes'
regexp: '^\s*Wednesday\s*$'
check_mode: yes
register: wednesdayOccur
- debug: msg="{{ wednesdayOccur.stdout }}" #Show on terminal "wednesday is found"
when: wednesdayOccur == 1
答え1
与えられたファイル
# test.yml
- hosts: localhost
tasks:
- lineinfile:
path: /tmp/blah
state: present
line: 'yes'
regexp: '^\s*Wednesday\s*$'
check_mode: yes
register: wednesdayOccur
- debug: msg="wednesday is found"
when: wednesdayOccur is defined
とりわけ、これは以下を生成します。
$ printf Wednesday'\n' > /tmp/blah
$ ANSIBLE_NOCOLOR=1 ansible-playbook test.yml
...
TASK [debug] *******************************************************************
ok: [localhost] => {
"msg": "wednesday is found"
}
...
これがAnsibleがデバッグ出力をレンダリングする方法です。
変数に何かが必要な場合は、各モジュールが独自の操作を実行するため、各モジュールの詳細なマニュアルを参照する必要があります。ファイル行または、変数を記録して、指定された条件でdebug
変数に含まれる内容を確認します。
# test2.yml
- hosts: localhost
tasks:
- lineinfile:
path: /tmp/blah
state: present
line: 'yes'
regexp: '^\s*Wednesday\s*$'
check_mode: yes
register: wednesdayOccur
- debug: msg="wednesdayOccur"
when: wednesdayOccur is defined
とりわけ、これは以下を生成します。
TASK [debug] *******************************************************************
ok: [localhost] => {
"msg": {
"backup": "",
"changed": true,
"diff": [
{
"after": "",
"after_header": "/tmp/blah (content)",
"before": "",
"before_header": "/tmp/blah (content)"
},
{
"after_header": "/tmp/blah (file attributes)",
"before_header": "/tmp/blah (file attributes)"
}
],
"failed": false,
"msg": "line replaced"
}
}
それからそれをどのように活用するかを考えなければなりません。