当状态存在时,Ansible lineinfile 找不到特定的字符串

当状态存在时,Ansible lineinfile 找不到特定的字符串

我想检查admin_token我的文件中是否定义了keystone.conf。为此,我想使用 Ansible。但是,即使文件中存在该属性,任务也会失败。

我用该lineinfile模块比较了两个任务(相同的正则表达式,状态先出现然后消失)。在第一个任务中,Ansible 没有找到该行。但是,它在第​​二个任务中找到了它。任何时候都不会对文件进行任何修改。

这是从哪里来的?

PLAY [Docker] ******************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************
ok: [keystone]

TASK [Check admin_token is present] *******************************************************************************
fatal: [keystone]: FAILED! => {"changed": false, "msg": "line is required with state=present"}
...ignoring

TASK [Check admin_token not present] *******************************************************************************
changed: [keystone]

TASK [debug] *******************************************************************************************************
ok: [keystone] => {
    "comment_admin_token_1": {
        "changed": false,
        "failed": true,
        "msg": "line is required with state=present"
    }
}

TASK [debug] *******************************************************************************************************
ok: [keystone] => {
    "comment_admin_token_2": {
        "backup": "",
        "changed": true,
        "diff": [
            {
                "after": "",
                "after_header": "/etc/keystone/keystone.conf (content)",
                "before": "",
                "before_header": "/etc/keystone/keystone.conf (content)"
            },
            {
                "after_header": "/etc/keystone/keystone.conf (file attributes)",
                "before_header": "/etc/keystone/keystone.conf (file attributes)"
            }
        ],
        "failed": false,
        "found": 1,
        "msg": "1 line(s) removed"
    }
}

PLAY RECAP *********************************************************************************************************

keystone                   : ok=5    changed=1    unreachable=0    failed=0

剧本如下

- name: Docker
  hosts: containers
  connection: docker
  tasks:
  - name: Check admin_token is present 
    lineinfile:
      path: /etc/keystone/keystone.conf
      regexp: '^admin_token *= *.*'
      state: present
    register: comment_admin_token_disabled
    ignore_errors: yes
    check_mode: yes
  - name: Check admin_token not present 
    lineinfile:
      path: /etc/keystone/keystone.conf
      regexp: '^admin_token *= *.*'
      state: absent
    register: comment_admin_token_disabled
    ignore_errors: yes
    check_mode: yes

答案1

lineinfile模块旨在编辑文件,而不仅仅是检查其内容,因此要求您line在指定时提供替换state=present。由于您没有指定替换,因此您会收到错误:"line is required with state=present"

admin_token如何解决问题取决于您在问题中未提供的背景。但我猜您应该在替换中设置line。例如:

    line: "admin_token={{keystone_admin_token}}"

相关内容