2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ansibleで対象ディレクトリ配下のファイルを一括操作するメモ

Last updated at Posted at 2019-03-29

はじめに

この記事ではAnsibleを使って対象ディレクトリ配下の複数ファイルをまとめて操作する方法を紹介します。

実行環境

Ansible 2.7
クライアント側OS:Linux

概要

  1. findコマンドで対象ディレクトリ配下のファイル名一覧を取得。
  2. Ansibleのwith_linesに一覧を渡してファイル毎に操作。

サンプル

改行コードをCRLFからLFに一括変換してみます。

・クライアント側ディレクトリ構成

/home/user/sample
             |---aaa
             |    |---a.txt
             |    |---bbb
             |         |---b.txt
             |---ccc
                  |---c.txt

・Ansibleのロール

main.yml
- name: convert CRLF to LF
  replace:
    path: '{{ item }}'
    regexp: '\r'
  with_lines: find /home/user/sample/* -type f

・実行結果

TASK [main : replace] 
changed: [localhost] => (item=/home/user/sample/aaa/bbb/b.txt)
changed: [localhost] => (item=/home/user/sample/aaa/a.txt)
changed: [localhost] => (item=/home/user/sample/ccc/c.txt)

with_fileglob: /home/user/sample/* では再帰的に処理してくれないので注意。

参考

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?