Ansible自动化运维
一.
1.生成密钥对、
[root@centos ~]# ssh-keygen -t RSA
2.上传密钥对
[root@centos ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.100.20
3.验证免交互式认证
4.修改yum配置文件
5.安装ansible
6.配置ansbile客户端
7.测试客户端连通性
[root@centos ~]# ansible -i /etc/ansible/hosts web -m ping
[root@centos ~]# ansible web -m ping
二、ansible的应用
1.ansible交互式管理
(1 )使用ansible交互式工具
[root@centos ~]# ansible-console
(2)切换组
(3)列出客户端
2.shell模组
(1)使用shell模组查看sshd
[root@centos ~]# ansible web -m shell -a ‘netstat -anptu | grep sshd’
(2)使用shell重定向操作
[root@centos ~]# ansible web -m shell -a ‘echo “centos02 192.168.100.20” >> /etc/hosts’
3.Command 模组
(1)使用Command 模组切换目录
[root@centos ~]# ansible web -m command -a ‘cd /mnt/’
4.copy 模组
(1)使用copy模组修改文件名字、权限、所属用户、和组
[root@centos ~]# ansible web -m copy -a ‘src=/etc/hosts dest=/root/host01 mode=777 owner=test group=root’
5.yum模块
(1)使用yum模块安装httpd
[root@centos ~]# ansible web -m yum -a ‘name=httpd state=present’
6.service
(1)使用service启动htppd服务
[root@centos ~]# ansible web -m service -a ‘name=httpd state=started’
7.User
(1)使用user创建用户
[root@centos ~]# ansible web -m user -a ‘name=bob system=yes shell=/bin/bash password=pwd@123’