LoginSignup
5
4

More than 5 years have passed since last update.

【ansible】ファイルの先頭行に文字列を追加するplaybookの例

Last updated at Posted at 2015-11-27

きっとansibleに限らずなんだけども設定ファイルなどでファイルの先頭行に文字列を追加したい時があると思う。

(設定ファイルの設定の順番で挙動で変わるものとか)

今まではCentOSであんまり気にしなかったけどもどうやらOSXではうまく行かなかったのでその時のメモ

そして@urauraさんのやり方が一番シンプルだったことに気づいた・・・・

環境

  • Mac OSX Yosemite(10.10.5)
    • python 2.7.10
      • ansible 1.9.4

ファイルの先頭行に文字列を追加するplaybook

~/.zshrc に挿入したい文字列が存在しなければ .zshrc の先頭行に文字列を追加する例。lineinfileモジュールを使う
(そして教えてもらった@urauraさんに感謝)

/path/to/ansible-role/tasks/main.yml
- name: Add .zshrc prezto.init.sh
  lineinfile:
    dest={{ ansible_env.HOME }}/.zshrc
    line="source ~/.zsh/prezto.init.sh"
    insertbefore=BOF state=present regexp="prezto\.init\.sh"
  notify:
    - execute source .zshrc

最後に

小1時間ほどハマってしまった・・・・

5
4
3

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
4