How to Prompt for Variables in Ansible Tasks and Update sysctl.conf Dynamically


2 views

When working with Ansible's interactive prompts, many developers encounter scope issues where variables aren't properly passed between tasks. The error message 'kernel' is undefined typically indicates the variable wasn't registered correctly from the prompt.

Here's the correct way to structure your playbook to capture user input and apply it to sysctl.conf:

- name: Configure kernel parameters
  hosts: all
  become: yes
  vars_prompt:
    - name: shmmax_value
      prompt: "Enter the value for kernel.shmmax"
      private: no

  tasks:
    - name: Update sysctl.conf with shmmax value
      lineinfile:
        path: /etc/sysctl.conf
        regexp: "^kernel.shmmax"
        line: "kernel.shmmax = {{ shmmax_value }}"
        state: present
        backup: yes
  • Using vars_prompt at play level instead of task level ensures proper variable scope
  • Clear variable naming (shmmax_value) avoids confusion with the parameter name
  • Added backup: yes to create a backup of sysctl.conf before modification

For production use, consider adding input validation:

- name: Validate and configure kernel parameters
  hosts: all
  become: yes
  vars_prompt:
    - name: shmmax_value
      prompt: "Enter the value for kernel.shmmax (numeric)"
      private: no

  tasks:
    - name: Validate shmmax input
      fail:
        msg: "shmmax must be a positive integer"
      when: not shmmax_value.isdigit() or shmmax_value|int < 1

    - name: Update sysctl.conf
      lineinfile:
        path: /etc/sysctl.conf
        regexp: "^kernel\\.shmmax"
        line: "kernel.shmmax = {{ shmmax_value }}"
        state: present

If you prefer task-level prompts, use register properly:

- name: Get shmmax value
  pause:
    prompt: "Enter value for kernel.shmmax"
  register: shmmax_prompt

- name: Apply shmmax setting
  lineinfile:
    path: /etc/sysctl.conf
    regexp: "^kernel\\.shmmax"
    line: "kernel.shmmax = {{ shmmax_prompt.user_input }}"

When working with Ansible to modify system parameters in sysctl.conf, you might encounter issues with variable handling between prompts and subsequent tasks. The error message 'kernel' is undefined typically indicates a variable scope or naming issue.

The original approach attempts to:

- Prompt for a value
- Store it in a variable
- Use that variable in a lineinfile task

However, there are two key issues:

  1. The variable from prompt isn't properly registered
  2. The variable reference in the lineinfile task doesn't match the prompted variable

Here's the proper way to implement this:

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

- name: Set kernel.shmmax parameter
  lineinfile:
    path: /etc/sysctl.conf
    regexp: "^kernel.shmmax"
    line: "kernel.shmmax = {{ shmmax_value }}"
    state: present
    create: true
    backup: yes

For more robust implementations, consider these approaches:

1. Validation with Custom Filters

- name: Validate and set shmmax
  vars_prompt:
    - name: shmmax_value
      prompt: "Enter shmmax value (numeric only)"
      private: false
  tasks:
    - name: Verify input is numeric
      fail:
        msg: "shmmax must be a numeric value"
      when: not shmmax_value is match('^[0-9]+$')

2. Using Default Values

- name: Set shmmax with default
  vars_prompt:
    - name: shmmax_value
      prompt: "Enter shmmax value [default: 4194304]"
      default: "4194304"
      private: false

3. Complete Playbook Example

- hosts: all
  become: yes
  vars_prompt:
    - name: shmmax_value
      prompt: "Enter kernel.shmmax value"
      private: false

  tasks:
    - name: Update sysctl.conf
      lineinfile:
        path: /etc/sysctl.conf
        regexp: "^kernel.shmmax"
        line: "kernel.shmmax = {{ shmmax_value }}"
        state: present
        backup: yes

    - name: Load new settings
      command: sysctl -p
      register: sysctl_result
      changed_when: sysctl_result.rc == 0
  • Always verify variable names match between declaration and usage
  • Use debug tasks to check variable values
  • Consider using ansible-vault for sensitive parameters
  • Test changes with --check mode first

For production environments:

  1. Store commonly used values in group/host vars
  2. Implement input validation
  3. Add handler for sysctl -p after file changes
  4. Document acceptable value ranges in the prompt