5
9

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 1 year has passed since last update.

Ansible 2.6 — replace モジュールを使って置換する

Last updated at Posted at 2018-08-01

replace module を使うことでファイル内の文字列を置換できる。

パラメータ

基本

パラメータ 説明
path リモートマシン上のファイルパス
regexp マッチさせる正規表現
https://docs.python.org/2/library/re.html

行頭マッチ ^ と行末マッチ $を使うことで1行ごとの置換ができる
replace 置換後の文字列
指定なしの場合はマッチした文字列を削除する

応用

パラメータ 説明
backup yes を指定した場合は置換前のバックアップが取られる
例: people_source.txt.772.2018-08-01@12:00:28~
unsafe_writes デフォルトではnoでアトミックに置換処理をおこなうが、yesにして非アトミックな処理に変えることも出来る
(モジュールドキュメントではdockerでのマウントファイルの場合が挙げられている)
encoding ファイルを読み書きするエンコード
デフォルトは utf-8

Playbookの例

01_replace_playbook.yml
- hosts:
    - localhost

  tasks:
    - name: copy
      copy:
        src: people_source.txt
        dest: people_replaced.txt

    # Replace "Alice" to "Alice is lady" in all line
    - name: replace
      replace:
        path: people_replaced.txt
        regexp: '^Alice$'
        replace: Alice is a lady

    # Replace "Bob (Lastname)" to "Bob (Lastname) is gentleman" in all line
    - name: replace
      replace:
        path: people_replaced.txt
        # () is capture of regular expression
        regexp: '^Bob ([a-zA-Z]+)$'
        # \1 is replaced by first captured strings
        replace: 'Bob \1 is a gentleman'

    - name: check replaced file
      slurp:
        src: people_replaced.txt
      register: replaced_file

    - name: check replaced file body
      debug:
        msg: "{{ replaced_file.content | b64decode }}"

元ファイル

people_source.txt
Alice
Bob Marley
Carol
Alice
Bob Wills
Carol
Alice
Bob Horper
Carol

置換後のファイル

people_replaced.txt
Alice is a lady
Bob Marley is a gentleman
Carol
Alice is a lady
Bob Wills is a gentleman
Carol
Alice is a lady
Bob Horper is a gentleman
Carol

実行例


PLAY [localhost] **************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************
ok: [localhost]

TASK [copy] *******************************************************************************************************************************
changed: [localhost]

TASK [replace] ****************************************************************************************************************************
changed: [localhost]

TASK [replace] ****************************************************************************************************************************
changed: [localhost]

TASK [check replaced file] ****************************************************************************************************************
ok: [localhost]

TASK [check replaced file body] ***********************************************************************************************************
ok: [localhost] => {
    "msg": "Alice is a lady\nBob Marley is a gentleman\nCarol\nAlice is a lady\nBob Wills is a gentleman\nCarol\nAlice is a lady\nBob Horper is a gentleman\nCarol\n"
}

PLAY RECAP ********************************************************************************************************************************
localhost                  : ok=6    changed=3    unreachable=0    failed=0   

Gist

参考

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

5
9
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
5
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?