a stray sheep

お仕事周りの雑記帳

冪等性を損なうサンプルplaybook

サンプル

site.yml

---
- hosts: all
  become: true
  tasks:
    - name: test lineinfile
      lineinfile:
        dest: /tmp/test.conf
        regexp: '^#ServerName '
        line: "ServerName {{ ansible_fqdn }}:80"


test.conf

#ServerName localhost:80

ansible-playbookの実行

1回目

ServerName localhost.localdomain:80
  • regexpにマッチした行を、line行で置き換えた

2回目

ServerName localhost.localdomain:80

ServerName localhost.localdomain:80
  • regexpにマッチする行はない。lineが挿入される。

マニュアルにも "The line to insert/replace into the file" と記載されている。

3回目

ServerName localhost.localdomain:80

ServerName localhost.localdomain:80
ServerName localhost.localdomain:80
  • さらに追加される。


何回も実行すると、同じ結果にならない。= 冪等でない。

そんなサンプルでした。