LoginSignup
0
0

More than 1 year has passed since last update.

【ルール説明・shared】no-relative-paths

Last updated at Posted at 2022-12-23

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

今回はルール no-relative-paths について説明します。

no-relative-paths

no-relative-pathsansible.builtin.copy モジュールおよび ansible.builtin.template モジュールにおいてsrcキーの要素に相対パスが含まれていないか検証します。

ansible.builtin.copy モジュールおよびansible.builtin.template モジュールでファイル操作を行う場合はファイルのパスの指定方法が決まっています。ansible.builtin.copy モジュールを利用してファイルをコピーする場合、ファイルはプロジェクトのfiles/ディレクトリ以下に置かれている事が想定されます。files/ディレクトリ以下以外のパスへファイルを置く場合、パスは絶対パスで指定します。また ansible.builtin.template モジュールを利用してファイル操作を行う場合はプロジェクトのtemplates/ディレクトリ以下にテンプレートファイルが置かれている事が想定されます。templates/ディレクトリ以下以外のパスへファイルを置く場合はパスは絶対パスで指定します。

問題のあるコードその1

srcが相対パスで指定されている

---
- name: Example playbook
  hosts: all
  tasks:
    - name: Template a file to /etc/file.conf
      ansible.builtin.template:
        src: ../my_templates/foo.j2 # <- 相対パスになっている
        dest: /etc/file.conf
        owner: bin
        group: wheel
        mode: "0644"

修正されたコードその1

プロジェクトのtemplatesディレクトリ内のパスを指定する

---
- name: Example playbook
  hosts: all
  tasks:
    - name: Template a file to /etc/file.conf
      ansible.builtin.template:
        src: foo.j2 # <- `templates`ディレクトリ内のパスを指定する
        dest: /etc/file.conf
        owner: bin
        group: wheel
        mode: "0644"

問題のあるコードその2

- name: Example playbook
  hosts: all
  vars:
    source_path: ../../my_templates/foo.j2 # <- 相対パスが指定されている
  tasks:
    - name: Copy a file to /etc/file.conf
      ansible.builtin.copy:
        src: "{{ source_path }}" # <- 変数が代入されている部分
        dest: /etc/foo.conf
        owner: foo
        group: foo
        mode: "0644"

修正されたコードその2

- name: Example playbook
  hosts: all
  vars:
    source_path: foo.j2 # <- filesディレクトリ内でのパスを指定する
  tasks:
    - name: Copy a file to /etc/file.conf
      ansible.builtin.copy:
        src: "{{ source_path }}" # <- 変数が代入されている部分
        dest: /etc/foo.conf
        owner: foo
        group: foo
        mode: "0644"

参考サイト

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