community.general.ini_file 模块 – 调整 INI 文件中的设置
注意
此模块是 community.general 集合(版本 10.1.0)的一部分。
如果您正在使用 ansible
包,您可能已经安装了此集合。它不包含在 ansible-core
中。要检查是否已安装,请运行 ansible-galaxy collection list
。
要安装它,请使用:ansible-galaxy collection install community.general
。
要在 playbook 中使用它,请指定:community.general.ini_file
。
概要
管理(添加、删除、更改)INI 风格文件中的单个设置,而无需使用 ansible.builtin.template 或 ansible.builtin.assemble 等模块管理整个文件。
如果缺少节,则添加它们。
此模块会向文件添加缺少的结尾换行符,以符合 POSIX 标准,即使不需要应用其他修改。
参数
参数 |
注释 |
---|---|
允许没有值且没有“=”符号的选项。 选择
|
|
结果文件系统对象应具有的属性。 要获取支持的标志,请查看目标系统上 此字符串应包含与 默认情况下假定为 |
|
创建一个包含时间戳信息的备份文件,这样如果以某种方式错误地破坏了它,您可以恢复原始文件。 选择
|
|
如果设置为 默认情况下,如果文件丢失,它将创建该文件。 选择
|
|
如果设置为 如果设置为 选择
|
|
应拥有文件系统对象的所有者的组的名称,就像提供给 如果未指定,它将使用当前用户的当前组,除非您是 root 用户,在这种情况下,它可以保留以前的所有权。 |
|
如果这样做只会添加或删除 选择
|
|
结果文件系统对象应具有的权限。 对于那些习惯使用 如果给 Ansible 一个数字,而没有遵循上述任何规则,最终将会得到一个十进制数,这会产生意想不到的结果。 从 Ansible 1.8 开始,模式可以指定为符号模式(例如, 如果未指定 如果未指定 指定 |
|
默认情况下,此模块会替换与给定选项匹配的注释行。 将此选项设置为 选择
|
|
请勿在“=”符号前后插入空格。 选择
|
|
应该拥有文件系统对象的用户的名称,如同提供给 如果未指定,则使用当前用户,除非您是 root 用户,在这种情况下,它可以保留先前的所有权。 指定数字用户名将被视为用户 ID 而不是用户名。避免使用数字用户名以避免这种混淆。 |
|
INI 样式文件的路径;如果需要,将创建此文件。 |
|
INI 文件中的节名称。当设置单个值时,如果 如果省略,则 如果配置格式不支持节,则也需要省略 |
|
在可能具有相同名称的多个节中,选择第一个包含匹配选项和值的节。 如果 如果 |
|
匹配的 |
|
匹配的 |
|
SELinux 文件系统对象上下文的级别部分。 这是 MLS/MCS 属性,有时称为 当设置为 |
|
SELinux 文件系统对象上下文的角色部分。 当设置为 |
|
SELinux 文件系统对象上下文的类型部分。 当设置为 |
|
SELinux 文件系统对象上下文的用户部分。 默认情况下,它使用 当设置为 |
|
如果设置为 如果设置为 如果设置为 如果设置为 选择
|
|
影响何时使用原子操作来防止数据损坏或从目标文件系统对象读取不一致的数据。 默认情况下,此模块使用原子操作来防止数据损坏或从目标文件系统对象读取不一致的数据,但有时系统配置或损坏的方式会阻止这样做。一个例子是 Docker 挂载的文件系统对象,它们无法从容器内部原子更新,只能以不安全的方式写入。 此选项允许 Ansible 在原子操作失败时回退到不安全的文件系统对象更新方法(但是,它不会强制 Ansible 执行不安全的写入)。 重要提示!不安全的写入会受到竞争条件的影响,并可能导致数据损坏。 选择
|
|
属性
属性 |
支持 |
描述 |
---|---|---|
支持:完全 |
可以在 |
|
支持:完全 |
在 diff 模式下,将返回已更改内容(或在 |
注释
示例
- name: Ensure "fav=lemonade is in section "[drinks]" in specified file
community.general.ini_file:
path: /etc/conf
section: drinks
option: fav
value: lemonade
mode: '0600'
backup: true
- name: Ensure "temperature=cold is in section "[drinks]" in specified file
community.general.ini_file:
path: /etc/anotherconf
section: drinks
option: temperature
value: cold
backup: true
- name: Add "beverage=lemon juice" is in section "[drinks]" in specified file
community.general.ini_file:
path: /etc/conf
section: drinks
option: beverage
value: lemon juice
mode: '0600'
state: present
exclusive: false
- name: Ensure multiple values "beverage=coke" and "beverage=pepsi" are in section "[drinks]" in specified file
community.general.ini_file:
path: /etc/conf
section: drinks
option: beverage
values:
- coke
- pepsi
mode: '0600'
state: present
- name: Add "beverage=lemon juice" outside a section in specified file
community.general.ini_file:
path: /etc/conf
option: beverage
value: lemon juice
state: present
- name: Remove the peer configuration for 10.128.0.11/32
community.general.ini_file:
path: /etc/wireguard/wg0.conf
section: Peer
section_has_values:
- option: AllowedIps
value: 10.128.0.11/32
mode: '0600'
state: absent
- name: Add "beverage=lemon juice" outside a section in specified file
community.general.ini_file:
path: /etc/conf
option: beverage
value: lemon juice
state: present
- name: Update the public key for peer 10.128.0.12/32
community.general.ini_file:
path: /etc/wireguard/wg0.conf
section: Peer
section_has_values:
- option: AllowedIps
value: 10.128.0.12/32
option: PublicKey
value: xxxxxxxxxxxxxxxxxxxx
mode: '0600'
state: present
- name: Remove the peer configuration for 10.128.0.11/32
community.general.ini_file:
path: /etc/wireguard/wg0.conf
section: Peer
section_has_values:
- option: AllowedIps
value: 10.4.0.11/32
mode: '0600'
state: absent
- name: Update the public key for peer 10.128.0.12/32
community.general.ini_file:
path: /etc/wireguard/wg0.conf
section: Peer
section_has_values:
- option: AllowedIps
value: 10.4.0.12/32
option: PublicKey
value: xxxxxxxxxxxxxxxxxxxx
mode: '0600'
state: present