ものづくりのブログ

うちのネコを題材にしたものづくりができたらいいなと思っていろいろ奮闘してます。

Ansible で apt コマンドを実行する方法

ansible-playbook を実行したことろ下記の警告が出たので、その時の対応(ymlの書き方)をここにメモします。

[WARNING]: Consider using 'become', 'become_method', and 'become_user' rather than running sudo

このメッセージは、Ansible で sudoコマンドを直接利用するのは非推奨になってしまったから...とのこと...


その場合は対応方法は become ディレクティブを有功にすると、デフォルトでは root ユーザーに切り替わるようです。
なので、"apt update" するなら以下のような書き方もできるようです。

アップデート

- hosts: all
  gather_facts: False
  become: yes
  
  - name: "apt update"
      shell: "apt update"

以下のような書き方もできるっぽいです。

- hosts: all
  gather_facts: False
  become: yes
  
  - name: "apt update"
    apt:
      update_cache: yes

インストール

apt モジュールをインストールしたい場合は以下のように書けるようです。

- hosts: all
  gather_facts: False
  become: yes
  
  - name: "Install multi packages"
    shell: "apt install gimp"

または...

- hosts: all
  gather_facts: False
  become: yes
  
  - name: "Install multi packages"
    apt:
      name:
        - gimp

アンインストール

ちなみにアンインストールするなら...

- hosts: all
  gather_facts: False
  become: yes
  
  - name: "Install multi packages"
    apt:
      name:
        - gimp
      state: absent
      purge: yes
      autoremove: yes