2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ansibleを使用してUbuntuを最新のLTSにアップデートするには

Last updated at Posted at 2025-06-03

要点

do-release-upgradeを叩くとAnsibleが動作しなくなり進まなくなる

原因

Pythonインタプリタを指定していない場合、Ansibleが気を利かせて利用できるPythonインタプリタを直接バージョンで指定してくれます。
しかし、do-release-upgradeを実行するとPythonインタプリタも置き換えられ、場合によっては実行中に利用していたインタプリタが利用できなくなりTaskが中断してしまいます。
再度プレイブックを実行すれば新しいバージョンのインタプリタを検出して実行できますが、せっかくAnsibleで作業しているのに叩き直すのは無駄なことこの上ありません。

対策

varsansible_python_interpriterを設定し、/usr/bin/python3を指定することでアップグレードされても影響を受けずに実行を継続することが出来ます。
タスク単位で設定してもいいですし、面倒なら対象ホスト一覧で直接定義しても良いでしょう。

do-release-upgradeを無人実行するには

下記の通りのタスクを実行してください。

    - name: Update-manager-coreをインストール(do-release-upgradeに必要なため)
      ansible.builtin.apt:
        name: update-manager-core
        state: present

    - name: 新しいLTSリリースがないかを確認
      ansible.builtin.command: do-release-upgrade -c --allow-third-party
      register: release_check
      changed_when: false
      ignore_errors: true
      
    - name: 新しいLTSリリースがある場合、アップグレードを実施
      ansible.builtin.command: do-release-upgrade -f DistUpgradeViewNonInteractive
      register: upgrade_result
      async: 1800
      poll: 30
      changed_when: true
      ignore_errors: true
      when: "'There is no development version of an LTS available.' not in release_check.stdout" # 新しいリリースが見つかった場合のみ実行

    - name: 新しいLTSリリースがある場合、再起動を実施
      ansible.builtin.reboot:
        reboot_timeout: 600 # 再起動後、最大600秒待機
      when: "'There is no development version of an LTS available.' not in release_check.stdout" # 新しいリリースが見つかった場合のみ実行

do-release-upgrade -f DistUpgradeViewNonInteractiveは、アップデート中に発生するインタラクティブ操作を全てカットします。
まあまあ時間がかかるので、async/pollにしています。
Intel N150の4vCPU/4GB環境でだいたい15分くらいかかりました。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?