Ansible常用模块有哪些

发布时间:2022-02-19 09:59:59 作者:iii
来源:亿速云 阅读:155

这篇文章主要介绍“Ansible常用模块有哪些”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Ansible常用模块有哪些”文章能帮助大家解决问题。

Ansible是一种新流行的自动化运维工具,基于python2-paramiko模块开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令功能。

Ansible常用模块有哪些

一、ping模块

测试主机是否是通的,用法很简单,不涉及参数:

[root@361way ~]# ansible 10.212.52.252 -m ping10.212.52.252 | success >> {"changed": false,"ping": "pong"}

二、setup模块

setup模块,主要用于获取主机信息,在playbooks里经常会用到的一个参数gather_facts就与该模块相关。setup模块下经常使用的一个参数是filter参数,具体使用示例如下(由于输出结果较多,这里只列命令不写结果):

[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里的主机名)

三、file模块

file模块主要用于远程主机上的文件操作,file模块包含如下选项:

使用示例:

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模块

复制文件到远程主机,copy模块包含如下选项:

示例如下:

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'"

五、service模块

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

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

六、cron模块

示例:

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'

七、yum模块

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

示例如下:

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'

八、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

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

[root@361way ~]# openssl passwd -1 -salt $(
Password:$1$YngB4z8s$atSVltYKnDxJmWZ3s.4/80
或者
[root@361way ~]# echo "123456" | openssl passwd -1 -salt $(
| head -c 32) -stdin$1$4P4PlFuE$ur9ObJiT5iHNrb9QnjaIB0#经验证下面生成的密码串也可以正常使用,不过与/etc/shadow的格式不统一,不建议使用[root@361way ~]# openssl passwd -salt -1 "123456"-1yEWqqJQLC66#使用上面的密码创建用户[root@361way ~]#ansible all -m user -a 'name=foo password="$1$4P4PlFuE$ur9ObJiT5iHNrb9QnjaIB0"'

不同的发行版默认使用的加密方式可能会有区别,具体可以查看/etc/login.defs文件确认,centos 6.5版本使用的是SHA512加密算法,生成密码可以通过ansible官方给出的示例:

python -c "from passlib.hash import sha512_crypt; import getpass; print sha512_crypt.encrypt
(getpass.getpass())"

2、group示例

- group: name=somegroup state=present

九、synchronize模块

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

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

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

十、mount模块

示例:

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'

十一、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

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

关于“Ansible常用模块有哪些”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

推荐阅读:
  1. ansible安装及模块的管理
  2. Ansible常用模块的介绍和使用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ansible

上一篇:Gitlab的备份恢复与升级方法

下一篇:linux中du命令怎么用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》