一、实验环境
server5:172.25.6.5 管理主机
server6:172.25.6.6
server7:172.25.6.7
server8:172.25.6.8
要保证server虚拟机环境纯净,安装虚拟机步骤详细见: https://blog.csdn.net/lcqrehl/article/details/107832183
二、Ansible Ad-Hoc命令集
ansible-doc -l #显示所有可用模块
ansible-doc yum #获取yum模块帮助,需要什么模块就在后面加,例如copy service等等
ansible的常用模块
1.copy模块
ansible test -m copy -a "src=/etc/passwd dest=/mnt" #-m调用模块,-a参数 #命令行表示在管理主机server5远程复制一个passwd文件到server6的/mnt,(因为test组下是server6)源路径是/etc/ 目的路径是/mnt
2.file模块
ansible test -m file -a "dest=/etc/httpd/conf/httpd.conf mode=600 owner=devops group=root" #修改文件权限和属性 ansible test -m file -a "dest=/mnt/westos mode=755 owner=root group=root state=directory" #递归属性
3.yum模块
ansible test -m yum -a "name=httpd state=present" #下载httpd, ansible test-m yum -a "name=httpd state=absent" #卸载httpd
下载httpd
server5中执行远程在server6上安装httpd服务
卸载httpd
4.service模块
ansible test -m service -a "name=httpd state=started" #打开httpd服务 ansible test -m service -a "name=httpd state=restarted" #重启httpd服务 ansible test -m service -a "name=httpd state=stopped" #停止httpd服务
执行以上命令后,server6 的httpd打开
执行以上命令后,server6 的httpd服务关闭
5.user模块
ansible test -m user -a "name=lcq password=<加密密码>" ansible test -m user -a "name=lcq state=absent remove=yes"
6.mysql_user模块
ansible prod -m mysql_user -a "login_user=root name=wxh password=testpass priv='*.*:select' host='%' state=present" #远程主机需要安装MySQL-python