「Ansible lineinfile が重複行を追加する」のバリアント

okwaves2024-01-25  11

ubuntu で無人アップグレードを構成するために Ansible を取得しようとしています。残念ながら、ロールを複数回実行すると重複した行が表示されます。

これが私のコードです:

- name: apt | Configure unattended-upgrades
  lineinfile:
    dest: /etc/apt/apt.conf.d/50unattended-upgrades
    regexp: "{{ item }}"
    line: "{{ harden_linux_unattended_upgrades_settings[item] }}"
    state: present
  with_items:
      - "{{ harden_linux_unattended_upgrades_settings | list }}"

そして

harden_linux_unattended_upgrades_settings:
    "^Unattended-Upgrade::Mail": 'Unattended-Upgrade::Mail "{{ ubuntu_common_email }}";'
    "^Unattended-Upgrade::MailReport": 'Unattended-Upgrade::MailReport "on-change";' # later if working set to "only-on-error"
    "^Unattended-Upgrade::Remove-Unused-Kernel-Packages": 'Unattended-Upgrade::Remove-Unused-Kernel-Packages "true";'
    "^Unattended-Upgrade::Remove-Unused-Dependencies": 'Unattended-Upgrade::Remove-Unused-Dependencies "true";'
    "^Unattended-Upgrade::Automatic-Reboot": 'Unattended-Upgrade::Automatic-Reboot "true";'
    "^Unattended-Upgrade::Automatic-Reboot-Time": 'Unattended-Upgrade::Automatic-Reboot-Time "2:50";'

実行した結果、最初は正常に動作しました。その後、毎回この結果が得られ、(奇妙なことに一部のみ)重複した行が表示されます。

changed: [cloud-host] => (item=^Unattended-Upgrade::Mail)
changed: [cloud-host] => (item=^Unattended-Upgrade::MailReport)
ok: [cloud-host] => (item=^Unattended-Upgrade::Remove-Unused-Kernel-Packages)
ok: [cloud-host] => (item=^Unattended-Upgrade::Remove-Unused-Dependencies)
changed: [cloud-host] => (item=^Unattended-Upgrade::Automatic-Reboot)
changed: [cloud-host] => (item=^Unattended-Upgrade::Automatic-Reboot-Time)

ファイルの最後にこれを追加します:

Unattended-Upgrade::Mail "[email protected]";
Unattended-Upgrade::Mail "[email protected]";
Unattended-Upgrade::Remove-Unused-Kernel-Packages "true";
Unattended-Upgrade::Remove-Unused-Dependencies "true";
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Mail "[email protected]";
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Mail "[email protected]";
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Mail "[email protected]";
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::MailReport "on-change";
Unattended-Upgrade::Automatic-Reboot-Time "2:50";

重複行に関する問題が発生しているスレッドを多く見てきましたが、そこで問題を特定できませんでした。特に、これがなぜ機能するのかわかりません。

Unattended-Upgrade::Remove-Unused-Kernel-Packages

そして

Unattended-Upgrade::Remove-Unused-Dependencies

他の行との違いがわかりません。

編集: とても役立つ回答をありがとうございます!最終的には次のようになりました (他の部分も変更しました)。

- name: apt | Configure unattended-upgrades
  lineinfile:
    dest: /etc/apt/apt.conf.d/50unattended-upgrades
    regexp: "^{{ item }}\s"
    line: '{{ item }} "{{ harden_linux_unattended_upgrades_settings[item] }}";'
    state: present
  with_items:
      - "{{ harden_linux_unattended_upgrades_settings | list }}"

そして

harden_linux_unattended_upgrades_settings:
    "Unattended-Upgrade::Mail": "{{ ubuntu_common_email }}"
    "Unattended-Upgrade::MailReport": "on-change" # later if working set to "only-on-error"
    "Unattended-Upgrade::Remove-Unused-Kernel-Packages": "true"
    "Unattended-Upgrade::Remove-Unused-Dependencies": "true"
    "Unattended-Upgrade::Automatic-Reboot": "true"
    "Unattended-Upgrade::Automatic-Reboot-Time": "2:50"


------------------------

あなたは次のものを持っています:

    "^Unattended-Upgrade::Automatic-Reboot": 'Unattended-Upgrade::Automatic-Reboot "true";'
    "^Unattended-Upgrade::Automatic-Reboot-Time": 'Unattended-Upgrade::Automatic-Reboot-Time "2:50";'

最初の正規表現 ^Unattended-Upgrade::Automatic-Reboot は、Unattended-Upgrade::Automatic-Reboot と Unattended-Upgrade::Automatic-Reboot-Time の両方に一致します。

2 回目には次の処理が行われます。

Unattended-Upgrade::Automatic-Reboot に一致し、何も行わない Unattended-Upgrade::Automatic-Reboot-Time と一致し、Unattended-Upgrade::Automatic-Reboot に置き換えます。 Unattended-Upgrade::Automatic-Reboot-Time はもうないので、再度追加します。

これで、自動再起動が 2 つと自動再起動時間が 1 つになりました。

Unattended-Upgrade::Mai の同じロジックl および無人アップグレード::メールレポート

次の " と一致させることで正規表現を改善できます。

総合生活情報サイト - OKWAVES
総合生活情報サイト - OKWAVES
生活総合情報サイトokwaves(オールアバウト)。その道のプロ(専門家)が、日常生活をより豊かに快適にするノウハウから業界の最新動向、読み物コラムまで、多彩なコンテンツを発信。