In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Common modules of Ansible
The modules included in this section:
(1) timing task module cronvar
(2) Archiving module archive
(3) unpacking module unarchive
(4) download module get_url
(5) wait_for module
(6) script module
Timing task module cronvar
In addition to the cron module itself, which can manage the environment variables of cron, another module, cronvar, can also define environment variables for scheduled tasks.
Cronvar--Ansible official instruction document
Ansible-doc-s cronvar- name: Manage variables in crontabs cronvar: backup: # (yes/no) if set, the file names of these files cron_file: # custom cron_file are backed up before the remote cron_file is modified, and the relative path means that state: # present is used to create variables in / etc/cron.d Absent is used to remove the variable user: # to specify which user's crontab will be modified, which defaults to the value of the root value: # environment variable and requires state=present
(1) for example: create a job, synchronize time every 2 minutes, and customize the 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 the addition is correct.
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 that the name must match. If necessary, specify both cron_file and user.
The file ansible test-m cron-a 'name= "ntpdate" state=absent cron_file=ntpdate_cron user=root'-o # / etc/cron.d/ntpdate_cron still exists (empty content)
Archiving module archive
Used to compress files at the remote end. Of course, the premise is that there is a corresponding compression tool on the remote host. Zip/gz/tar/bz2 is supported.
Archive--Ansible official instruction document
Ansible-doc-s archive- name: Creates a compressed archive of one or more files or trees archive: dest: # destination archive filename. Unless path specifies a single file to be compressed, the dest option format: # is required to specify the compression format, which defaults to the gz format group: # file / directory group owner: # file / directory owner mode: # sets the permissions of the file / directory Supports formats such as' 0644'or 'uprirwx' or 'uprirw.gcompresrdirection / glob' format path: # the file to be compressed can be an absolute path, a path configured by glob, or a file list remove: # delete the source file after compression
Examples are as follows:
(1) compress the file to the default gz format. Since path specifies a single file to be compressed, you can not use dest:
Ansible test-m archive-a 'path= "/ tmp/mu.txt"'
Explain the decompression command of ⚠️: gz suffix compression package: gzip-d mu.txt.gz
(2) compress the directory / path/to/foo/ to / path/to/foo.tgz:
Ansible test-m archive-a 'path= "/ tmp/xyz" dest=/tmp/xyz.tgz'
(3) compress the single file / path/to/foo to zip format:
Ansible test-m archive-a 'path= "/ tmp/mu.txt" format=zip'
Explain the decompression command of ⚠️: zip suffix compression package: unzip mu.txt.zip
(4) compress the given file list into bz2 format, and the path of the package is / 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 unpacking module unarchive
By default, the archive file on the ansible side is copied to the controlled host, and then unpacked on the controlled host. If you set the option remote_src=yes, it means to unpack the archive file on the controlled host.
A corresponding unpacking command is required on the controlled host. The unzip command is used to extract the ".zip" file, and the gtar (provided by the tar package) command is used to extract ".tar", ".tar.gz", ".tar.bz 2" and ".tar.xz".
Unarchive--Ansible official instruction document
Ansible-doc-s unarchive- name: Unpacks an archive after (optionally) copying it from the local machine. Unarchive: creates: # if the specified file exists, the task is not performed. Can be used to implement idempotent dest: # archived files on remote machines that need to be unpacked The requirement is absolute path exclude: # list directories and files to be ignored during unpacking group: # Group of files / directories owner: # owner of files / directories mode: # set permissions for files / directories Supports formats such as' 0644'or 'uprirwx' or 'uprirwdagenrdirection' keep_newer: # during unpacking, if there is a file with the same name in the destination path as in the package and is newer than the file in the package, the new file list_files: # is retained when it is set to true Setting the list of files in the returned archive file remote_src: # to yes means that there is already a target archive file on the remote host, that is, the archive file is no longer copied locally to the remote end, but is unpackaged directly at the remote end. # default is no src: # if remote_src=no, the local archive file will be copied to the remote end, but the relative path can also be absolute path. # if remote_src=yes, unpack the remote existing archive file # if remote_src=yes and the src contains ": / /", the remote host will be directed to download the file from url and unpack the download module get_url
Get_url--Ansible official instruction document
Ansible-doc-s get_url- name: Downloads files from HTTP, HTTPS, or FTP to node get_url: backup: # create a backup file dest: # file save path with a timestamp in its name when downloading the file, which must be an absolute path. # if dest is a directory, use url's base name as the file name # if dest is a directory, the 'force' option does not take effect # if dest is a directory, the target file will always be downloaded, but the old file will only be replaced if the existing file has changed. Force: # if set to yes and dest is not a directory The file will always be downloaded, but the old file will be replaced only if the existing file has changed # if set to no (default), the file will be downloaded only if the file does not exist in the directory path. Tmp_dest: # temporarily stores the directory when downloading, and deletes the downloaded temporary file group: # file / directory group owner: # file / directory owner mode: # sets the permissions of the file / directory before the task is completed The format timeout: # timeout when requesting url is supported, such as' 0644'or 'uprirwx' or 'upright rwx'. By default, 10 seconds url: # the url path to download, (http | https | ftp): / / [user [: pass]] @ host.domain [: port] / path # also supports the path in file format to achieve replication. File:///path/to/file
Note that when dest is a directory or force=yes, files are always downloaded to a temporary directory, but the old files are not necessarily replaced. Only if force=no (the default) and dest is a file, the file will not be downloaded when it already exists.
Examples are as follows:
(1) download foo.conf. If / etc/foo.conf already exists, the file will not be downloaded:
-name: Download foo.conf get_url: url: http://example.com/path/file.conf dest: / etc/foo.conf mode: '0440'wait_for module
Sometimes tasks are dependent on resources such as status, files, ports, and so on, and the task will continue only if the premise is met. The wait_for module is used to determine what conditions the task will continue under. It is mainly used to judge whether the port is open, whether the file exists, and whether there are some strings in the file.
Wait_for--Ansible official instruction document
Ansible-doc-s wait_for- name: Waits for a condition before continuing wait_for: delay: # number of seconds to wait before checking operation host: # wait for this host to be started The default is 127.0.0.1 port: # wait for the port to be open path: # whether the file already exists search_regex: # regular matching state: # present/started/stopped/absent/drained in the file. Default started # when checking a port: # started: make sure the port is open # stopped: make sure the port is closed # when checking a file: # present/started: only when checking for the existence of a file Will continue # absent: check until the file has been removed before continuing sleep: # the number of seconds of sleep between checks Default 1 second timeout: # check wait timeout (seconds, default 300)
Examples are as follows:
(1) check whether port 8000 is open until 10 seconds after connecting to the host. If it is not open within 300 seconds (default), the timeout occurs.
-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) it will 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 the "completed" string is 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) do not 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) do not continue until the / proc/3466/status file is removed, which can be used 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: absentscript 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 before executing it. When executing the script, it uses the shell environment on the remote host.
Script--Ansible official instruction document
Ansible-doc-s script- name: Runs a local script on a remote node after transferring it script: chdir: # change to this directory before executing the script remotely. Creates: # when this file exists, no script is executed. Can be used to achieve idempotency. Removes: # when this file does not exist, the script is not executed. Can be used to achieve idempotency. Free_form: # local script paths, options, and parameters to be executed. It is called free_form because it is a script name + option + parameter. Description
This blog is a reference to Ma long Shuai boss article collation and generation, belongs to the blogger reading notes, if there is infringement, please contact me, delete!
Finally, thank open source, embrace open source ~
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.