Ansible:检查文件文本,如果没有则添加它

Ansible: check for file text and if not there add it

这是我目前拥有的:

- hosts: test
  tasks:
    - name: check existence of line in the target file to replace if there
      command: grep fs.file-max /etc/sysctl.conf
      changed_when: false
      failed_when: false
      register: file_test
    - name: add config line to the file and reload
      lineinfile:
        path: /etc/sysctl.conf
        line: fs.file-max = 65000
      when: item.rc == 1
      with_items:
        - '{{ file_test.results }}'
      command: sysctl -a

但是,它总是失败并显示以下内容:

The error appears to be in '/root/playbooks/sysctl.yml': line 8, column 7, but may                                    
be elsewhere in the file depending on the exact syntax problem.                                                      
                                                                                                                      
The offending line appears to be:                                                                                    
                                                                                                                      
      register: file_test                                                                                             
    - name: add config line to the file and reload                                                                    
      ^ here                

我试过修复缩进并改变我这样做的方式,但我什么也没得到。

查看 sysctl 模块。对于 sysctl.conf 文件中的 add/edit 行,您可以使用它来代替使用多个任务和逻辑。

示例:

- hosts: test

  tasks:
  - name: add fs.file-max to sysctl.conf
    sysctl:
      name: 'fs.file-max'
      value: '65000'