# platform = multi_platform_all
# reboot = true
# strategy = disable
# complexity = low
# disruption = medium
- (xccdf-var sysctl_net_ipv4_tcp_invalid_ratelimit_value)


- name: Configure Kernel to Rate Limit Sending of Duplicate TCP Acknowledgments - Set fact for sysctl paths
  ansible.builtin.set_fact:

    sysctl_paths:
      - "/etc/sysctl.d/"
      - "/run/sysctl.d/"
      - "/usr/local/lib/sysctl.d/"



- name: Configure Kernel to Rate Limit Sending of Duplicate TCP Acknowledgments - Find all files that contain net.ipv4.tcp_invalid_ratelimit
  ansible.builtin.shell:
    cmd: find -L {{ sysctl_paths | join(" ") }} -type f -name '*.conf' | xargs grep -HP '^\s*net.ipv4.tcp_invalid_ratelimit\s*=\s*.*$'
  register: find_all_values
  check_mode: false
  changed_when: false
  failed_when: false

- name: Configure Kernel to Rate Limit Sending of Duplicate TCP Acknowledgments - Find all files that set net.ipv4.tcp_invalid_ratelimit to correct value
  ansible.builtin.shell:
    cmd: find -L {{ sysctl_paths | join(" ") }} -type f -name '*.conf' | xargs grep -HP '^\s*net.ipv4.tcp_invalid_ratelimit\s*=\s*{{ sysctl_net_ipv4_tcp_invalid_ratelimit_value }}$'
  register: find_correct_value
  check_mode: false
  changed_when: false
  failed_when: false

- name: Configure Kernel to Rate Limit Sending of Duplicate TCP Acknowledgments - Comment out any occurrences of net.ipv4.tcp_invalid_ratelimit from config files
  ansible.builtin.replace:
    path: '{{ item | split(":") | first }}'
    regexp: '^[\s]*net.ipv4.tcp_invalid_ratelimit'
    replace: '#net.ipv4.tcp_invalid_ratelimit'
  loop: '{{ find_all_values.stdout_lines }}'
  when: find_correct_value.stdout_lines | length == 0 or find_all_values.stdout_lines | length > find_correct_value.stdout_lines | length


- name: Configure Kernel to Rate Limit Sending of Duplicate TCP Acknowledgments - Comment out any occurrences of net.ipv4.tcp_invalid_ratelimit from /etc/ufw/sysctl.conf
  ansible.builtin.replace:
    path: "/etc/ufw/sysctl.conf"
    regexp: '(^[\s]*net.ipv4.tcp_invalid_ratelimit.*$)'
    replace: '# \1'



- name: Configure Kernel to Rate Limit Sending of Duplicate TCP Acknowledgments - Ensure sysctl net.ipv4.tcp_invalid_ratelimit is set
  ansible.posix.sysctl:
    name: "net.ipv4.tcp_invalid_ratelimit"
    value: "{{ sysctl_net_ipv4_tcp_invalid_ratelimit_value }}"

    sysctl_file: "/etc/sysctl.conf"

    state: present
    reload: yes
