1

考虑一下:

我有一个禁用 ipv6 的 RHEL8 服务器。我正在运行一些用于安全合规性的 ansible 脚本。

Ansible 检查如下所示:

- name: "SCORED | 3.1.1 | PATCH | Ensure IP forwarding is disabled"
  block:
  - name: "SCORED | 3.1.1 | PATCH | Ensure IP forwarding is disabled | Disable IPv4 forwarding"
    sysctl:
        name: '{{ item.name }}'
        value: '{{ item.value }}'
        state: present
        reload: yes
        ignoreerrors: yes
    with_items:
        - { name: net.ipv4.ip_forward, value: 0 }
        - { name: net.ipv4.route.flush, value: 1}

    notify:
        - sysctl flush ipv4 route table

  - name: "SCORED | 3.1.1 | PATCH | Ensure IP forwarding is disabled | Disable IPv6 forwarding"
    sysctl:
        name: '{{ item.name }}'
        value: '{{ item.value }}'
        state: present
        reload: yes
        ignoreerrors: yes
    with_items:
        - { name: net.ipv6.conf.all.forwarding, value: 0 }
        - { name: net.ipv6.route.flush, value: 1}
    when:
        - rhel8cis_ipv6_required
    notify:
        - sysctl flush ipv6 route table
  when:
    - not rhel8cis_is_router
    - rhel8cis_rule_3_1_1
  tags:
    - level1
    - sysctl
    - patch
    - rule_3.1.1

多变的

rhel8cis_ipv6_required 设置为 false。

被调用的处理程序如下所示:

- name: sysctl flush ipv4 route table
  become: yes
  sysctl:
    name: net.ipv4.route.flush
    value: 1
    sysctl_set: yes
  when: ansible_virtualization_type != "docker"

- name: sysctl flush ipv6 route table
  become: yes
  sysctl:
    name: net.ipv6.route.flush
    value: 1
    sysctl_set: yes
  when: ansible_virtualization_type != "docker"

playbook 的输出如下所示:

任务 [RHEL8_CIS:得分 | 3.1.1 | 补丁 | 确保禁用 IP 转发 | 禁用 IPv4 转发] ************************************************ ****************************************************** ****************************************** 好的:[alrha001.acc.vlkintern.nl] = > (item={'name': 'net.ipv4.ip_forward', 'value': 0}) 更改:[alrha001.acc.vlkintern.nl] => (item={'name': 'net.ipv4. route.flush','值':1})

任务 [RHEL8_CIS:得分 | 3.1.1 | 补丁 | 确保禁用 IP 转发 | 禁用 IPv6 转发] ********************************************** ****************************************************** ****************************************** 跳过:[alrha001.acc.vlkintern.nl] = > (item={'name': 'net.ipv6.conf.all.forwarding', 'value': 0}) 跳过:[alrha001.acc.vlkintern.nl] => (item={'name':' net.ipv6.route.flush','值':1})

RUNNING HANDLER [RHEL8_CIS : sysctl flush ipv4 route table] *************************************** ****************************************************** ****************************************************** ****************************************** [警告]:值 1(类型 int)字符串字段被转换为“1”(字符串类型)。如果这看起来不像您所期望的,请引用整个值以确保它不会改变。

致命:[alrha001.acc.vlkintern.nl]:失败!=> {"changed": false, "msg": "重新加载 sysctl 失败:fs.suid_dumpable = 0\nkernel.randomize_va_space = 2\nnet.ipv4.conf.all.forwarding = 0\nnet.ipv4.conf.all .send_redirects = 0\nnet.ipv4.conf.default.send_redirects = 0\nnet.ipv4.conf.all.accept_source_route = 0\nnet.ipv4.conf.default.accept_source_route = 0\nnet.ipv4.conf.all.accept_redirects = 0\nnet.ipv4.conf.default.accept_redirects = 0\nnet.ipv4.conf.all.secure_redirects = 0\nnet.ipv4.conf.default.secure_redirects = 0\nnet.ipv4.conf.all.log_martians = 1 \nnet.ipv4.conf.default.log_martians = 1\nnet.ipv4.icmp_echo_ignore_broadcasts = 1\nnet.ipv4.icmp_ignore_bogus_error_responses = 1\nnet.ipv4.conf.all.rp_filter = 1\nnet.ipv4.conf.default.rp_filter = 1\nnet.ipv4.tcp_syncookies = 1\nnet.ipv4.route.flush = 1\nnet.ipv4。

这就是我完全困惑的地方。如您所见,执行了 ipv4 的处理程序。致命输出中显示的所有变量都与 ipv4 相关。然而,最后,处理程序抱怨它找不到 ipv6 文件。这是正确的,因为在此服务器上未启用 ipv6。

这里有什么问题?

4

2 回答 2

0

据我所知,您要求修改 IPv6 设置;但是,因为您没有启用 IPv6,所以这些设置不存在。看一下 IPv6 刷新语句:

- name: sysctl flush ipv6 route table
  become: yes
  sysctl:
    name: net.ipv6.route.flush
    value: 1
    sysctl_set: yes
  when: ansible_virtualization_type != "docker"

在后台,通过使用procfssysctl中的特殊文件执行此操作。除非启用 IPv6,否则/proc/sys/net/ipv6/route/flush此文件以及 中的所有内容均不存在。/proc/sys/net/ipv6/我认为您的其他 IPv6 语句由于同样的原因而失败,它们试图访问/proc/sys/net/ipv6/不存在的参数。

于 2020-12-03T15:24:55.877 回答
0

您的 中可能有一个 ipv6 密钥/etc/sysctl.conf,可能是在此修复之前,或者是由它更早引起的。这通常会被忽略,但由于多种因素,它碰巧在这里导致失败。

默认情况下, ansiblesysctl模块总是尝试更新磁盘上的 sysctl 文件/etc/sysctl.conf。它没有临时更改的规定。在这里,处理程序试图进行时间点更改(路由刷新),但通过将此更新写入/etc/sysctl.conf. 永久更改会导致文件重新加载(因为reload:默认为yes),sysctl然后模块会失败,因为它对每个键的工作都非常严格。所以它正在检测您不相关的 ipv6 密钥并失败。

简而言之,这是一个处理程序错误。更改处理程序以sysctl直接使用该命令,它应该可以工作。

- name: sysctl flush ipv4 route
  command: sysctl -w net.ipv4.route.flush=1
  ...

也可以ignoreerrors: yessysctl模块一起使用,以便处理程序不会崩溃,甚至reload: no. 但这仍然使更改永久化,因此正确的解决方案是手动发出 sysctl。事实上,Red Hat明确表示您不能在/etc/sysctl.conf. (不幸的是,链接在他们的付费墙后面。)

出于同样的原因,您可能需要设置ignoreerrors: yes是否曾经编写过实际需要修改的剧本/etc/sysctl.conf,以及其中是否有无效的密钥。

于 2021-05-20T19:05:47.283 回答