简体   繁体   English

如何使用ansible替换文件中的特定行?

[英]How to replace a particular line in a file using ansible?

I want to use ansible to go to the nignx configuration file and and replace the ssl_certificate line with my own.我想使用 ansible 转到 nignx 配置文件,并将ssl_certificate行替换为我自己的。 This is the line in nginx.conf这是nginx.conf的行

ssl_certificate "/etc/pki/nginx/server.crt";

All I want to know is what module and how do I use it to replace the path of the certificate to what I want.我只想知道是什么模块以及如何使用它来将证书的路径替换为我想要的。

Try below试试下面

- name: modify
  replace:
    path=/path/to/nginx.conf
    regexp="^(ssl_certificate\s+)[^\n]+$"
    replace="ssl_certificate \"/required/file/path/here\";"

To replace a particular line in a file using ansible you can use the lineinfile_module使用ansible替换文件中的特定行,您可以使用lineinfile_module

- name: Replace a localhost entry with our own
  lineinfile:
    path: nginx.conf
    regexp: '^    ssl_certificate "/etc/pki/nginx/server.crt";'
    line: '    ssl_certificate "/your/path/server.crt";'

In my example I get this diff在我的例子中,我得到了这个差异

<     ssl_certificate "/etc/pki/nginx/server.crt";
---
>     ssl_certificate "/your/path/server.crt";

But consider using the ansible role for NGINX .但请考虑对 NGINX使用ansible 角色

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM