Home > System Tutorial > LINUX > body text

A brief introduction to common modules of ansible

WBOY
Release: 2024-09-02 13:36:00
Original
354 people have browsed it

A brief introduction to common modules of ansible

This article introduces commonly used modules. According to the official classification, the modules are classified by function into: cloud module, command module, database module, file module, asset module, message module, monitoring module, network module, notification module, package management module, source code control module, system module, Unit module, web facility module, windows module, please refer to the official page for details.

Here we select some of the most commonly used modules from the official categories for introduction (the commands module has been introduced in the previous article and will not be mentioned here).

1. Ping module

Test whether the host is connected. The usage is very simple and does not involve parameters:

[root@361way ~]# ansible 10.212.52.252 -m ping
10.212.52.252 | success >> {
"changed": false,
"ping": "pong"
}
Copy after login
2. Setup module

The setup module is mainly used to obtain host information. A parameter gather_facts that is often used in playbooks is related to this module. A parameter often used under the setup module is the filter parameter. The specific usage examples are as follows (due to the large number of output results, only the commands are listed here without the results):

[root@361way ~]# ansible 10.212.52.252 -m setup -a 'filter=ansible_*_mb' 
//查看主机内存信息
[root@361way ~]# ansible 10.212.52.252 -m setup -a 'filter=ansible_eth[0-2]' 
//查看地接口为eth0-2的网卡信息
[root@361way ~]# ansible all -m setup --tree /tmp/facts 
//将所有主机的信息输入到/tmp/facts目录下,每台主机的信息输入到主机名文件中(/etc/ansible/hosts里的主机名)
Copy after login
3. file module

The file module is mainly used for file operations on the remote host. The file module contains the following options:

  • force: You need to force the creation of a soft link in two situations, one is when the source file does not exist but will be created later; the other is when the target soft link already exists, you need to cancel the previous soft link first, and then Create a new soft link, there are two options: yes|no
  • group: defines the group of the file/directory
  • mode: Define the permissions of files/directories
  • owner: defines the owner of the file/directory
  • path: required option, defines the path of the file/directory
  • recurse: Set file attributes recursively, only valid for directories
  • src: The path of the source file to be linked, only applies to the case of state=link
  • dest: the path to be linked to, only applies to the case of state=link
  • state: directory: If the directory does not exist, create the directory
    file: Even if the file does not exist, it will not be created
    link: Create a soft link
    hard: Create a hard link
    touch: If the file does not exist, a new file will be created. If the file or directory already exists, its last modification time will be updated
    absent: delete directories, files or unlink files

Usage example:

ansible test -m file -a "src=/etc/fstab dest=/tmp/fstab state=link"
ansible test -m file -a "path=/tmp/fstab state=absent"
ansible test -m file -a "path=/tmp/test state=touch"
Copy after login
4. Copy module

Copy files to the remote host. The copy module contains the following options:

  • backup: Back up the original file before overwriting. The backup file contains time information. There are two options: yes|no
  • content: used to replace "src", you can directly set the value of the specified file
  • dest: required. The absolute path of the remote host to which the source file is to be copied. If the source file is a directory, the path must also be a directory
  • directory_mode: recursively set the permissions of the directory, the default is the system default permissions
  • force: If the target host contains the file, but the content is different, if set to yes, it will force overwriting, if set to no, it will only copy if the file does not exist in the target location of the target host. Default is yes
  • others: All options in the file module can be used here
  • src: The local address of the file to be copied to the remote host. It can be an absolute path or a relative path. If the path is a directory, it will be copied recursively. In this case, if the path ends with "/", only the contents of the directory will be copied. If it does not end with "/", the entire contents including the directory will be copied, similar to rsync.
  • validate: The validation command to run before copying into place. The path to the file to validate is passed in via '%s' which must be present as in the visudo example below.

An example is as follows:

ansible test -m copy -a "src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode=0644"
ansible test -m copy -a "src=/mine/ntp.conf dest=/etc/ntp.conf owner=root group=root mode=644 backup=yes"
ansible test -m copy -a "src=/mine/sudoers dest=/etc/sudoers validate='visudo -cf %s'"
Copy after login
5. Service module

用于管理服务
该模块包含如下选项:

arguments:给命令行提供一些选项

enabled:是否开机启动 yes|no

name:必选项,服务名称

pattern:定义一个模式,如果通过status指令来查看服务的状态时,没有响应,就会通过ps指令在进程中根据该模式进行查找,如果匹配到,则认为该服务依然在运行

runlevel:运行级别

sleep:如果执行了restarted,在则stop和start之间沉睡几秒钟

state:对当前服务执行启动,停止、重启、重新加载等操作(started,stopped,restarted,reloaded)

使用示例:

# Example action to reload service httpd, in all cases
- service: name=httpd state=reloaded
# Example action to enable service httpd, and not touch the running state
- service: name=httpd enabled=yes
# Example action to start service foo, based on running process /usr/bin/foo
- service: name=foo pattern=/usr/bin/foo state=started
# Example action to restart network service for interface eth0
- service: name=network state=restarted args=eth0
Copy after login
六、cron模块
  • 用于管理计划任务
    包含如下选项:
  • backup:对远程主机上的原任务计划内容修改之前做备份
  • cron_file:如果指定该选项,则用该文件替换远程主机上的cron.d目录下的用户的任务计划
  • day:日(1-31,*,*/2,……)
  • hour:小时(0-23,*,*/2,……)
  • minute:分钟(0-59,*,*/2,……)
  • month:月(1-12,*,*/2,……)
  • weekday:周(0-7,*,……)
  • job:要执行的任务,依赖于state=present
  • name:该任务的描述
  • special_time:指定什么时候执行,参数:reboot,yearly,annually,monthly,weekly,daily,hourly
  • state:确认该任务计划是创建还是删除
  • user:以哪个用户的身份执行

示例:

ansible test -m cron -a 'name="a job for reboot" special_time=reboot job="/some/job.sh"'
ansible test -m cron -a 'name="yum autoupdate" weekday="2" minute=0 hour=12 user="root
ansible 10.212.52.252 -m cron -a 'backup="True" name="test" minute="0" hour="2" job="ls -alh > /dev/null"'
ansilbe test -m cron -a 'cron_file=ansible_yum-autoupdate state=absent'
Copy after login
七、yum模块

使用yum包管理器来管理软件包,其选项有:

  • config_file:yum的配置文件
  • disable_gpg_check:关闭gpg_check
  • disablerepo:不启用某个源
  • enablerepo:启用某个源
  • name:要进行操作的软件包的名字,也可以传递一个url或者一个本地的rpm包的路径
  • state:状态(present,absent,latest)

示例如下:

ansible test -m yum -a 'name=httpd state=latest'
ansible test -m yum -a 'name="@Development tools" state=present'
ansible test -m yum -a 'name=http://nginx.org/packages/centos/6/noarch/RPMS/
nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present'
Copy after login
八、user模块与group模块

user模块是请求的是useradd, userdel, usermod三个指令,goup模块请求的是groupadd, groupdel, groupmod 三个指令,具体参数这里不再细讲,直接上示例。
1、user模块示例:

- user: name=johnd comment="John Doe" uid=1040 group=admin
- user: name=james shell=/bin/bash groups=admins,developers append=yes
- user: name=johnd state=absent remove=yes
- user: name=james18 shell=/bin/zsh groups=developers expires=1422403387
#生成密钥时,只会生成公钥文件和私钥文件,和直接使用ssh-keygen指令效果相同,不会生成authorized_keys文件。
- user: name=test generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa
Copy after login

注:指定password参数时,不能使用后面这一串密码会被直接传送到被管理主机的/etc/shadow文件中,所以需要先将密码字符串进行加密处理。然后将得到的字符串放到password中即可。

[root@361way ~]# openssl passwd -1 -salt $(
<p>不同的发行版默认使用的加密方式可能会有区别,具体可以查看/etc/login.defs文件确认,centos 6.5版本使用的是SHA512加密算法,生成密码可以通过ansible官方给出的示例:</p>
<pre class="brush:php;toolbar:false">python -c "from passlib.hash import sha512_crypt; import getpass; print sha512_crypt.encrypt
(getpass.getpass())"
Copy after login

2、group示例

- group: name=somegroup state=present
Copy after login
九、synchronize模块

使用rsync同步文件,其参数如下:

  • archive: 归档,相当于同时开启recursive(递归)、links、perms、times、owner、group、-D选项都为yes ,默认该项为开启
  • checksum: 跳过检测sum值,默认关闭
  • compress:是否开启压缩
  • copy_links:复制链接文件,默认为no ,注意后面还有一个links参数
  • delete: 删除不存在的文件,默认no
  • dest:目录路径
  • dest_port:默认目录主机上的端口 ,默认是22,走的ssh协议
  • dirs:传速目录不进行递归,默认为no,即进行目录递归
  • rsync_opts:rsync参数部分
  • set_remote_user:主要用于/etc/ansible/hosts中定义或默认使用的用户与rsync使用的用户不同的情况
  • mode: push或pull 模块,push模的话,一般用于从本机向远程主机上传文件,pull 模式用于从远程主机上取文件

另外还有其他参数,这里不再一一说明。上几个用法:

src=some/relative/path dest=/some/absolute/path rsync_path="sudo rsync"
src=some/relative/path dest=/some/absolute/path archive=no links=yes
src=some/relative/path dest=/some/absolute/path checksum=yes times=no
src=/tmp/helloworld dest=/var/www/helloword rsync_opts=--no-motd,--exclude=.git mode=pull
Copy after login
十、mount模块
  • 配置挂载点
    选项:
  • dump
    fstype:必选项,挂载文件的类型
  • name:必选项,挂载点
  • opts:传递给mount命令的参数
  • src:必选项,要挂载的文件
  • state:必选项present:只处理fstab中的配置absent:删除挂载点mounted:自动创建挂载点并挂载之umounted:卸载

示例:

name=/mnt/dvd src=/dev/sr0 fstype=iso9660 opts=ro state=present
name=/srv/disk src='LABEL=SOME_LABEL' state=present
name=/home src='UUID=b3e48f45-f933-4c8e-a700-22a159ec9077' opts=noatime state=present
ansible test -a 'dd if=/dev/zero of=/disk.img bs=4k count=1024'
ansible test -a 'losetup /dev/loop0 /disk.img'
ansible test -m filesystem 'fstype=ext4 force=yes opts=-F dev=/dev/loop0'
ansible test -m mount 'name=/mnt src=/dev/loop0 fstype=ext4 state=mounted opts=rw'
Copy after login
十一、get_url 模块

该模块主要用于从http、ftp、https服务器上下载文件(类似于wget),主要有如下选项:

sha256sum:下载完成后进行sha256 check;

timeout:下载超时时间,默认10s

url:下载的URL

url_password、url_username:主要用于需要用户名密码进行验证的情况

use_proxy:是事使用代理,代理需事先在环境变更中定义

示例:

- name: download foo.conf
get_url: url=http://example.com/path/file.conf dest=/etc/foo.conf mode=0440
- name: download file with sha256 check
get_url: url=http://example.com/path/file.conf dest=/etc/foo.conf sha256sum=b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c
Copy after login

模块部分就先介绍到这里吧,官方提供的可能用到模块有git、svn版本控制模块,sysctl 、authorized_key_module系统模块,apt、zypper、pip、gem包管理模块,find、template文件模块,mysql_db、redis数据库模块,url 网络模块等。具体可以参看官方手册模块部分

The above is the detailed content of A brief introduction to common modules of ansible. For more information, please follow other related articles on the PHP Chinese website!

source:linuxprobe.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!