Ansibleでdnfを使ってパッケージのインストールするには、dnfモジュールを使います。
【Ansible】yumモジュールの説明の記事も書いたのでdnf版を書きました。基本的な部分はyum版とほとんど変わりませんが、dnfコマンド特有の部分やRHEL 8系から導入されたModularityに対応したところが異なります。
よく使いそうなパラメータ
よく使いそうなパラメータのみ紹介します。このパッケージを使うのは、パッケージのインストールがメインだと思いますので、ここで記載したものでほとんどの場合は十分でしょう。
- disable_gpg_check
-
インストール時にGPGシグネチャをチェックするかどうかを指定する。
「no(デフォルト)」か「yes」を指定する。
- disablerepo
-
インストール/アップデートするときに無効化するレポジトリの「repo id」を指定する。
複数指定する場合は、カンマ(,)で区切る。
- name(必須)
-
パッケージ名(name-1.0のようにバージョンがついたものでも良い)。「state=latest」が指定されているときに「*」を使うと、「dnf -y update」の実行と同じになる。
カンマ(,)区切りで複数のパッケージを指定することもできる。
- state
-
パッケージをインストール時に「present」「latest」、削除時に「absent」のいずれかを指定する。
公式ドキュメントのサンプル
このパッケージを使い方は、公式ドキュメントのサンプルの通りにすれば良いでしょう。主だったサンプルを見てみましょう。
- name: install the latest version of Apache
dnf:
name: httpd
state: latest
- name: install the latest version of Apache and MariaDB
dnf:
name:
- httpd
- mariadb-server
state: latest
- name: remove the Apache package
dnf:
name: httpd
state: absent
- name: install the latest version of Apache from the testing repo
dnf:
name: httpd
enablerepo: testing
state: present
パッケージのインストールと削除です。
- name: upgrade all packages
dnf:
name: "*"
state: latest
「dnf -y update」と同様な効果があるAnsibleでの書き方です。
- name: install the nginx rpm from a remote repo
dnf:
name: 'http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm'
state: present
- name: install nginx rpm from a local file
dnf:
name: /usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm
state: present
リポジトリ以外からのインストール例。
- name: install the 'Development tools' package group
dnf:
name: '@Development tools'
state: present
パッケージグループでのインストールです。
- name: Uninstall httpd but keep its dependencies
dnf:
name: httpd
state: absent
autoremove: no
依存関係は削除しないでhttpdだけ削除する例です。
- name: Install package with multiple repos enabled
yum:
name: sos
enablerepo: "epel,ol7_latest"
- name: Install package with multiple repos disabled
yum:
name: sos
disablerepo: "epel,ol7_latest"
複数のリポジトリの有効化・無効化の例です。
- name: install a modularity appstream with defined stream and profile
dnf:
name: '@postgresql:9.6/client'
state: present
- name: install a modularity appstream with defined stream
dnf:
name: '@postgresql:9.6'
state: present
- name: install a modularity appstream with defined profile
dnf:
name: '@postgresql/client'
state: present
RHEL8系から新たに導入されたModularityに対応したパッケージのインストール方法ですね。
まとめ
yumモジュール同様、dnfモジュールの使い方に難しいことはありませんでした。