Ansible 的 lineinfile 模块用于在文件中添加、修改或删除特定行。这对于在配置文件中添加或修改特定配置项非常有用。以下是 lineinfile 模块的一些常见用法和选项
Parameter | Comments |
---|---|
path required |
The file to modify.Before Ansible 2.3 this option was only usable as [dest] , [destfile] and [name] . |
line aliases: valuestring | The line to insert/replace into the file.Required for state=present . |
backup boolean | Create a backup file including the timestamp information so you can get the original file back if you somehow clobbered it incorrectly. Default choice is false |
create boolean | Used with state=present .If specified, the file will be created if it does not already exist. Default choice is false |
regexp aliases: regexstring | The regular expression to look for in every line of the file. For state=present , the pattern to replace if found. Only the last line found will be replaced.For state=absent , the pattern of the line(s) to remove.If the regular expression is not matched, the line will be added to the file in keeping with [insertbefore] or [insertafter] settings.When modifying a line the regexp should typically match both the initial state of the line as well as its state after replacement by [line] to ensure idempotence.Uses Python regular expressions. |
insertafter string | Used with state=present .If specified, the line will be inserted after the last match of specified regular expression. If the first match is required, use(firstmatch=yes).A special value is available; EOF for inserting the line at the end of the file.If specified regular expression has no matches, EOF will be used instead.If [insertbefore] is set, default value EOF will be ignored.Choices:"EOF" ← (default)regex" |
insertbefore string | Used with state=present .If specified, the line will be inserted before the last match of specified regular expression. If the first match is required, use firstmatch=yes .A value is available; BOF for inserting the line at the beginning of the file.If specified regular expression has no matches, the line will be inserted at the end of the file.Choices: "BOF"``"regex" |
state string | Whether the line should be there or not. **Choices: **absent ,present ← (default) |
firstmatch boolean | Used with [insertafter] or [insertbefore] .If set, [insertafter] and [insertbefore] will work with the first line that matches the given regular expression. Default choice is false |
group string | Name of the group that should own the filesystem object, as would be fed to chown.When left unspecified, it uses the current group of the current user unless you are root, in which case it can preserve the previous ownership. |
owner string | Name of the user that should own the filesystem object, as would be fed to chown.When left unspecified, it uses the current user unless you are root, in which case it can preserve the previous ownership.Specifying a numeric username will be assumed to be a user ID and not a username. Avoid numeric usernames to avoid this confusion. |
mode any | The permissions the resulting filesystem object should have.For those used to /usr/bin/chmod remember that modes are actually octal numbers. You must give Ansible enough information to parse them correctly. For consistent results, quote octal numbers (for example, '644' or '1777' ) so Ansible receives a string and can do its own conversion from string into number. |
search_string stringadded in ansible-core 2.11 | The literal string to look for in every line of the file. This does not have to match the entire line. For state=present , the line to replace if the string is found in the file. Only the last line found will be replaced.For state=absent , the line(s) to remove if the string is in the line.If the literal expression is not matched, the line will be added to the file in keeping with [insertbefore] or [insertafter] settings.Mutually exclusive with [backrefs] and [regexp] . |
示例
添加一行到文件中:
1 | - name: Add a line to a file |
确保特定文本存在于文件中:
1 | - name: Ensure a line is in the file |
在特定行之前或之后添加文本:
1 | - name: Add a line before or after a matching line |
修改文件中的特定行:
1 | - name: Change a line in the file |
删除文件中的特定行:
1 | - name: Remove a line from the file |
这些是 lineinfile 模块的一些常见使用方法。该模块允许您对文件进行精确的行级操作,非常适合用于自动化配置文件的管理。
从提供的url中下载文件
Parameter | Comments |
---|---|
url string / required | HTTP, HTTPS, or FTP URL in the form (http|https|ftp)://[user[:pass]]@host.domain[:port]/path |
dest path / required | Absolute path of where to download the file to. If [dest] is a directory, either the server provided filename or, if none provided, the base name of the URL on the remote server will be used.If a directory, [force] has no effect.If [dest] is a directory, the file will always be downloaded (regardless of the [force] and [checksum] option), but replaced only if the contents changed. |
backup boolean | Create a backup file including the timestamp information so you can get the original file back if you somehow clobbered it incorrectly. Default choice is false |
force boolean | If true and [dest] is not a directory, will download the file every time and replace the file if the contents change.If false , the file will only be downloaded if the destination does not exist. Generally should be true only for small local files. Default choice is false |
decompress boolean | Whether to attempt to decompress gzip content-encoded responses. Default choice is true |
owner string | Name of the user that should own the filesystem object, as would be fed to chown.When left unspecified, it uses the current user unless you are root, in which case it can preserve the previous ownership. |
group string | Name of the group that should own the filesystem object, as would be fed to chown.When left unspecified, it uses the current group of the current user unless you are root, in which case it can preserve the previous ownership. |
mode any | The permissions the resulting filesystem object should have.For those used to /usr/bin/chmod remember that modes are actually octal numbers. You must give Ansible enough information to parse them correctly. For consistent results, quote octal numbers (for example, '644' or '1777' ) so Ansible receives a string and can do its own conversion from string into number. |
url_password aliases: passwordstring | The password for use in HTTP basic authentication. |
url_username aliases: usernamestring | The username for use in HTTP basic authentication.This parameter can be used without [url_password] for sites that allow empty passwords. |