Ansible prompt a variable in a task

ansible

i'm trying to add a value in inline module in sysctl.conf file, how can i achieve that? once i enter the value in the prompt it should be updated in the sysctl.conf file.

- name: shmmax
      prompt: " Please enter the value for kernel.shmmax "
      private: false


- name: Set some kernel parameters
      lineinfile:
        dest: /etc/sysctl.conf
        regexp: "{{ item.regexp }}"
        line: "{{ item.line }}"
      with_items:
        - { regexp: '^kernel.shmmax', line: 'kernel.shmmax = {{ shmmax }}' }


error is get is.

TASK [Set some kernel parameters] **********************************************************************************************************************************
fatal: [192.168.1.28]: FAILED! => {"msg": "'kernel' is undefined"}
        to retry, use: --limit `enter code here`

Thank you

Best Answer

There is the pause module to prompt for an input inside a task.

For example

    - hosts: localhost
      tasks:
        - pause:
            prompt: "Please enter the value for kernel.shmmax "
            echo: yes
          register: result
        - set_fact:
            shmmax: "{{ result.user_input }}"
        - debug:
            var: shmmax

gives (abridged)

    TASK [pause] 
    [pause]
    Please enter the value for kernel.shmmax :
    [ok: [localhost]
    TASK [set_fact]
    ok: [localhost]
    TASK [debug] 
    ok: [localhost] => {
        "shmmax": "new_kernel.shmmax_value"
    }

Notes

  1. This part of the code from the question can't work because there is no module in the task
    - name: shmmax
          prompt: " Please enter the value for kernel.shmmax "
          private: false

Running such code would produce

    ERROR! Syntax Error while loading YAML.
      mapping values are not allowed in this context
  1. A variable can be used in any task after it has been prompted for, registered, and declared with set_fact.