GNU/Linux >> Linux の 問題 >  >> Ubuntu

Ansibleを使用してDebian/UbuntuLinuxを更新/アップグレードする方法

同様に、OSの再起動が必要なカーネルの更新がある場合は、reboot ansibleモジュールを使用してマシンを再起動し、マシンがダウンするのを待ってから起動し、コマンドに応答するのが最適です。

このガイドでは、Debianベースのシステムキャッシュを更新し、インストールされているパッケージをアップグレードするためのスクリプトを作成します。再起動が必要なカーネルアップデートがあった場合にサーバーを再起動するタスクも含まれます。

また、チェックしてください:

  • SSH –sshキーの生成と操作

目次

  1. Ansibleaptモジュールを使用してすべてのパッケージを更新する
  2. カーネルのアップグレードがある場合はシステムを再起動します
  3. hostsファイルの作成
  4. プレイブック全体
  5. プレイブックの実行

1。 Ansibleaptモジュールを使用してすべてのパッケージを更新する

Debianベースのシステムでパッケージのアップグレードを行う前に、aptキャッシュの更新を実行することを常にお勧めします。これは、次のコマンドを使用して実行できます:

sudo apt-get update

このタスクでAnsibleを使用して同じことを達成できます:

- name: Update apt repo and cache on all Debian/Ubuntu boxes
  apt:
    update_cache: yes
    force_apt_get: yes
    cache_valid_time: 3600

どこで、

  1. update_cache:はい apt-get updateと同等の機能を実行します すべてのサーバーでのコマンド
  2. force_apt_get:はい – aptitudeコマンドを使用せず、代わりにDebian/Ubuntuボックスでapt-getコマンドを使用します
  3. cache_valid_time:3600 cache_valid_timeより古い場合は、aptキャッシュを更新します 。このオプションは秒単位で設定されます。この例では、3600秒に設定されています。

次に、アップグレードを行います。通常、このapt-getコマンドを実行して、次の機能を実行します。

sudo apt-get upgrade -y

これは、アップグレードを実現するためのAnsibleタスクです:

- name: Upgrade all packages on servers
  apt:
    upgrade: dist
    force_apt_get: yes

どこで、

  1. アップグレード:dist apt-get upgradeと同等の機能を実行します すべてのUbuntuまたはDebianLinuxサーバーでのコマンド。つまり、すべてのパッケージを最新バージョンにアップグレードします。
  2. force_apt_get:はい –aptitudeの代わりにapt-getを使用します。

2。カーネルのアップグレードがある場合のシステムの再起動

カーネルのアップグレードがある場合は、それらの変更を適用するためにシステムを再起動する必要があります。再起動が必要な場合は、このパスが/var/run/reboot-requiredのファイル 作成されます。

そのファイルが存在するかどうかを確認してから、システムを再起動します。 ansible statを使用して、ファイル/ var / run / restart-requiredがシステムに存在する場合、新しい変数を登録できます:

- name: Check if a reboot is needed on all servers
  register: reboot_required_file
  stat:
    path: /var/run/reboot-required
    get_md5: no

場所:

  1. 登録:reboot_required_file register キーワードは結果を保存する変数を決定し、次のように使用してボックスを再起動します。
  2. 統計:パス:/ var / run / restart-required –パス(/ var / run / restart-required)が存在するかどうかを確認します
  3. get_md5:いいえ –ファイルのチェックサムを決定するアルゴリズム。この例では、md5を使用していますが、sha1、sha224、sha256、sha384、およびsha512を使用できます。

サーバーを再起動するかどうかがわかったので、ファイルが存在する場合にサーバーを再起動するタスクを追加しましょう。

- name: Reboot the server if kernel updated
  reboot:
    msg: "Reboot initiated by Ansible for kernel updates"
    connect_timeout: 5
    reboot_timeout: 300
    pre_reboot_delay: 0
    post_reboot_delay: 30
    test_command: uptime
  when: reboot_required_file.stat.exists

どこで、

  1. test_command:稼働時間 –再起動したサーバーでuptimeコマンドを実行し、成功を期待して、マシンが次のタスクの準備ができていることを確認します。
  2. いつ:reboot_required_file.stat.exists –まず、reboot_required_fileという名前の変数を使用して、/ var / run/reboot-requiredという名前のファイルが存在することを確認します。再起動モジュールは、そのファイルが存在し、「when:reboot_required_file.stat.exists」Ansible条件を使用して強制される場合にのみ機能します。

3。ホストファイルの作成

ロジックが整ったので、hosts.yamlファイルを作成しましょう。このコンテンツを hosts.yamlというファイルに保存します 。

all:
  hosts:
    ubuntusrv:
      ansible_ssh_host: 10.2.11.10
      ansible_ssh_user: ubuntu
    debiansrv:
      ansible_ssh_host: 10.2.11.11
      ansible_ssh_user: admin
  children:
    allservers:
      hosts:
        ubuntusrv:
        debiansrv:

4。プレイブック全体

ロジック全体をプレイブックに入れることができます。このコンテンツをファイルupgrade.yamlに保存します

---
- name: Playbook to Update cache, upgrade packages and reboot os if necessary
  hosts: allservers
  become: yes
  gather_facts: False
  tasks:
    - name: Update apt repo and cache on all Debian/Ubuntu boxes
      apt:
        update_cache: yes
        force_apt_get: yes
        cache_valid_time: 3600

    - name: Upgrade all packages on servers
      apt:
        upgrade: dist
        force_apt_get: yes

    - name: Check if a reboot is needed on all servers
      register: reboot_required_file
      stat:
        path: /var/run/reboot-required
        get_md5: no

    - name: Reboot the server if kernel updated
      reboot:
        msg: "Reboot initiated by Ansible for kernel updates"
        connect_timeout: 5
        reboot_timeout: 300
        pre_reboot_delay: 0
        post_reboot_delay: 30
        test_command: uptime
      when: reboot_required_file.stat.exists

5。プレイブックの実行

必ずsshキーを設定し、次のように実行してください。

ansible-playbook -i hosts.yaml upgrade.yaml -vv

これは私のサーバーの出力です

➜ ansible-playbook -i hosts.yaml upgrade.yaml -vv
ansible-playbook [core 2.12.1]
  config file = /Users/etowett/.ansible.cfg
  configured module search path = ['/Users/etowett/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Library/Python/3.8/site-packages/ansible
  ansible collection location = /Users/etowett/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible-playbook
  python version = 3.8.2 (default, Apr  8 2021, 23:19:18) [Clang 12.0.5 (clang-1205.0.22.9)]
  jinja version = 3.0.3
  libyaml = True
Using /Users/etowett/.ansible.cfg as config file
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
Skipping callback 'default', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.

PLAYBOOK: upgrade.yaml ************************************************************************************************************************************************************
1 plays in upgrade.yaml

PLAY [Playbook to Update cache, upgrade packages and reboot os if necessary] ******************************************************************************************************
META: ran handlers

TASK [Update apt repo and cache on all Debian/Ubuntu boxes] ***********************************************************************************************************************
task path: /Users/etowett/Proj/me/infme/ansible/upgrade.yaml:7
changed: [ubuntusrv] => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"}, "cache_update_time": 1639737368, "cache_updated": true, "changed": true}

TASK [Upgrade all packages on servers] ********************************************************************************************************************************************
task path: /Users/etowett/Proj/me/infme/ansible/upgrade.yaml:13
ok: [ubuntusrv] => {"changed": false, "msg": "Reading package lists...\nBuilding dependency tree...\nReading state information...\nCalculating upgrade...\n0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.\n", "stderr": "", "stderr_lines": [], "stdout": "Reading package lists...\nBuilding dependency tree...\nReading state information...\nCalculating upgrade...\n0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.\n", "stdout_lines": ["Reading package lists...", "Building dependency tree...", "Reading state information...", "Calculating upgrade...", "0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded."]}

TASK [Check if a reboot is needed on all servers] *********************************************************************************************************************************
task path: /Users/etowett/Proj/me/infme/ansible/upgrade.yaml:18
ok: [ubuntusrv] => {"changed": false, "stat": {"atime": 1639737351.6237016, "attr_flags": "", "attributes": [], "block_size": 4096, "blocks": 8, "charset": "us-ascii", "checksum": "20f7959b87e8cd55b7c985e46d6fa38a4063037d", "ctime": 1639679787.0725238, "dev": 26, "device_type": 0, "executable": false, "exists": true, "gid": 0, "gr_name": "root", "inode": 1143, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "mimetype": "text/x-diff", "mode": "0644", "mtime": 1639679787.0725238, "nlink": 1, "path": "/var/run/reboot-required", "pw_name": "root", "readable": true, "rgrp": true, "roth": true, "rusr": true, "size": 32, "uid": 0, "version": null, "wgrp": false, "woth": false, "writeable": true, "wusr": true, "xgrp": false, "xoth": false, "xusr": false}}

TASK [Reboot the server if kernel updated] ****************************************************************************************************************************************
task path: /Users/etowett/Proj/me/infme/ansible/upgrade.yaml:24
changed: [ubuntusrv] => {"changed": true, "elapsed": 73, "rebooted": true}
META: ran handlers
META: ran handlers

PLAY RECAP ************************************************************************************************************************************************************************
ubuntusrv                  : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

まとめ

DebianおよびUbuntuLinuxボックスのすべてのパッケージを更新し、必要に応じてAnsibleプレイブックを使用してサーバーを再起動する方法を学びました。


Ubuntu
  1. Debian8JessieをDebianLinux9Stretchにアップグレードする方法

  2. UbuntuでLinuxカーネルを更新する方法

  3. DebianLinuxをアップデートする方法

  1. Ubuntu20.04にアップグレードする方法

  2. KaliLinuxをアップデートする方法

  3. Ubuntuを20.10にアップグレードする方法

  1. Ubuntuを21.10にアップグレードする方法

  2. Ubuntuを21.04にアップグレードする方法

  3. Ubuntu21.10にアップグレードする方法