0
1

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 3 years have passed since last update.

ansible 小技

Last updated at Posted at 2019-05-10

file test

Makefile みたいに「ファイルが存在するかどうか」で実行するかどうかを判断したい場合もある。典型的には shell module の creates がまさにそのような挙動をするが、より一般的に次のような when を書けば大丈夫。

- name: some action
  when: not lookup("file", "path/to/file", errors="ignore")

blockinfile で placeholder

plugin 的なものは設定ファイルで blockinfile を使うと便利。

一つのファイルで複数個所書き換える場合は、marker を書き換えておかないと、書き換え合戦が起こることに注意(マーカー(目印)を起点にコメントで囲まれたブロックを放り込むため)。

書き換え先のファイルも自分で用意する場合は、いっそのことあらかじめマーカーを用意しておくと分かりやすくなる。いわゆる「プレースホルダ」。

さらに推し進めると、blockinfilemarker を最初から書いておくという手もありです。

プレースホルダの例

書き換えられる側の設定ファイルにマーカーを用意しておく。

/path/to/conf.ini
[sectionA]

;;sectionA_content

[sectionB]

;;sectionA_content

blockinfile 側の設定では、このマーカーに向かって insertbefore あるいは insertafter で放り込みます。

roles/example/tasks/main.yml
---
- name: Put example in section A
  blockinfile:
    path: /path/to/conf.ini
    insertbefore: ";;sectionA_content"
    marker: "# {mark} ANSIBLE MANAGED BLOCK example"
    content: |
      example1=test1
      example2=test2

直接マーカーを置く

もっとあからさまにコメントブロックを置いておくのでもよい。

/path/to/conf.ini
[sectionA]

# BEGIN ANSIBLE MANAGED BLOCK example
# ansible will fill this block
# END ANSIBLE MANAGED BLOCK example

[sectionB]

# BEGIN ANSIBLE MANAGED BLOCK another
# ansible will fill this block
# END ANSIBLE MANAGED BLOCK another

apt module の update_cache

見当違いのところを調べたりしていたけれども、外部との疎通に問題があっただけだった。

 [WARNING]: Could not find aptitude. Using apt-get instead

fatal: [hostname]: FAILED! => {"changed": false, "msg": "Failed to update apt cache: "}
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?