0
0

More than 1 year has passed since last update.

【ルール説明・safety】avoid-implicit

Last updated at Posted at 2022-12-28

こちらの記事は Ansible lint Advent Calendar 2022 カレンダー2 14日目の記事になります。

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

avoid-implicit

avoid-implicitansible.builtin.copy においてコピー元のデータの型が文字列で渡された時に形式が不明ならエラーを出力します。ansible.builtin.copy ではコピー元のデータはsrcからデータのパスを指定します。また文字列をテキストファイルへコピーする(書き込む)場合は ansible.builtin.copy ではなく可能な限り ansible.builtin.template を利用します。

Parameter Comments
content

string

added in Ansible 1.1
When used instead of src, sets the contents of a file directly to the specified value.

Works only when dest is a file. Creates the file if it does not exist.

For advanced formatting or if content contains a variable, use the ansible.builtin.template module.

もし ansible.builtin.copy モジュールを利用して文字列をコピーする(書き込む)場合はデータの形式をできるだけ明示するようにします。

問題のあるコード

---
- name: Example playbook
  hosts: localhost
  tasks:
    - name: Write file content
      ansible.builtin.copy:
        content: { "foo": "bar" } # <- データの形式が不明瞭
        dest: /tmp/foo.txt

修正されたコード

---
- name: Example playbook
  hosts: localhost
  tasks:
    - name: Write file content
      vars:
        content: { "foo": "bar" }
      ansible.builtin.copy:
        content: "{{ content | to_json }}" # <- jinja2 フィルターを用いて JSON 形式へ変換する
        dest: /tmp/foo.txt

その他

ドキュメントにもありますが ansible.builtin.copy で文字列のコピー処理(もしくは書き込み処理)は可能ですが機能が少なかったり正式な方法ではないため ansible.builtin.template を利用しましょう。

参考サイト

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