2016-06-15 6 views
0

SSL証明書は数日で使い果たされます。だから私は、Anabilitiesがサーバ上に新しい証明書を置くことができ、apache2サイトを変更できると考えました。可能なモジュール "lineinfile"が複数のファイルの複数の行を置き換えます

サーバーのサイトがこのサーバーで実行されています。

私は次の行置き換えたい:

  • をSSLCertificateChainFile
  • SSLCertificateKeyFile
  • SSLCertificateFile

私は、内のすべてのサイトのリストを取得するには、このコマンドを使用します/ etc/apache2パターン "SSLCertificate"が存在する場所。

- name: apache2.2.* | configure certs 
    lineinfile: dest=/path/to/... regexp={{ item.regexp }} line={{ item.line}} backrefs=yes 
    with_items: 
     - { regexp: "SSLCertificateChainFile", line: " SSLCertificateChainFile = ..." } 
     - { regexp: "SSLCertificateKeyFile ", line: " SSLCertificateKeyFile = ..." } 
     - { regexp: "SSLCertificateFile", line: " SSLCertificateFile = ..." 
    notify: reload apache2 

がどのように私は、変数にリストされている複数のファイルでこのコードを使用するansible伝えることができを「apache22_sites」:

- name: Apache 2.2 list sites files and store it in register 
    command: grep -lR --exclude default-ssl "SSLCertificate" /etc/apache2/ 
    register: apache22_sites 

この

は私が唯一つのファイルを変更する必要がある場合、使用しているものです複数行?

私は良いヒントを見つけましたhere、悲しいことに1行だけが悪いです。

私は任意のティップス、トリックを感謝し、ヒント:)

挨拶 デニス

+0

は、あなたがすべき良いpractice-ない

は、だからあなたの場合には、あなたのようなものを持っているでしょうテンプレートモジュールを使用してください。 – tedder42

答えて

0

tedder42コメントで指摘し、人々がlineinfileを使用している場合、一般的にそうであるように、あなたは多くのだろうとこれらのファイルを代わりにtemplatingより良い。

しかし、複数のリストをどのようにループするかというより一般的な問題を解決したい場合は、with_nested loopを使用する必要があります。あなたはこのようにどこか​​を定義している限り

- name: Apache 2.2 list sites files and store it in register 
    command: grep -lR --exclude default-ssl "SSLCertificate" /etc/apache2/ 
    register: apache22_sites 

- name: apache2.2.* | configure certs 
    lineinfile: dest={{ item.0 }} regexp={{ item.1.regexp }} line={{ item.1.line}} backrefs=yes 
    with_nested: 
     - apache22_sites 
     - lines_to_replace 
    notify: reload apache2 

:このようlineinfile使用

lines_to_replace: 
    - { regexp: "SSLCertificateChainFile", line: " SSLCertificateChainFile = ..." } 
    - { regexp: "SSLCertificateKeyFile ", line: " SSLCertificateKeyFile = ..." } 
    - { regexp: "SSLCertificateFile", line: " SSLCertificateFile = ..." 
関連する問題