Ansible(2)ファイル関連モジュール

Ansible

blockinfile

ファイル内にテキストブロックを追加/削除する。

変数 デフォルト 内容 備考
path 必須 ファイルパス  
block   挿入するテキストブロック  
insertafter EOF 何の後にテキストブロックを追加するか  
insertbefoer   何の前にテキストブロックを追加するか  
marker # {mark} ANSIBLE MANAGED BLOCK テキストブロック前後に挿入されるマーカー  
backup no バックアップファイルを作成するかどうか  
owner   所有者  
group   グループ  
mode   ファイルパーミッション  
state present 実行内容 absent, present

Example

- name: Add mappings to /etc/hosts
  blockinfile:
    path: /etc/hosts
    block: |
      {{ item.ip }} {{ item.name }}
    marker: "# {mark} ANSIBLE MANAGED BLOCK {{ item.name }}"
  with_items:
    - { name: host1, ip: 10.10.1.10 }
    - { name: host2, ip: 10.10.1.11 }
    - { name: host3, ip: 10.10.1.12 }

copy

リモートサーバへファイルをコピーする。

変数 デフォルト 内容 備考
src   コピー元のファイルパス  
remote_src no コピー元のファイルパスがリモートサーバのパスであるかどうか  
backup no バックアップファイルを作成するかどうか  
owner   所有者  
group   グループ  
mode   ファイルパーミッション  

Example

- name: example copying file with owner and permissions
  copy:
    src: /srv/myfiles/foo.conf
    dest: /etc/foo.conf
    owner: foo
    group: foo
    mode: 0644

file

ファイル属性を変更する。

変数 デフォルト 内容 備考
path   コピー元のファイルパス  
owner   所有者  
group   グループ  
mode   ファイルパーミッション  
state file ファイルの状態 absent, directory, file, hard, link, touch

Example

# change file ownership, group and mode
- file:
    path: /etc/foo.conf
    owner: foo
    group: foo
    # when specifying mode using octal numbers, add a leading 0
    mode: 0644

get_url

ファイルのダウンロードを行う。

変数 デフォルト 内容 備考
url 必須 ファイルのURL  
dest 必須 ダウンロードしたファイルを設置するディレクトリの絶対パス  
timeout 10 タイムアウト  
force no 既にファイルが存在していたとしても毎回ダウンロードを行うかどうか  
insertbefoer   何の前にテキストブロックを追加するか  
backup no バックアップファイルを作成するかどうか  
owner   所有者  
group   グループ  
mode   ファイルパーミッション  

Example

- name: Download foo.conf
  get_url:
    url: https://example.com/path/file.conf
    dest: /etc/foo.conf
    mode: 0440

git

gitプロジェクトをデプロイする。

変数 デフォルト 内容 備考
dest 必須 gitプロジェクトのチェックアウト先  
repo 必須 gitリポジトリ  

Example

# Example git checkout from Ansible Playbooks
- git:
    repo: 'https://foosball.example.org/path/to/repo.git'
    dest: /srv/checkout
    version: release-0.22

lineinfile

ファイル内にテキスト行を追加/削除する。

変数 デフォルト 内容 備考
path 必須 ファイルパス  
line   挿入するテキスト  
insertafter EOF 何の後にテキストブロックを追加するか  
insertbefoer   何の前にテキストブロックを追加するか  
backup no バックアップファイルを作成するかどうか  
owner   所有者  
group   グループ  
mode   ファイルパーミッション  
state present 実行内容 absent, present

Example

# Before 2.3, option 'dest', 'destfile' or 'name' was used instead of 'path'
- lineinfile:
    path: /etc/selinux/config
    regexp: '^SELINUX='
    line: 'SELINUX=enforcing'

replace

特定の文字列を置換する。

変数 デフォルト 内容 備考
path 必須 ファイル名  
regexp 必須 対象文字列の正規表現  
replace   変換後の文字列  
backup no バックアップファイルを作成するかどうか  
owner   所有者  
group   グループ  
mode   ファイルパーミッション  

Example

- replace:
    path: /etc/hosts
    regexp: '(\s+)old\.host\.name(\s+.*)?$'
    replace: '\1new.host.name\2'
    backup: yes

template

テンプレートファイルをリモートサーバに設置する。

変数 デフォルト 内容 備考
src 必須 Playbook上のテンプレートファイルの名前  
dest 必須 リモートサーバ上のパス  
backup no バックアップファイルを作成するかどうか  
owner   所有者  
group   グループ  
mode   ファイルパーミッション  

Example

- template:
    src: /mytemplates/foo.j2
    dest: /etc/file.conf
    owner: bin
    group: wheel
    mode: 0644

unarchive

アーカイブを展開する。

変数 デフォルト 内容 備考
src 必須 展開するアーカイブのパス  
remote_src no 展開するアーカイブがリモートサーバにあるかどうか  
dest 必須 リモートサーバ上のパス  
owner   所有者  
group   グループ  
mode   ファイルパーミッション  

Example

- name: Unarchive a file that is already on the remote machine
  unarchive:
    src: /tmp/foo.zip
    dest: /usr/local/bin
    remote_src: yes