Ansible lineinfile – Fixing Issue with Finding a Particular String

ansibleansible-playbook

I would like to check if admin_token is defined in my keystone.conf file. To do so I want to use Ansible. However, even if the attribute is present in the file, the task fails.

I compared two tasks with the lineinfile module (same regex, state present then absent). In the first task, Ansible didn't find the line. However, it found it in the second task. No modification is done on the file at any time.

Where could this come from?

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

Here after the playbook

- 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

Best Answer

The lineinfile module is meant to edit files, not simply check their content, and thus requires you to supply a replacement line when you specify state=present. Because you didn't specify a replacement, you get the error: "line is required with state=present".

How to solve the problem depends on context that you haven't given in your question. But I would guess you should be setting the admin_token in a replacement line. For example:

    line: "admin_token={{keystone_admin_token}}"