开源运维自动化工具 Ansible 详解(5)

示例如下:

[root@centos01 ~]# ansible web -m shell -a "/usr/bin/rm -rf /etc/yum.repos.d/CentOS-*" <!--批量化删除web组主机的yum源--> [root@centos01 ~]# ansible web -m shell -a "/usr/bin/mount /dev/cdrom /mnt" <!--批量化挂载光盘--> [WARNING]: Consider using mount module rather than running mount 192.168.100.20 | SUCCESS | rc=0 >> mount: /dev/sr0 写保护,将以只读方式挂载 192.168.100.30 | SUCCESS | rc=0 >> mount: /dev/sr0 写保护,将以只读方式挂载 [root@centos01 ~]# ansible web -m yum -a "name=httpd state=present" <!--批量化安装httpd程序--> [root@centos01 ~]# ansible web -m shell -a "rpm -qa | grep httpd" <!--批量化查看安装的httpd程序包--> [WARNING]: Consider using yum, dnf or zypper module rather than running rpm 192.168.100.20 | SUCCESS | rc=0 >> httpd-2.4.6-67.el7.centos.x86_64 httpd-tools-2.4.6-67.el7.centos.x86_64 192.168.100.30 | SUCCESS | rc=0 >> httpd-2.4.6-67.el7.centos.x86_64 httpd-tools-2.4.6-67.el7.centos.x86_64 [root@centos01 ~]# ansible web -m shell -a "systemctl start httpd" <!--批量启动服务--> [root@centos01 ~]# ansible web -m shell -a "netstat -anptu | grep httpd" <!--批量化监听httpd服务是否启动成功--> 192.168.100.20 | SUCCESS | rc=0 >> tcp6 0 0 :::80 :::* LISTEN 2072/httpd 192.168.100.30 | SUCCESS | rc=0 >> tcp6 0 0 :::80 :::* LISTEN 3098/httpd

管理端只是发送yum指令到被管理端,被管理端要存在可用的yum仓库才可以成功安装。

6)service模块

service模块为用来管理远程主机上的服务的模块。常见的参数如下:

name:被管理的服务名称;

state=started|stopped|restarted:动作包含启动,关闭或重启;

enable=yes|no:表示是否设置该服务开机自启动;

runlevel:如果设定了enabled开机自启动,则要定义在哪些运行目标下自动启动;

示例如下:

[root@centos01 ~]# ansible web -m service -a "name=httpd enabled=yes state=restarted" <!--设置httpd服务重新启动和开机自动启动--> 7)user模块

user模块主要用于管理远程主机上的用户账号。常见的参数如下:

name:必选参数,账号名称;

state=present|absent:创建账号或者删除账号,present表示创建,absent表示删除;

system=yes|no:是否为系统账户;

uid:用户UID;

group:用户的基本组;

groups:用户的附加组;

shell:默认使用的shell;

home:用户的家目录;

mve_home=yes|no:如果设置的家目录已经存在,是否将已存在的家目录进行移动;

pssword:用户的密码,建议使用加密后的字符串;

comment:用户的注释信息;

remore=yes|no:当state=absent时,是否要删除用户的家目录;

创建用户示例如下:

[root@centos01 ~]# ansible web -m user -a "name=user01 system=yes uid=502 group=root groups=root shell=/etc/nologin home=/home/user01 password=pwd@123" <!--在web组的所有主机上新建一个系统用户,UID为502, 属组是root,��字是user01,密码是pwd@123--> 四、playbook配置文件 1、执行配置文件

playbook配置文件使用YAML语法,具有简洁明了、结构清晰等特点。playbook配置文件类似于shell脚本,是一个YAML格式的文件,用于保存针对特定需求的任务列表。上面介绍的ansible命令虽然可以完成各种任务,但是当配置一些复杂任务时,逐条输入就显得效率非常低下。更有效的方案是在playbook配置文件中放置所有的任务代码,利用ansible-playbook命令执行该文件,可以实现自动化运维。YAML文件的扩展名通常为.yaml或.yml。

YAML语法与其他高级语言类似,其结构通过缩进来展示,通过“-”来代表项;“:”用来分隔键和值;整个文件以“---”开头并以“...”结尾,如下所示:

[root@centos01 ~]# grep -v ^# /etc/ansible/hosts | grep -v ^$ <!--查看hosts中的分组信息--> [web1] 192.168.100.20 [web2] 192.168.100.30 [root@centos01 ~]# vim /etc/ansible/a.yml <!--创建a.yml文件,写入以下内容--> --- - hosts: web1 <!--针对web1组中的操作--> remote_user: root <!--远端执行用户身份为root--> tasks: <!--任务列表--> - name: adduser <!--任务名称--> user: name=user1 state=present <!--执行user模块,创建用户--> tags: <!--创建tag标签--> - aaa <!--tag标签为aaa--> - name: addgroup <!--任务名称--> group: name=root system=yes <!--执行group模块,创建组--> tags: <!--创建tag标签--> - bbb <!--tag标签为bbb--> - hosts: web2 <!--针对web2组中的操作--> remote_user: root <!--远端执行用户身份为root--> tasks: <!--任务列表--> - name: copy file to web <!--任务名称--> copy: src=https://www.linuxidc.com/etc/passwd dest=/home <!--执行copy模块,复制文件--> tags: <!--创建tag标签--> - ccc <!--tag标签为ccc--> ...

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/83f828c48821a1020ae3d9c1d6ed245b.html