29
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ansible aptモジュール サンプル

Last updated at Posted at 2019-04-30

ページ概要

aptによるdebパッケージのインストール/アンインストールのサンプル。

specs

- 細かいオプションについて

あくまで最低限のサンプルで色々とオプションを付与できます。
それは「apt modlueのオプションが色々あるので見てみる」に書いておきました。
特に「state」オプションは知っておいた方が良いです。少しハマった。(==;

- machine specs

ansible version : 2.7.10 (pip)
OS version : ubuntu 16.04.5 ( in conoha VPS)

  • official page : apt
  • output : github
  • Usages: ansible-playbook tasks/apt/<taskfile> -i inventories/inventory

install

- install 1package
tasks/apt/install_one_package.yml
- hosts: all
  user: root
  tasks:
  - name: Install the package "libvirt-bin"
    apt:
      name: libvirt-bin
- install select multi packages

どこからのバージョンからwith_items使えなくなったらしい。

tasks/apt/install_multi_packaegs.yml
- hosts: all
  user: root
  tasks:
  - name: Install multi packages
    apt:
      name:
        - vim-gnome
        - vlc
        - gimp
- install select version

複数パッケージ&バージョン指定したい場合は、前述と組み合わせでいける

tasks/apt/install_select_version.yml
- hosts: all
  user: root
  tasks:
  - name: Install the package "libvirt-bin" selected version
    apt:
      name: libvirt-bin=1.3.1-1ubuntu10.25
- install use debfile
tasks/apt/install_use_debfile.yml
- hosts: all
  user: root
  tasks:
  - name: Install vlc use DebFile
    apt:
      deb: /var/cache/apt/archives/vlc_2.2.2-5ubuntu0.16.04.4_amd64.deb

remove(アンインストール)

- remove(単なるアンインストール)

この場合、confファイルが残る(dpkg上、「rc」扱いになる)

tasks/apt/remove_one_package.yml
- hosts: all
  user: root
  tasks:
  - name: Remove the package "libvirt-bin"
    apt:
      name: libvirt-bin
      state: absent
- purge(conf含めてアンインストール)

configも含めて削除(apt purge相当)の場合は「purge yes」を追加する
これだと指定したパッケージの依存関係パッケージは削除されない

tasks/apt/purge_one_package.yml
- hosts: all
  user: root
  tasks:
  - name: Purge the package "libvirt-bin"
    apt:
      name: libvirt-bin
      state: absent
      purge: yes
- autoremove & purge (--auto-removeオプション)

依存関係のパッケージも含めて削除する

tasks/apt/autoremove_one_package.yml
- hosts: all
  user: root
  tasks:
  - name: Autoremove the package "libvirt-bin"
    apt:
      name: libvirt-bin
      state: absent
      purge: yes
      autoremove: yes

apt Update / Upgrade

- apt update
tasks/apt/apt_update.yml
- hosts: all
  user: root
  tasks:
  - name: apt update
    apt:
      update_cache: yes
- apt upgrade

いくつかupgrade種別が有るので注意。[こっち](apt modlueのオプションが色々あるので見てみる)に書いておいた。
あと、すげー時間が掛かる...

tasks/apt/apt_upgrade.yml
- hosts: all
  user: root
  tasks:
  - name: apt upgrade
    apt:
      upgrade: yes
29
27
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
29
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?