2
2

More than 5 years have passed since last update.

Ansibleで設定ファイルの行を書き換える(コンテキスト編)

Posted at

すでにある環境に対して、Ansibleで構成変更するとき、特定のコンテキストのことろだけ、既存の行を変更したいことがある。

sed を使って、書き換えるべきかどうかを判定して、必要なときに書き換える例。

- hosts: servers
  vars:
    httpd_conf: /etc/apache2/apache2.conf
  tasks:
    - name: lookup DocumentRoot in VirtualHost
      shell: sed -n '/^<VirtualHost \*:80>/,/<\/VirtualHost>/{/DocumentRoot/p; }' {{httpd_conf}}
      register: lookup
      changed_when: False
    - name: change httpd.conf
      shell: sed -i$(date +'.%Y-%m-%d@%H:%M~') '/^<VirtualHost \*:80>/,/<\/VirtualHost>/{s/\(DocumentRoot[[:space:]]*\).*$/\1\/var\/www/; }' {{httpd_conf}}
      when: lookup.stdout != ""
2
2
1

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