Ansible Common Modules
Modules included in this section:
(1) Timing task module cronvar
(2) archive module
(3) Unpacking module unarchive
(4) Download module get_url
(5) wait_for module
(6) script module
Timed Task Module cronvar
In addition to the cron module itself, which can manage cron's environment variables, another module, cronvar, can also define environment variables for timed tasks.
cronvar--Ansible official usage documentation
ansible-doc -s cronvar - name: Manage variables in crontabs cronvar: backup: # (yes/no) If set, these files will be backed up before modifying the remote cron_file cron_file: # Customize the filename of cron_file, using relative paths to represent in/etc/cron.d state: # Presents create variables, absent s remove variables user: # Specifies which user's crontab will be modified, defaulting to root value: # The value of the environment variable, requiring state=present
(1) For example, create a job, synchronize every 2 minutes, and customize cron_file.
ansible test -m cron -a 'name="ntpdate" job="/usr/sbin/ntpdate ntp1.aliyun.com" cron_file=ntpdate_cron user=root minute=*/2' -o -f 8
Verify that you added it correctly.
ansible test -m shell -a 'cat /etc/cron.d/ntpdate_cron'
192.168.246.187 | CHANGED | rc=0 >> #Ansible: ntpdate */2 * * * * root /usr/sbin/ntpdate ntp1.aliyun.com
(2) Remove a job, requiring the name to match.If necessary, both cron_file and user need to be specified.
Ansible Test-M cron-a'name="ntpdate" state=absent cron_file=ntpdate_cron user=root'-o #/etc/cron.d/ntpdate_cron This file still exists (empty content)
archive module
Used to compress files remotely.Of course, the precondition is to have a corresponding compression tool on the remote host.Supports zip/gz/tar/bz2.
Archve--Ansible official usage documentation
ansible-doc -s archive - name: Creates a compressed archive of one or more files or trees archive: dest: # Target archive file name.The dest option is required unless path specifies that a single file is to be compressed format: # Specify compression format, default to gz format group: # The group to which the file/directory belongs owner: # Owner of file/directory mode: # Set file/directory permissions to support formats such as'0644'o r'u+rwx' o r'u=rw,g=r,o=r' path: # The file to be compressed can be an absolute path, a glob-specific path, or a list of files remove: # Delete source files after compression
Examples are as follows:
(1) Compress the file into the default gz format. Since path specifies that the file is to be compressed as a single file, dest is not used:
ansible test -m archive -a 'path="/tmp/mu.txt"'
Description: GZ suffix compression package decompression command: gzip-d mu.txt.gz
(2) Compress directory/path/to/foo/to/path/to/foo.tgz:
ansible test -m archive -a 'path="/tmp/xyz" dest=/tmp/xyz.tgz'
(3) Compress single file/path/to/foo into zip format:
ansible test -m archive -a 'path="/tmp/mu.txt" format=zip'
Description: Decompression command for zip suffix compression package: unzip mu.txt.zip
(4) Compress the given list of files into bz2 format with a compressed package path of/path/file.tar.bz2:
- name: Create a bz2 archive of multiple files, rooted at /path archive: path: - /path/to/foo - /path/wong/foo dest: /path/file.tar.bz2 format: bz2
Unpack module unarchive
By default, the archive files on the ansible side are copied to the controlled host and unpacked on the controlled host.If the option remote_src=yes is set, it means unpacking the archive files on the controlled host.
Requires corresponding unpacking commands on the controlled host.The unzip command is used to unzip the'.zip'file, and the gtar (provided by the tar package) command is used to unzip'.tar','.tar.gz','.tar.bz 2' and'.tar.xz'.
unarchive--Ansible official usage documentation
ansible-doc -s unarchive - name: Unpacks an archive after (optionally) copying it from the local machine. unarchive: creates: # Do not perform the task if the specified file exists.Can be used to achieve idempotency dest: # An archive file on a remote machine that needs to be unpacked, requiring an absolute path exclude: # List directories and files that you want to ignore during unpacking group: # The group to which the file/directory belongs owner: # Owner of file/directory mode: # Set file/directory permissions to support formats such as'0644'o r'u+rwx' o r'u=rw,g=r,o=r' keep_newer: # During unpacking, if a file with the same name exists in the destination path and in the package and is newer than the file in the package, the new file is retained list_files: # When set to true, a list of files in the archive file is returned remote_src: # Setting to yes indicates that the target archive file already exists on the remote host, that is, the archive file is no longer copied locally to the remote end and is unpacked directly at the remote end. # Default to no src: # If remote_src=no, the local archive file will be copied to the remote, either relative or absolute. # If remote_src=yes, the remote existing archive file will be unpacked # If remote_src=yes and Src contains'://', command the remote host to download and unpack files from the url
Download module get_url
get_url--Ansible official usage documentation
ansible-doc -s get_url - name: Downloads files from HTTP, HTTPS, or FTP to node get_url: backup: # Create a backup file with a time stamp in the name when downloading the file dest: # File save path, must be absolute path. # If dest is a directory, use base name of url as file name # The'force'option does not work if dest is a directory # If dest is a directory, the target file will always be downloaded, but the old file will only be replaced if the existing file changes force: # If set to yes and dest is not a directory, files are always downloaded, but old files are replaced only if existing files have changed # If set to no (default), the file will only be downloaded if it does not exist in the directory path. tmp_dest: # Temporarily store directory during download and delete downloaded temporary files before task execution is complete group: # The group to which the file/directory belongs owner: # Owner of file/directory mode: # Set file/directory permissions to support formats such as'0644'o r'u+rwx' o r'u=rw,g=r,o=r' timeout: # Timeout when url is requested, default 10 seconds url: # url path to download, (http|https|ftp): //[user[:pass]@host.domain[:port]/path # Paths in file format are also supported for replication.file:///path/to/file
Note that when dest is a directory or force=yes, files are always downloaded to a temporary storage directory, but the old files are not necessarily replaced.Only if force=no (default) and dest is a file, the file will not be downloaded if it already exists.
Examples are as follows:
(1) Download foo.conf, if / etc/foo.conf already exists, do not download it:
- name: Download foo.conf get_url: url: http://example.com/path/file.conf dest: /etc/foo.conf mode: '0440'
wait_for module
Sometimes tasks depend on resources such as status, files, ports, etc. The task will continue only if the prerequisite is met.The wait_for module is used to determine what conditions a task will continue to meet.Mainly used to determine whether the port is open, whether the file exists, whether there are some strings in the file.
wait_for--Ansible official usage documentation
ansible-doc -s wait_for - name: Waits for a condition before continuing wait_for: delay: # Number of seconds to wait before the check operation takes place host: # Wait for this host to be started, defaulting to 127.0.0.1 port: # Wait for this port to open path: # Does this file already exist search_regex: # Regular Matching in Files state: # Present/start/stop/absent/drained.Default start # When checking a port: # Start: Make sure the port is open # stopped: Make sure the port is closed # When checking a file: # Present/start: Continue after checking that the file exists # absent: Check to continue until the file is removed sleep: # Seconds of sleep between checks, default 1 second timeout: # Check wait timeout (seconds, default 300)
Examples are as follows:
(1) Check if port 8000 is open 10 seconds after connecting to the host, or time out if port 8000 is not open within 300 seconds (default).
- name: Wait for port 8000 to become open on the host, don't start checking for 10 seconds wait_for: port: 8000 delay: 10
(2) Do not continue until / tmp/foo file exists
- name: Wait until the file /tmp/foo is present before continuing wait_for: path: /tmp/foo
(3) Do not continue until a "completed" string can be matched in the /tmp/foo file
- name: Wait until the string "completed" is in the file /tmp/foo before continuing wait_for: path: /tmp/foo search_regex: completed
(4) Continue until the lock file / var/lock/file.lock is removed
- name: Wait until the lock file is removed wait_for: path: /var/lock/file.lock state: absent
(5) Continue until the / proc/3466/status file is removed to determine whether the process is started or stopped, whether the pid file exists or is removed, etc.
- name: Wait until the process is finished and pid was destroyed wait_for: path: /proc/3466/status state: absent
script module
The script module is used to control the remote host to execute scripts.Before executing the script, ansible transfers the local script to the remote host and executes it.When executing the script, it uses the shell environment on the remote host.
script--Ansible official usage documentation
ansible-doc -s script - name: Runs a local script on a remote node after transferring it script: chdir: # Switch to this directory before remotely executing the script. creates: # When this file exists, the script is not executed.It can be used to realize idempotency. removes: # Do not execute scripts when this file does not exist.It can be used to realize idempotency. free_form: # Local script path, options, parameters to execute.free_form is called because it is a script name + option + parameter.
Explain
This blog is for reference Maroon handsome man Article collation generated, belongs to the blogger reading notes, if there is infringement, please contact me, delete!
Finally, thank Open Source and embrace Open Source ~