chzerv / ansible-role-sysctl

Ansible role for tweaking sysctl on Linux systems.
MIT License
1 stars 0 forks source link

Add vars for Redhat.yml #2

Open damianoneill opened 3 years ago

damianoneill commented 3 years ago

Hi @chzerv when running on Centos 7 (distribution family - Redhat), sysctl_required_packages is not being set correctly.

Assume your task check is matching any instance of Redhat.yml in the playbook, adding a Redhat.yml to your vars directory should resolve the issue.

- name: Set distribution family specific variables.
  include_vars: "{{ item }}"
  with_first_found:
    - "{{ ansible_os_family }}.yml"
    - default.yml
TASK [chzerv.sysctl : Set distribution family specific variables.] ***************************************************************************************
ok: [yyyy-10] => (item=/Users/doneill/projects/git.xxx.net/zzzz/provisioner/roles/common/vars/RedHat.yml) => {"ansible_facts": {"required_packages": ["the_silver_searcher", "ansible", "yum-utils", "nc", "@Xfce"]}, "ansible_included_var_files": ["/Users/doneill/projects/git.xxx.net/zzzz/provisioner/roles/common/vars/RedHat.yml"], "ansible_loop_var": "item", "changed": false, "item": "/Users/doneill/projects/git.xxx.net/zzzz/provisioner/roles/common/vars/RedHat.yml"}

TASK [chzerv.sysctl : Ensure variables are set correctly.] ***********************************************************************************************
included: /Users/doneill/.ansible/roles/chzerv.sysctl/tasks/variable-check.yml for yyyy-10

TASK [chzerv.sysctl : Ensure that the variable 'sysctl_set' is set correctly.] ***************************************************************************
ok: [yyyy-10] => {
    "changed": false,
    "msg": "All assertions passed"
}

TASK [chzerv.sysctl : Ensure that the variable 'sysctl_reload' is set correctly.] ************************************************************************
ok: [yyyy-10] => {
    "changed": false,
    "msg": "All assertions passed"
}

TASK [chzerv.sysctl : Ensure that 'state' is either 'present' or 'absent'.] ******************************************************************************
ok: [yyyy-10] => (item={'name': 'fs.inotify.max_user_watches', 'value': 16384, 'state': 'present'}) => {
    "ansible_loop_var": "item",
    "changed": false,
    "item": {
        "name": "fs.inotify.max_user_watches",
        "state": "present",
        "value": 16384
    },
    "msg": "All assertions passed"
}

TASK [chzerv.sysctl : Install required packages.] ********************************************************************************************************
fatal: [yyyy-10]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'sysctl_required_packages' is undefined\n\nThe error appears to be in '/Users/doneill/.ansible/roles/chzerv.sysctl/tasks/main.yml': line 11, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Install required packages.\n  ^ here\n"}

PLAY RECAP ***********************************************************************************************************************************************
yyyy-10             : ok=42   changed=2    unreachable=0    failed=1    skipped=12   rescued=0    ignored=0
chzerv commented 3 years ago

I can't reproduce this when running on Vagrant VMs or on a VPS. Unless I'm missing something, the vars/default.yml file should be included for every distribution that is not Debian (and family), so I can't see why this wouldn't work..

Here is a snippet from the playbook used:

# ....
  tasks:
    - name: Sysctl tweaks.
      include_role:
        name: chzerv.sysctl
        apply:
          become: true

    - name: Print distribution and version.
      debug:
        msg: "{{ ansible_distribution }} {{ ansible_distribution_version }}"

    - name: Print 'sysctl_required_packages'
      debug:
        msg: "{{ sysctl_required_packages }}"

And the results:

TASK [ansible-role-sysctl : Install required packages.] **************************************************
ok: [x.x.x.x]

TASK [ansible-role-sysctl : Apply configuration.] ********************************************************
ok: [x.x.x.x] => (item=net.ipv4.ip_forward = 1)
ok: [x.x.x.x] => (item=kernel.kexec_load_disabled = 1)

TASK [Print distribution and version.] *******************************************************************
ok: [x.x.x.x] => {
    "msg": "CentOS 7.9"
}

TASK [Print 'sysctl_required_packages'] ******************************************************************
ok: [x.x.x.x] => {
    "msg": [
        "procps-ng"
    ]
}
chzerv commented 3 years ago

Please disregard my previous comment. This should be now fixed.

damianoneill commented 3 years ago

I have a pre-existing Redhat.yml, your with_first_found means this is loaded. Therefore default is never loaded, so the variable is missing. The easiest fix is to add a Redhat.yml alongside your Debian.yml with the redhat package.

chzerv commented 3 years ago

Is the fix I pushed not working or do you think there is a reason I should go back to using with_first_found and simply create a file for each distribution family?