hello云胜

技术与生活

0%

当我们需要修改内核参数时,可以使用sysctl 模块。

比如,我们在shell中会使用

1
echo 'vm.swappiness=1' >> /etc/sysctl.conf

来修改内核的swappiness参数,但是如果在ansible脚本中,直接这样写,会破坏幂等性。

每次执行ansible脚本都会添加一行配置,很丑。所以ansible提供了sysctl 模块。

sysctl 可以对值进行设置,如果需要查询可以使用shell 模块。

  • name:变量名
  • value:值
  • reload:文件被更新时,是否使用 sysctl -p reload 文件
  • state:是在文件中 移除(absent)或者设置(present)
  • sysctl_file:如果不是默认文件,指定其他文件
  • sysctl_set:使用sysctl 命令设置,不一定需要reload 文件

使用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Usage:
sysctl [options] [variable[=value] ...]

Options:
-a, --all display all variables
-A alias of -a
-X alias of -a
--deprecated include deprecated parameters to listing
-b, --binary print value without new line
-e, --ignore ignore unknown variables errors
-N, --names print variable names without values
-n, --values print only values of a variables
-p, --load[=<file>] read values from file
-f alias of -p
--system read values from all system directories
-r, --pattern <expression>
select setting that match expression
-q, --quiet do not echo variable set
-w, --write enable writing a value to variable
-o does nothing
-x does nothing
-d alias of -h

-h, --help display this help and exit
-V, --version output version information and exit

For more details see sysctl(8).

ansible-doc sysctl 文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
> SYSCTL    (/usr/lib/python2.7/site-packages/ansible/modules/system/sysctl.py)

This module manipulates sysctl entries and optionally performs a `/sbin/sysctl -p' after changing them.

OPTIONS (= is mandatory):

- ignoreerrors
Use this option to ignore errors about unknown keys.
(Choices: yes, no)[Default: False]

= name
The dot-separated path (aka `key') specifying the sysctl variable.
(Aliases: key)[Default: None]

- reload
If `yes', performs a `/sbin/sysctl -p' if the `sysctl_file' is updated. If `no', does not reload `sysctl' even if the
`sysctl_file' is updated.
(Choices: yes, no)[Default: yes]

- state
Whether the entry should be present or absent in the sysctl file.
(Choices: present, absent)[Default: present]

- sysctl_file
Specifies the absolute path to `sysctl.conf', if not `/etc/sysctl.conf'.
[Default: /etc/sysctl.conf]

- sysctl_set
Verify token value with the sysctl command and set with -w if necessary
(Choices: yes, no)[Default: False]
version_added: 1.5


- value
Desired value of the sysctl key.
(Aliases: val)[Default: None]


AUTHOR: David CHANIAL (@davixx) <david.chanial@gmail.com>
METADATA:
status:
- stableinterface
supported_by: core

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
EXAMPLES:
# Set vm.swappiness to 5 in /etc/sysctl.conf
- sysctl:
name: vm.swappiness
value: 5
state: present

# Remove kernel.panic entry from /etc/sysctl.conf
- sysctl:
name: kernel.panic
state: absent
sysctl_file: /etc/sysctl.conf

# Set kernel.panic to 3 in /tmp/test_sysctl.conf
- sysctl:
name: kernel.panic
value: 3
sysctl_file: /tmp/test_sysctl.conf
reload: no

# Set ip forwarding on in /proc and do not reload the sysctl file
- sysctl:
name: net.ipv4.ip_forward
value: 1
sysctl_set: yes
# Set ip forwarding on in /proc and in the sysctl file and reload if necessary
- sysctl:
name: net.ipv4.ip_forward
value: 1
sysctl_set: yes
state: present
reload: yes