LoginSignup
0
0

More than 1 year has passed since last update.

【ルール説明・basic】command-instead-of-module・command-instead-of-shell

Last updated at Posted at 2022-12-10

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

今回はルールcommand-instead-of-moduleおよびcommand-instead-of-shellについて説明します。

command-instead-of-module

ansible.builtin.commandモジュールを利用する際コマンドにAnsibleモジュールで代替できるようなコマンドが含まれているか検査します。もし含まれている場合はエラーを出力します。

代替を提示されるコマンドは以下です。Ansible lintのソースコードより確認できます。

コマンド 代替モジュール
apt-get ansible.builtin.apt
chkconfig ansible.builtin.service
curl ansible.builtin.get_url / ansible.builtin.uri
git ansible.builtin.git
hg community.general.hg
letsencrypt community.crypto.acme_certificate
mktemp ansible.builtin.tempfile
mount ansible.posix.mount
patch ansible.posix.patch
rpm ansible.builtin.yum / ansible.builtin.rpm_key
rsync ansible.posix.synchronize
sed ansible.builtin.template / ansible.builtin.replace / ansible.builtin.lineinfile
service ansible.builtin.service
supervisorctl community.general.supervisorctl
svn ansible.builtin.subversion
systemctl ansible.builtin.systemd
tar ansible.builtin.unarchive
unzip ansible.builtin.unarchive
wget ansible.builtin.get_url / ansible.builtin.uri
yum ansible.builtin.yum

問題のあるコード

---
- name: Update apt cache
  hosts: all
  tasks:
    - name: Run apt-get update
      ansible.builtin.command: apt-get update # <-- better to use ansible.builtin.apt module

修正されたコード

---
- name: Update apt cache
  hosts: all
  tasks:
    - name: Run apt-get update
      ansible.builtin.apt:
        update_cache: true

command-instead-of-shell

ansible.builtin.shellモジュールではなくansible.builtin.commandモジュールを利用するように警告を出力します。ansible.builtin.shellモジュールはansible.builtin.commandモジュールに比べて動作が遅く特殊な使い方(環境変数を設定したりパイプを利用する等)をしない限りはansible.builtin.commandモジュールを利用するのが良いとされています。

問題のあるコード

---
- name: Problematic example
  hosts: localhost
  tasks:
    - name: Echo a message
      ansible.builtin.shell: echo hello # <-- command is better in this case
      changed_when: false

修正されたコード

---
- name: Correct example
  hosts: localhost
  tasks:
    - name: Echo a message
      ansible.builtin.command: echo hello
      changed_when: false

参考サイト

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