1.设置免交互式身份验证
1)生成秘钥对: ssh-keygen -t RSA
2)上传到100.20和100.30上:ssh-copy-id -i .ssh/id_rsa.pub root@192.168.100.30
3)登录100.20:ssh root@192.168.100.20
2.安装ansible
1)删除系统自带yum源:rm -rf /etc/yum.repos.d/CentOS-*
2)切换ansible.iso光盘
3)挂载: mount /dev/cdrom /mnt/
4)修改yum文件:vim /etc/yum.repos.d/local.repo
5)安装:yum -y install ansible
6)配置ansbile客户端:vim /etc/ansible/hosts
3.测试客户端的连通性
1)客户端配置文件列表测试 :ansible -i /etc/ansible/hosts web -m ping
2)加载ping模块测试: ansible web -m ping
4.ansible交互式管理
操作:
1)使用ansible交互式工具:ansible-console
2)切换组:cd web
3)列出客户端:list
5.使用shell模块
操作:
1)使用shell模块查看sshd服务状态:ansible web -m shell -a ‘netstat -anptu | grep sshd’
2)重定向操作: ansible web -m shell -a ‘echo “centos02 192.168.100.20” >> /etc/hosts’
3)在100.20上查看:cat /etc/hosts
6.command模块
操作:
1)远程切换目录 :ansible web -m command -a ‘chdir=/ ls ./’
7.copy 模块
操作:
1)应用copy : ansible web -m copy -a ‘src=/etc/hosts dest=/root/host01 mode=777 owner=test group=root’
**8.yum模块 **
操作:
1)挂载:ansible web -m shell -a ‘mount /dev/cdrom /mnt’
2)安装apache :ansible web -m yum -a ‘name=httpd state=present’
2)查看是否安装上去:ansible web -m shell -a’rpm -qa | grep httpd’
3)启动服务:ansible web -m shell -a’systemctl start httpd’
4)查看端口:ansible web -m shell -a’netstat -anptu | grep 80’
**9.service模块 **
操作:
1)管理apache服务,停掉服务: ansible web -m service -a ‘name=httpd state=stopped’
2)查看端口:ansible web -m shell -a’netstat -anptu | grep 80’
10.user模块
操作:
1)创建用户 :ansible web -m user -a ‘name=bob system=yes shell=/bin/bash’
2)查看:ansible web -m shell -a’tail -2 /etc/passwd’