Ansible,连接文件

pou*_*toi 6 ansible

我有一个具有不同角色的可靠剧本。在每个角色中,我想在同一个配置文件中添加几行。例如,向ini文件添加部分:

[部分]
参数 1=值 1
参数 2=值 2

实现这一目标的最佳方法是什么?

是否可以连接模板片段?例如:

[部分]
param1={{ value_var1 }}
param2={{ value_var2 }}

And*_*nko 3

以你的例子我会这样做

- name: Checking is applied aleady
  shell: grep "ANSIBLE_ROLE_X_APPLIED" /path/to/file
  ignore_errors: yes
  register: grep_role_x_applied

- name: Applying changes in file
  lineinfile: dest=/path/to/file line='{{ item }}'
  when: grep_role_x_applied.stdout == ""
  with_items:
    - '; ANSIBLE_ROLE_X_APPLIED'
    - '[section]'
    - 'param1=value1'
    - 'param2=value2' 
Run Code Online (Sandbox Code Playgroud)

当然,每个角色都有不同的标签,例如 ANSIBLE_ROLE_X_APPLIED

或者基于这个想法的东西

编辑:如果这真的是 ini 文件 - 我最好使用 ansible 核心模块ini_file