web-dev-qa-db-ja.com

ansible変数を複数のタスクに登録する

さまざまなサーバーに更新プログラムをインストールするansibleスクリプトで次のタスクを使用しています。これらのタスクはCentOSマシン用です。

- name: Check for outstanding reboot
  Shell: needs-restarting > /dev/null || echo Reboot required
  when: ansible_distribution_major_version|int < 7
  register: result
- name: Check for outstanding reboot
  Shell: needs-restarting -r > /dev/null || echo Reboot required
  when: ansible_distribution_major_version|int >= 7
  register: result
- name: Report reboot
  debug: msg="{{ result.stdout_lines }}"

結果:

TASK [Check for outstanding reboot] ***********************************************************************************
skipping: [Host1]
skipping: [Host2]
skipping: [Host5]
changed: [Host3]
changed: [Host4]

TASK [Check for outstanding reboot] ***********************************************************************************
skipping: [Host3]
skipping: [Host4]
changed: [Host2]
changed: [Host1]
changed: [Host5]

TASK [Report reboot] **************************************************************************************************
fatal: [Host3]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout_lines'\n\nThe error appears to have been in '/path/to/updates.yml': line 52, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n    register: result\n  - name: Report reboot\n    ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'dict object' has no attribute 'stdout_lines'"}
ok: [Host1] => {
    "msg": [
        "Reboot required"
    ]
}
fatal: [Host4]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout_lines'\n\nThe error appears to have been in '/path/to/updates.yml': line 52, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n    register: result\n  - name: Report reboot\n    ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'dict object' has no attribute 'stdout_lines'"}
ok: [Host2] => {
    "msg": [
        "Reboot required"
    ]
}
ok: [Host5] => {
    "msg": [
        "Reboot required"
    ]
}
        to retry, use: --limit @/path/to/updates.retry

ご覧のとおり、両方のチェックタスクは「result」という変数に結果を保存しますが、2番目のタスクのホストの変数のみ(ansible_distribution_major_version|int >= 7)が入力され、エラーメッセージが表示されます。 2番目のチェックタスクは、前のタスクの結果を解除するようです。

両方のタスクの結果を保持することは可能ですか?または、レポートタスクをコピーして両方にバージョンチェックを追加する必要がありますか?レポートは1か所にまとめておきます。

Ansibleバージョンは2.4.4.0です

5

これは、タスクがスキップされた場合でも、Ansibleが結果を保存するために発生します。

タスクが失敗またはスキップされた場合でも、変数は失敗またはスキップされたステータスで登録されます。変数の登録を回避する唯一の方法は、タグを使用することです。

登録済み変数に関するAnsibleユーザーガイド

したがって、両方のタスクの結果を保持することはできません。

すでにおっしゃったように、レポートタスクをコピーして、両方にバージョンチェックを追加します。

3
Henrik Pingel