通过ansible在配置文件中添加字符串

通过ansible在配置文件中添加字符串,ansible,yaml,ansible-template,Ansible,Yaml,Ansible Template,我需要在一个配置文件中添加一个新字符串。我必须在images=中添加一个,比如说mycustomimage。简而言之,我需要的输出是images=previousimage,mycustomimage mycnf.conf的视图 id=1 images=previousimage 为此,我尝试了这个代码 --- - hosts: test_server - name: Add new string after "," in images lineinfile: pa

我需要在一个配置文件中添加一个新字符串。我必须在images=中添加一个,比如说mycustomimage。简而言之,我需要的输出是images=previousimage,mycustomimage

mycnf.conf的视图

id=1
images=previousimage
为此,我尝试了这个代码

---
- hosts: test_server
  - name: Add new string after "," in images
    lineinfile:
        path: /home/mycnf.conf
        regexp: 'images='
        insertafter: '^,'
        line: mycustomimage

预期产量

id= 1
images=previousimage,mycustomimage
但这对我不起作用。有什么想法吗? 提前谢谢! Sid

来自您的示例

您知道文件中最后一行是images=previousimage,mycustomimage 如果此行存在且与最后一行不匹配,则要添加此行以替换已存在的图像=.*行。 以下内容将完成这项工作

    - name: Replace line if needed
      lineinfile:
        path: /home/mycnf.conf
        regex: images=.*
        line: images=previousimage,mycustomimage
注意:如果由于任何原因,您的文件中没有与regex匹配的行,则该行将添加到文件的末尾。

来自您的示例

您知道文件中最后一行是images=previousimage,mycustomimage 如果此行存在且与最后一行不匹配,则要添加此行以替换已存在的图像=.*行。 以下内容将完成这项工作

    - name: Replace line if needed
      lineinfile:
        path: /home/mycnf.conf
        regex: images=.*
        line: images=previousimage,mycustomimage
注意:如果由于任何原因,您的文件中没有与正则表达式匹配的行,该行将添加到文件的末尾。

这将有所帮助

- name: replace line
  lineinfile:
    path: myfile.txt
    regexp: "^image="
    line: 'image=previousimage,customimage'
这会有帮助的

- name: replace line
  lineinfile:
    path: myfile.txt
    regexp: "^image="
    line: 'image=previousimage,customimage'
假设不知道previousimage,您可以做两件事:

1.使用grep获取此行,注册到变量并添加该行

- name: get line
  shell: grep "^image=" /config/file.something
  register: current_image

- name: update image
  lineinfile:
    path: /config/file.something
    regexp: '^image='
    line: "{{ current_image.stdout }},{{ new_image | default('customimage') }}"
2.为此配置文件创建模板,并在每次运行playbook和检测到更改时进行渲染:

- set_fact:
    images: <list of images retrieved by lookup or static>

- name: update config.something
  template:
     src: my_template.j2
     dest: /config/file.something
假设不知道previousimage,您可以做两件事:

1.使用grep获取此行,注册到变量并添加该行

- name: get line
  shell: grep "^image=" /config/file.something
  register: current_image

- name: update image
  lineinfile:
    path: /config/file.something
    regexp: '^image='
    line: "{{ current_image.stdout }},{{ new_image | default('customimage') }}"
2.为此配置文件创建模板,并在每次运行playbook和检测到更改时进行渲染:

- set_fact:
    images: <list of images retrieved by lookup or static>

- name: update config.something
  template:
     src: my_template.j2
     dest: /config/file.something

我们假设您不知道previousimage是什么,您可能有几个以前的图像,并且您希望附加mycustomimage,并且您希望playbook是幂等的:

---
- hosts: all
  connection: ssh
  become: no
  gather_facts: no


  vars:
    image_name: mycustomimage

  tasks:
  - lineinfile:
      path: testfile.txt
      regexp: '^images=(.*(?<!{{ image_name }}))'
      line: '\1,{{ image_name }}'
      backrefs: yes
让我们来解释一下regexp:^images=您可以自己解决问题

第一个括号开始backref块,它将把所有内容都吸收到行的末尾:*

然后,它回顾它刚刚吸入的内容,并确保{{image_name}}不在末尾:


最后,我们使用关闭backref块。

我们假设您不知道previousimage是什么,您可能有几个以前的图像,并且您希望附加mycustomimage,并且您希望playbook是幂等的:

---
- hosts: all
  connection: ssh
  become: no
  gather_facts: no


  vars:
    image_name: mycustomimage

  tasks:
  - lineinfile:
      path: testfile.txt
      regexp: '^images=(.*(?<!{{ image_name }}))'
      line: '\1,{{ image_name }}'
      backrefs: yes
让我们来解释一下regexp:^images=您可以自己解决问题

第一个括号开始backref块,它将把所有内容都吸收到行的末尾:*

然后,它回顾它刚刚吸入的内容,并确保{{image_name}}不在末尾:


最后,我们用。

关闭backref块。您能提供预期结果的样本吗?问题中已经提到了。所需输出为images=previousimage,MyCustomImages您的行是否总是images=previousimage?您的初始行是ìmages=previousimage,它以昏迷结束?我会改变吗?如果它不同,是否可以包含多个图像?在这种情况下,它会永远以昏迷结束吗?它能在一个随机的地方包含自定义图像吗?请编辑您的问题并给出初始配置行的精确示例,详细描述可能的变化(如果有)和预期结果。是什么?我的评论中有5个问题。你能提供预期结果的样本吗?问题中已经提到了。所需输出为images=previousimage,MyCustomImages您的行是否总是images=previousimage?您的初始行是ìmages=previousimage,它以昏迷结束?我会改变吗?如果它不同,是否可以包含多个图像?在这种情况下,它会永远以昏迷结束吗?它能在一个随机的地方包含自定义图像吗?请编辑您的问题并给出初始配置行的精确示例,详细描述可能的变化(如果有)和预期结果。是什么?我的评论中有5个问题。