Loops 使用Ansible替换匹配文件中的几个单词
该项目将更新许多服务器上的通配符证书路径,每个服务器都有一个名称不同的Vhost-*.conf文件 我想搜索与Loops 使用Ansible替换匹配文件中的几个单词,loops,text,replace,ansible,Loops,Text,Replace,Ansible,该项目将更新许多服务器上的通配符证书路径,每个服务器都有一个名称不同的Vhost-*.conf文件 我想搜索与V*.conf匹配的文件,然后对它们进行grep,并替换crt,键和ca的值,如下所示 我找到的最接近的答案是,但我无法让它按原样运行。我认为replace模块比lineinfle更合适,因为我不想重写整行代码,而是想替换文件中出现的任意数量的内容 经过一些更改后,这是我最近的一次,但我还没有弄清楚为什么我的语法不正确: --- - hosts: myhost5 become: ye
V*.conf
匹配的文件,然后对它们进行grep,并替换crt
,键
和ca
的值,如下所示
我找到的最接近的答案是,但我无法让它按原样运行。我认为replace
模块比lineinfle更合适,因为我不想重写整行代码,而是想替换文件中出现的任意数量的内容
经过一些更改后,这是我最近的一次,但我还没有弄清楚为什么我的语法不正确:
---
- hosts: myhost5
become: yes
tasks:
- name: grab conf file names
shell: ls /etc/httpd/conf.d/V*.conf
register: vhost_files
- name: replace text
replace:
dest: '{{ item.0 }}'
regexp: '{{ item.1.regexp }}'
line: '{{ item.1.line}}'
backrefs: yes
backup: yes
with_nested:
- "{{vhost_files}}"
- "{{text_to_replace}}"
vars:
text_to_replace:
- { "regexp: 'mywildcard2014.crt', line: 'mywildcard.2016.crt'" }
- { "regexp: 'mywildcard2048_2014.key', line: 'mywildcard.2016.key'" }
- { "regexp: 'gd_bundle2014.crt', line: 'mywildcard.2016.ca-bundle'" }
handlers:
- name: restart apache
service: name=httpd state=restarted
我得到的答复是:
the field 'args' has an invalid value, which appears to include a
variable that is undefined. The error was: 'dict object' has no attribute 'regexp'
首先,您需要在此处删除不必要的双引号:
- { regexp: 'mywildcard2014.crt', line: 'mywildcard.2016.crt' }
但是您的代码中还有许多其他的小错误。还要记住,使用shell命令而不是模块不是一种可行的方法。
考虑使用<代码>查找< /代码>模块,而不是<代码> shell:ls > /p>
---
- hosts: myhost5
become: yes
vars:
text_to_replace:
- { regexp: 'mywildcard2014.crt', line: 'mywildcard.2016.crt' }
- { regexp: 'mywildcard2048_2014.key', line: 'mywildcard.2016.key' }
- { regexp: 'gd_bundle2014.crt', line: 'mywildcard.2016.ca-bundle' }
tasks:
- name: grab conf file names
find:
pattern: V*.conf
path: /etc/httpd/conf.d/
register: vhost_files
- name: replace text
replace:
dest: '{{ item.0.path }}'
regexp: '{{ item.1.regexp }}'
replace: '{{ item.1.line}}'
backup: yes
with_nested:
- "{{vhost_files.files}}"
- "{{text_to_replace}}"
notify: restart apache
handlers:
- name: restart apache
service: name=httpd state=restarted
我真的很感谢你帮我清理那些肮脏的代码!谢谢康斯坦丁的帮助!