Asible study notes - common module (c)

Ansible common module

This section contains a module:

(1)定时任务模块cronvar

(2)归档模块archive

(3)解包模块unarchive

(4)下载模块get_url

(5)wait_for模块

(6)script模块

Timing task module cronvar

In addition cron cron module itself can manage environment variables may be defined in another module cronvar environment variable timing of the task.

cronvar - Ansible official use of documentation

ansible-doc -s cronvar
- name: Manage variables in crontabs
  cronvar:
      backup:     # (yes/no)如果设置了,则会在修改远程cron_file前备份这些文件
      cron_file:  # 自定义cron_file的文件名,使用相对路径则表示在/etc/cron.d中
      state:      # present用于创建变量,absent用于移除变量
      user:       # 指定哪个用户的crontab将要被修改,默认为root
      value:      # 环境变量的值,要求state=present

(1) For example: to create a Job, once every 2 minutes for time synchronization, and a custom 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 added 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) removing a job, requires name must match. If necessary, you need to specify cron_file and user.

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 (is empty)

Archive module archive

At the distal end for compressing the files. Of course, the compression tool is provided on the distal end to have a corresponding host. Support zip / gz / tar / bz2.

archive - Ansible official use of documentation

ansible-doc -s archive
- name: Creates a compressed archive of one or more files or trees
  archive:
      dest:   # 目标归档文件名。除非path指定要压缩的是单文件,否则需要dest选项
      format: # 指定压缩格式,默认为gz格式
      group:  # 文件/目录的所属组
      owner:  # 文件/目录的所有者
      mode:   # 设置文件/目录的的权限,支持'0644'或'u+rwx'或'u=rw,g=r,o=r'等格式
      path:   # 要压缩的文件,可以是绝对路径,也可以是glob统配的路径,还可以是文件列表
      remove: # 压缩后删除源文件

Examples are as follows:

(1) compressed into a file format default gz, since the path specifies a single compressed file, it may not be used dest:

ansible test -m archive -a 'path="/tmp/mu.txt"'

Description ⚠️: gz suffix compression and decompression command packet:gzip -d mu.txt.gz

(2) the directory / path / to / foo / compressed /path/to/foo.tgz:

ansible test -m archive -a 'path="/tmp/xyz" dest=/tmp/xyz.tgz'

(3) a single file / path / to / foo zip the format:

ansible test -m archive -a 'path="/tmp/mu.txt" format=zip'

Description ⚠️: zip suffix compression and decompression command packet:unzip mu.txt.zip

(4) a list of files given bz2 compressed format, compressed path /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

Decapsulating module unarchive

The default archive copy ansible terminal charged to the host, and then unpacked on the controlled host. If you set the option remote_src = yes, it means unpack the archive file on the host charged.

It requires a corresponding command on the controlled unpacked host. command to decompress unzip ".zip" file, gtar (tar packet supplied) command is used to extract the ".tar", ".tar.gz", ". tar.bz 2" and ".tar.xz".

unarchive - Ansible official use of documentation

ansible-doc -s unarchive
- name: Unpacks an archive after (optionally) copying it from the local machine.
  unarchive:
      creates:         # 如果指定的文件存在则不执行该任务。可用于实现幂等性
      dest:            # 远程机器上需要被解包的归档文件,要求是绝对路径
      exclude:         # 列出解包过程中想要忽略的目录和文件
      group:           # 文件/目录的所属组
      owner:           # 文件/目录的所有者
      mode:            # 设置文件/目录的的权限,支持'0644'或'u+rwx'或'u=rw,g=r,o=r'等格式
      keep_newer:      # 在解包过程中,如果目标路径中和包中有同名文件,且比包中的文件更新,则保留新的文件
      list_files:      # 设置为true时,将返回归档文件中的文件列表
      remote_src:      # 设置为yes表示远程主机上已有目标归档文件,即不再从本地复制归档文件到远端,直接在远端解包。
                       # 默认为no
      src:             # 如果remote_src=no,将复制本地归档文件到远端,可相对路径也可绝对路径.
                       # 如果remote_src=yes, 将解包远程已存在的归档文件
                       # 如果remote_src=yes且src中包含了"://",将指挥远程主机从url中下载文件并解包

Download module get_url

get_url - Ansible official use of documentation

ansible-doc -s get_url
- name: Downloads files from HTTP, HTTPS, or FTP to node
  get_url:
      backup:     # 下载文件时同时创建一个名称中包含时间戳的备份文件
      dest:       # 文件保存路径,必须为绝对路径。
                  # 如果dest是一个目录,则使用url的base name作为文件名
                  # 如果dest是一个目录,则'force'选项不生效
                  # 如果dest是一个目录,则总是会下载目标文件,但只在已存在的文件变化了才会替换旧文件
      force:      # 如果设置为yes,且dest不是一个目录时,则总是会下载文件,但只在已存在的文件变化了才会替换旧文件
                  # 如果设置为no(默认),则只会在目录路径下不存在该文件时才会进行下载。
      tmp_dest:   # 下载时临时存放目录,在任务执行完成前会删除下载的临时文件
      group:      # 文件/目录的所属组
      owner:      # 文件/目录的所有者
      mode:       # 设置文件/目录的的权限,支持'0644'或'u+rwx'或'u=rw,g=r,o=r'等格式
      timeout:    # 请求url时的超时时间,默认10秒钟
      url:        # 要下载的url路径,(http|https|ftp)://[user[:pass]]@host.domain[:port]/path
                  # 还支持file格式的路径,实现复制功能。file:///path/to/file

Note, dest directory or force = yes, the always download the file to a temporary storage directory, but will not necessarily replace the old file. Only force = no (default) and dest is a file, the file is not downloaded if the file exists.

Examples are as follows:

(1) download foo.conf, if /etc/foo.conf already exists, not the file download:

- name: Download foo.conf
  get_url:
    url: http://example.com/path/file.conf
    dest: /etc/foo.conf
    mode: '0440'

wait_for module

Some time between tasks of state resources, documents, and other port is a dependent relationship, only to meet the premise, the task will continue. wait_for task module is used to determine what conditions are met in the case will continue. Primarily used to determine whether to open ports, the file exists, the existence of certain strings in the file.

wait_for - Ansible official use of documentation

ansible-doc -s wait_for
- name: Waits for a condition before continuing
  wait_for:
      delay:          # 在检查操作进行之前等待的秒数
      host:           # 等待这个主机处于启动状态,默认为127.0.0.1
      port:           # 等待这个端口已经开放
      path:           # 这个文件是否已经存在
      search_regex:   # 在文件中进行正则匹配
      state:          # present/started/stopped/absent/drained.默认started
                      # 当检查的是一个端口时:
                      # started:保证端口是开放的
                      # stopped:保证端口是关闭的
                      # 当检查的是一个文件时:
                      # present/started:在检查到文件存在才会继续
                      # absent:检查到文件被移除后才会继续
      sleep:          # 两次检查之间sleep的秒数,默认1秒
      timeout:        # 检查的等待超时时间(秒数,默认300)

Examples are as follows:

Not open within a timeout (1) connected to the host after 10 seconds to check whether the port 8000 is in an open state, 300 seconds (the default value).

- 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) until the / tmp / foo file will continue to exist

- name: Wait until the file /tmp/foo is present before continuing
  wait_for:
    path: /tmp/foo

(3) until the / tmp / foo file to match "completed" before continuing string

- name: Wait until the string "completed" is in the file /tmp/foo before continuing
  wait_for:
    path: /tmp/foo
    search_regex: completed

(4) until /var/lock/file.lock the lock file is removed before continuing

- name: Wait until the lock file is removed
  wait_for:
    path: /var/lock/file.lock
    state: absent

(5) until the / proc / 3466 / status file to be removed before continuing, the process can be used to determine whether to start or stop, pid file is present 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

script control module for the remote host to execute the script. Before executing the script, ansible local script will be transferred to a remote host, and then execute. In the implementation of the script, which uses a shell environment on a remote host.

script - Ansible official use of documentation

ansible-doc -s script
- name: Runs a local script on a remote node after transferring it
  script:
      chdir:       # 在远程执行脚本前先切换到此目录下。
      creates:     # 当此文件存在时,不执行脚本。可用于实现幂等性。
      removes:     # 当此文件不存在时,不执行脚本。可用于实现幂等性。
      free_form:   # 本地待执行的脚本路径、选项、参数。之所以称为free_form,是因为它是脚本名+选项+参数。

Explanation

Bowen is a reference Malone handsome big brother article finishing generation, belonging to the blogger study notes, if infringement, please contact me big brother, Li deleted!

Finally, thanks to open source, open source embrace ~

Guess you like

Origin blog.51cto.com/wutengfei/2450909