【问题标题】:How to add multiple lines in all files present in a directory using Ansible如何使用 Ansible 在目录中存在的所有文件中添加多行
【发布时间】:2022-08-24 07:14:12
【问题描述】:

在 Ansible 脚本中,首先我使用 find_module 查找目录中的所有文件,然后我使用 set_fact 提及我想在所有文件中添加的所有命令,然后我使用 lineinfile 模块添加多行在所有文件中,但它是以列表格式添加所有命令 [\'line1\',\'line2\',\'line3\'] 而不是这个我希望这些行在所有文件中一个接一个地添加。 下面提到的是脚本

    tasks:
      - name: finding all files present in something directory
        find:
          paths: /etc/something.d/
          file_type: file
          patterns: \'*.d\'
        register: c1
        become: true
      - set_fact:
          lines:
          - \"line1\"
          - \"line2\"
          - \"line3\"
      - lineinfile:
          path: \"{{ item.path }}\"
          line: \"{{ lines}}\"
          state: present
          create: yes
          backup: yes
        register: c2
        become: true
        with_items: \"{{ c1.files }}\"
      - debug:
          var: c1
      - debug:
          var: c2
  • 您有两次 with_items ...检查有关嵌套循环的文档。

标签: ansible


【解决方案1】:

所以......我更喜欢的旧方式是with_nested

set_fact:
  lines:
    - line1
    - line2
    - line3

lineinfile:
  ...
  ...
with_nested:
  - "{{ c1.files }}"
  - "{{ lines }}"

新的方式,我不知道从博士那里逃脱了什么。 CS程序想出这个,就是把列表组合起来:

set_fact:
  lines:
    - line1
    - line2
    - line3

lineinfile:
  ...
  ...
loop: "{{ c1.files | product | lines | list }}"

https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#with-nested-with-cartesian

如果所有行都在一起,您可能需要查看blockinfile

【讨论】:

    【解决方案2】:

    通过使用下面提到的 ansible 脚本,我能够在目录中存在的所有文件中添加多行,但是现在的问题是所有行都重复了,如果我再次运行此脚本,请提出任何方法,以免行得到在所有文件中重复

        tasks:
          - name: finding all files present in something directory
            find:
              paths: /etc/something.d/
              file_type: file
              patterns: '*.d'
            register: c1
            become: true
          - set_fact:
              lines:
              - "line1"
              - "line2"
              - "line3"
          - lineinfile:
              path: "{{ item.path }}"
              line: "{{ lines[0]+'\n'+lines[1]+'\n'+lines[2]+'\n'}}"
              state: present
              create: yes
              backup: yes
            register: c2
            become: true
            with_items: "{{ c1.files }}"
          - debug:
              var: c1
          - debug:
              var: c2
    

    【讨论】:

      【解决方案3】:

      这对我有用:

      tasks:
        - name: finding all files present in something directory
          find:
            paths: /etc/something.d/
            file_type: file
            patterns: '*.d'
          register: c1
          become: true
        - blockinfile:
            path: "{{ item.path }}"          
            state: present
            create: yes
            backup: yes
            block: |
              line1
              line1
              line1  
          register: c2
          become: true
          with_items: "{{ c1.files }}"
        - debug:
            var: c1
        - debug:
            var: c2
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多