ansible模块精细化剖析,你值得拥有

1,cron模块

两种状态(state):present表示添加(可以省略),absent表示移除。

ansible-doc -s cron        #查看cron模块信息
ansible mysql -m cron -a 'minute="*/1" job="/bin/echo hello"  name="ni"'
ansible mysql -a 'crontab -l'
ansible mysql -m cron -a 'name="ni" state=absent'  #移除计划任务,假如该计划任务没有取名字,

mark

mark

在mysql上面查看邮件。

mark

mark

2,user模块

user模块是请求的是useradd, userdel, usermod三个指令

ansible-doc -s user
ansible mysql -m user -a 'name="su"'        #创建用户su
ansible mysql -m command -a 'tail /etc/passwd'
ansible mysql -m user -a 'name="su" state=absent'  #删除用户su

mark

mark

mark

3,group模块

group模块请求的是groupadd, groupdel, groupmod三个指令

ansible-doc -ssh-add group
ansible mysql -m group -a 'name=mysql gid=306 system=yes'
ansible mysql -a 'tail /etc/group'
ansible mysql -m user -a 'name=nan uid=306 system=yes group=mysql'
ansible mysql -a 'tail /etc/passwd'

mark

mark

mark

mark

4,copy模块

ansible-doc -s copy
ansible mysql -m copy -a 'src=/etc/fstab dest=/opt/fstab.back owner=root mode=640'
ansible mysql -a 'ls -l /opt'
ansible mysql -a 'cat /opt/fstab.back'

ansible mysql -m copy -a 'content="hello world!" dest=/opt/fstab.back' //将hello world!写入/opt/fstab.back
ansible mysql -a 'cat /opt/fstab.back'

mark

mark

mark

mark

mark

5,file模块

ansible-doc -s file
ansible mysql -m user -a 'name=mysql system=yes'
ansible mysql -m group -a 'name=mysql system=yes'
ansible mysql -m file -a 'path=/opt/fstab.link src=opt/fstab.back state=link'  #设置/opt/fstab.link为/opt/fstab.back
ansible mysql -m file -a "path=/opt/fstab.back state=absent"      #删除-个文件
ansible mysql -m file -a "path=/opt/test state=touch"                #创建一个文件

mark

mark

mark

mark

mark

6,ping模块

ansible all -m ping

mark

7,yum模块

ansible -doc -s yum
ansible mysql -m yum -a 'name=httpd'            #yum安装httpd
ansible mysql -m yum -a 'name=httpd state=absent'  #卸载httpd

rpm -q httpd

mark

mark

8,service模块

ansible-doc -s service
yum install -y httpd
ansible mysql -m service -a 'enabled=true name=httpd state=started'      #启动httpd服务
ansible mysql -a 'systemctl status httpd'        #查看web服务器httpd运行状态
ansible all -m service -a 'name=firewalld state=stopped'        #关闭防火墙

systemctl status httpd           #查看是否开启

mark

mark

9,shel模块

ansible-doc -s shell
ansible mysql -m shell -a 'echo 123123|passwd --stdin mysql'    #创建用户使用无交互模式给用户设置密码

mark

10,script模块

ansible-doc -s script
vi test.sh         #在ansible管理端创建脚本
#!/bin/bash

echo "hello ansible from script" > /opt/script.txt
chmod +x test.sh
ansible mysql -m script -a 'test.sh'

cat /opt/script.txt

mark

mark

mark

猜你喜欢

转载自blog.csdn.net/weixin_51614581/article/details/115474402

相关文章