系统性能优化DevOpsAnsile

ansible常用模块

2022-03-10  本文已影响0人  Joening

1.command命令模块

# 默认模块, 执行命令
[root@m01 ~]# ansible webserver  -a "hostname"

# 如果需要一些管道操作,则使用shell
[root@m01 ~]# ansible webserver -m shell -a "ifconfig|grep eth0" -f 50


# -f =forks   /etc/ansible/ansible.cfg #结果返回的数量

2.script脚本模块

# 编写脚本
[root@m01 ~]# mkdir -p /server/scripts
[root@m01 ~]# cat /server/scripts/yum.sh
#!/usr/bin/bash
yum install -y iftop

#在本地运行模块,等同于在远程执行,不需要将脚本文件进行推送目标主机执行
[root@m01 ~]# ansible webserver -m script -a "/server/scripts/yum.sh"

3.yum安装软件模块


[root@m01 ~]# ansible webserver -m yum -a "name=httpd state=installed"
name        #指定要安装的软件包名称
state       #指定使用yum的方法
    installed,present   #安装软件包
    removed,absent      #移除软件包
    latest              #安装最新软件包 

4.copy文件拷贝模块


# 推送文件模块
[root@m01 ~]# ansible webserver -m copy -a "src=/etc/hosts dest=/tmp/test.txt"

# 在推送覆盖远程端文件前,对远端已有文件进行备份,按照时间信息备份
[root@m01 ~]# ansible webserver -m copy -a "src=/etc/hosts dest=/tmp/test.txt backup=yes"

# 直接向远端文件内写入数据信息,并且会覆盖远端文件内原有数据信息
[root@m01 ~]# ansible webserver -m copy -a "content='bgx' dest=/tmp/webserver"

src             #推送数据的源文件信息
dest            #推送数据的目标路径
backup          #对推送传输过去的文件,进行备份
content         #直接批量在被管理端文件中添加内容
group           #将本地文件推送到远端,指定文件属组信息
owner           #将本地文件推送到远端,指定文件属主信息
mode            #将本地文件推送到远端,指定文件权限信息

5.file文件配置模块


[root@m01 ~]# ansible webserver -m file -a "path=/tmp/webserver state=directory"
[root@m01 ~]# ansible webserver -m file -a "path=/tmp/tt state=touch mode=555 owner=root group=root"
[root@m01 ~]# ansible webserver -m file -a "src=/tmp/tt path=/tmp/tt_link state=link"

path            #指定远程主机目录或文件信息
recurse         #递归授权
state 
    directory   #在远端创建目录
    touch       #在远端创建文件
    link        #link或hard表示创建链接文件
    absent      #表示删除文件或目录
    mode        #设置文件或目录权限
    owner       #设置文件或目录属主信息
    group       #设置文件或目录属组信息

6.service服务模块


[root@m01 ~]# ansible webserver -m service -a "name=crond state=stopped enabled=yes"

name        # 定义要启动服务的名称
state       # 指定服务状态
    started     #启动服务
    stopped     #停止服务
    restarted   #重启服务
    reloaded    #重载服务
enabled         #开机自启

7.group组模块


[root@m01 ~]# ansible webserver -m group -a "name=oldgirl gid=888"

name            #指定创建的组名
gid             #指定组的gid
state
    absent      #移除远端主机的组
    present     #创建远端主机的组(默认)

8.user模块


#创建用户指定uid和gid,不创建家目录也不允许登陆
[root@m01 ~]# ansible webserver -m user -a "name=oldgirl uid=888 group=888 shell=/sbin/nologin create_home=no"

#将明文密码进行hash加密,然后进行用户创建
[root@m01 ~]# ansible localhost -m debug -a "msg={{ 'bgx' | password_hash('sha512', 'salt') }}"
localhost | SUCCESS => {
    "msg": "$6$salt$WP.Kb1hMfqJG7dtlBltkj4Um4rVhch54R5JCi6oP73MXzGhDWqqIY.JkSOnIsBSOeXpKglY7gUhHzY4ZtySm41"
}
[root@m01 ~]# ansible webserver -m user -a 'name=xlw password=$6$salt$WP.Kb1hMfqJG7dtlBltkj4Um4rVhch54R5JCi6oP73MXzGhDWqqIY.JkSOnIsBSOeXpKglY7gUhHzY4ZtySm41 create_home=yes shell=/bin/bash'

uid             #指定用户的uid
group           #指定用户组名称
groups          #指定附加组名称
password        #给用户添加密码
shell           #指定用户登录shell
create_home     #是否创建家目录

8.crond定时任务模块


# 正常使用crond服务
[root@m01 ~]# crontab -l
* * * * *  /bin/sh /server/scripts/yum.sh

# 使用ansible添加一条定时任务
[root@m01 ~]# ansible webserver -m cron -a "minute=* hour=* day=* month=* weekday=*  job='/bin/sh /server/scripts/test.sh'"
[root@m01 ~]# ansible webserver -m cron -a "job='/bin/sh /server/scripts/test.sh'"

# 设置定时任务注释信息,防止重复,name设定
[root@m01 ~]# ansible webserver -m cron -a "name='cron01' job='/bin/sh /server/scripts/test.sh'"

# 删除相应定时任务
[root@m01 ~]# ansible webserver -m cron -a "name='ansible cron02' minute=0 hour=0 job='/bin/sh /server/scripts/test.sh' state=absent"
 
# 注释相应定时任务,使定时任务失效
[root@m01 scripts]# ansible webserver -m cron -a "name='ansible cron01' minute=0 hour=0 job='/bin/sh /server/scripts/test.sh' disabled=no"

9.mount模块


[root@m01 ~]# ansible webserver -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=present"
[root@m01 ~]# ansible web -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=mounted"
[root@m01 ~]# ansible web -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=unmounted"
[root@m01 ~]# ansible web -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=absent"

present     # 开机挂载,仅将挂载配置写入/etc/fstab
mounted     # 挂载设备,并将配置写入/etc/fstab
unmounted   # 卸载设备,不会清除/etc/fstab写入的配置
absent      # 卸载设备,会清理/etc/fstab写入的配置
10.ansible查看帮助方法

其他操作

[root@m01 ~]# ansible-doc -l    --- 查看所有模块说明信息
[root@m01 ~]# ansible-doc copy  --- 表示指定查看某个模块参数用法信息
上一篇下一篇

猜你喜欢

热点阅读