Ansible 使用\u项运行多个LINEINFLE命令
如何在ansible中实现以下目标?我想在服务器列表中运行多个lineinfile 如果我执行以下操作,ansible会因为重复的Ansible 使用\u项运行多个LINEINFLE命令,ansible,devops,Ansible,Devops,如何在ansible中实现以下目标?我想在服务器列表中运行多个lineinfile 如果我执行以下操作,ansible会因为重复的lineinfle - name: ensure /etc/environment is correct tags: env with_items: "{{ hosts }}" lineinfile: dest=/etc/environment regexp=^PROFILE=line=PROFILE={{prof}} lineinfile: dest
lineinfle
- name: ensure /etc/environment is correct
tags: env
with_items: "{{ hosts }}"
lineinfile: dest=/etc/environment regexp=^PROFILE=line=PROFILE={{prof}}
lineinfile: dest=/etc/environment regexp=^OTHER=line=OTHER={{other}}
我尝试用_项嵌套
,但没有成功。以下是示例剧本,以确保文件/tmp/testfile在服务器1和服务器2上都将PROFILE设置为my_new_PROFILE,将OTHER设置为my_new_OTHER:
- hosts: server1, server2
tasks:
- lineinfile: dest=/tmp/testfile regexp={{item.regex}} line={{item.replace_with}}
with_items:
- regex: ^PROFILE=
replace_with: PROFILE=my_new_profile
- regex: ^OTHER=
replace_with: OTHER=my_new_other
更新:使用动态组和嵌套循环创建两个文件:testfile_host1和testfile_host2,在playbook中前面创建的my_dynamic_组中的所有服务器上都有行
- hosts: localhost
tasks:
- add_host: name={{ item }} groups=my_dynamic_group
with_items:
- server1
- server2
- hosts: my_dynamic_group
tasks:
- lineinfile: dest=/tmp/testfile_{{ item[0] }} regexp={{item[1].regex}} line={{item[1].replace_with}} create=yes
with_nested:
- [ 'host1', 'host2' ]
- [
{ regex: '^PROFILE=', replace_with: 'PROFILE=my_new_profile' },
{ regex: '^OTHER=', replace_with: 'OTHER=my_new_other' }
]
以下是示例剧本,以确保file/tmp/testfile在server1和server2上的PROFILE设置为my_new_PROFILE,OTHER设置为my_new_OTHER:
- hosts: server1, server2
tasks:
- lineinfile: dest=/tmp/testfile regexp={{item.regex}} line={{item.replace_with}}
with_items:
- regex: ^PROFILE=
replace_with: PROFILE=my_new_profile
- regex: ^OTHER=
replace_with: OTHER=my_new_other
更新:使用动态组和嵌套循环创建两个文件:testfile_host1和testfile_host2,在playbook中前面创建的my_dynamic_组中的所有服务器上都有行
- hosts: localhost
tasks:
- add_host: name={{ item }} groups=my_dynamic_group
with_items:
- server1
- server2
- hosts: my_dynamic_group
tasks:
- lineinfile: dest=/tmp/testfile_{{ item[0] }} regexp={{item[1].regex}} line={{item[1].replace_with}} create=yes
with_nested:
- [ 'host1', 'host2' ]
- [
{ regex: '^PROFILE=', replace_with: 'PROFILE=my_new_profile' },
{ regex: '^OTHER=', replace_with: 'OTHER=my_new_other' }
]
请描述您的示例中组['app']
的含义?要在ansible中的服务器列表上运行任务,请使用播放的hosts:
参数。要使用不同的参数运行同一任务,请在任务的参数中使用和{{item}
占位符。如果您想要嵌套循环(例如,为文件中的某些不同应用更改多个不同的行),可以将与\u nested:
一起使用。在任何情况下,任务的名称(lineinfle
在您的示例中)总是只指定一次。@KonstantinSuvorov而不是groups[app]
它可以是hosts
。我想在服务器列表上运行几个类似的任务。我使用嵌套的没有任何运气,你能提供一个例子吗?lineinfle
是一种反模式。你应该考虑<代码>模板<代码>。你的ANDILE版本是什么?我用的是AnEngress 2。请描述你的例子中的<代码>组[App' ] < /Cord>意思是什么?要在ansible中的服务器列表上运行任务,请使用播放的hosts:
参数。要使用不同的参数运行同一任务,请在任务的参数中使用和{{item}
占位符。如果您想要嵌套循环(例如,为文件中的某些不同应用更改多个不同的行),可以将与\u nested:
一起使用。在任何情况下,任务的名称(lineinfle
在您的示例中)总是只指定一次。@KonstantinSuvorov而不是groups[app]
它可以是hosts
。我想在服务器列表上运行几个类似的任务。我使用嵌套的没有任何运气,你能提供一个例子吗?lineinfle
是一种反模式。你应该考虑<代码>模板>代码>什么是你的ANDILE版本?我使用的是AnEffice 2。我不能用这个,因为我已经使用了不同的<代码> {主机}} /代码>我不知道你的意思是什么,但是我已经用动态组和嵌套循环示例更新了我的答案。我不能用这个,因为我已经使用了不同的<代码> {主机}}。
我不确定您的意思,但我已经用动态组和嵌套循环示例更新了我的答案。