【问题标题】:Ansible: Lineinfile with items in front of the other in same lineAnsible:Lineinfile,项目在同一行中的另一个前面
【发布时间】:2021-01-14 15:01:34
【问题描述】:

我想知道是否可以使用模块 lineinfile 或其他模块在同一行中插入两个或多个项目。 就像那个例子:

- name: Insert content in the File
  lineinfile: 
    dest: /tmp/file.txt
    line: 'Groups = {{ item }}'
    create: yes
  with_items: 
    - "{{ group_ad }}"

group_ad:
  - LINUX
  - DBA

在执行剧本后留在文件中:

Groups = LINUX

Groups = DBA

但我想保持如下:

Groups = LINUX, DBA

我是谁做的?

【问题讨论】:

    标签: ansible ansible-template


    【解决方案1】:

    您正在迭代列表,但您想添加列表。所以在这里使用 join 更容易

    - name: Insert content in the File
      lineinfile: 
        dest: /tmp/file.txt
        line: 'Groups = {{ group_ad | join(', ') }}'
        create: yes
    

    【讨论】:

    • 使用这个{{ group_ad|join(', ') }}
    • @VladimirBotka 谢谢,你的回答给了我解决方案
    • 对不起,是的 - 你的权利。在别的地方。
    猜你喜欢
    • 1970-01-01
    • 2019-11-06
    • 1970-01-01
    • 2014-08-09
    • 2020-09-25
    • 1970-01-01
    • 2020-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多