Ansible可以使用lineinfile
模块来在文件中添加一行。以下是一个示例代码:
- name: Add a line to a file
hosts: localhost
tasks:
- name: Add a line to a file
lineinfile:
path: /path/to/file
line: 'This is the line to be added'
insertafter: EOF
解释:
path
参数指定要操作的文件的路径。line
参数指定要添加的行内容。insertafter
参数指定在哪一行之后插入新行。在上面的示例中,使用EOF
表示在文件的末尾插入新行。请注意,上述示例中的hosts
字段设置为localhost
,这意味着该任务将在本地执行。您可以根据自己的需要更改为适当的主机组。
使用此示例代码,您可以在Ansible中将一行添加到文件中。