LoginSignup
0
0

More than 1 year has passed since last update.

【ルール説明・safety】latest・package-latest

Last updated at Posted at 2022-12-16

こちらの記事はAnsible lint Advent Calendar 2022 16日目の記事になります。

今回はルールlatestpackage-latestについて説明します。

latest

latestansible.builtin.gitモジュールおよびcommunity.general.hgモジュールにおいてソースコードが副作用が起こらないように取得されるか検証します。

問題のあるコードその1

---
- name: Example for `latest` rule
  hosts: localhost
  tasks:
    - name: Risky use of git module
      ansible.builtin.git:
        repo: "https://foosball.example.org/path/to/repo.git"
        version: HEAD # <-- HEADが指定されているためリポジトリの更新状態によりどのバージョンのコードが取得されるのか決まっていない

修正されたコードその1

---
- name: Example for `latest` rule
  hosts: localhost
  tasks:
    - name: Safe use of git module
      ansible.builtin.git:
        repo: "https://foosball.example.org/path/to/repo.git"
        version: abcd1234... # <-- コミットのSHA・タグ・ブランチ名等を指定し一意のコミットIDが指定されるようにする

問題のあるコードその2

---
- name: Example for `latest` rule
  hosts: localhost
  tasks:
    - name: Risky use of git module
      ansible.builtin.git:
        repo: "https://foosball.example.org/path/to/repo.git"
        version: main # <- mainブランチが指定されている

修正されたコードその2

---
- name: Example for `latest` rule
  hosts: localhost
  tasks:
    - name: Safe use of git module
      ansible.builtin.git:
        repo: "https://foosball.example.org/path/to/repo.git"
        version: main # noqa: latest <- mainブランチが常に安全であると確信がある場合はAnsible lintの検証をスキップする

package-latest

package-latestはOSのパッケージマネージャーが安全に副作用が起きないようにソフトウェアをインストールできるか検証します。

Ansibleではパッケージを新規インストールする場合はバージョンを指定するすでにインストールされたパッケージをアップデートする場合は余計なソフトウェアをインストールしないように記述するのが望ましいとのことです。

問題のあるコードその1

---
- name: Example playbook
  hosts: localhost
  tasks:
    - name: Install Ansible
      ansible.builtin.yum:
        name: ansible
        state: latest # <- 最新版のパッケージをインストールする

修正されたコードその1

---
- name: Example playbook
  hosts: localhost
  tasks:
    - name: Install Ansible
      ansible.builtin.yum:
        name: ansible-2.12.7.0 # <- バージョンを指定する
        state: present

問題のあるコードその2

---
- name: Example playbook
  hosts: localhost
  tasks:
    - name: Install Ansible-lint
      ansible.builtin.pip:
        name: ansible-lint
      args:
        state: latest # <- 最新版のパッケージを新規インストールする

修正されたコードその2

---
- name: Example playbook
  hosts: localhost
  tasks:
    - name: Install Ansible-lint
      ansible.builtin.pip:
        name: ansible-lint
      args:
        state: present
        version: 5.4.0 # <- バージョンを指定する

問題のあるコードその3

---
- name: Example playbook
  hosts: localhost
  tasks:
    - name: Install some-package
      ansible.builtin.package:
        name: some-package
        state: latest # <- 最新版のパッケージをインストールする

修正されたコードその3

---
- name: Example playbook
  hosts: localhost
  tasks:
    - name: Install some-package
      ansible.builtin.package:
        name: some-package
        state: present # <- presentを指定する

問題のあるコードその4

---
- name: Example playbook
  hosts: localhost
  tasks:
    - name: Install Ansible with update_only to false
      ansible.builtin.yum:
        name: sudo
        state: latest
        update_only: false # <- パッケージを最新版にアップデートする

修正されたコードその4

---
- name: Example playbook
  hosts: localhost
  tasks:
    - name: Update Ansible with update_only to true
      ansible.builtin.yum:
        name: sudo
        state: latest
        update_only: true # <- アップデートの実行はするが他のソフトウェアはインストールしない

参考サイト

0
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
0
0