ansible 安装和简单使用

有些话、适合烂在心里 提交于 2020-01-16 05:29:57
A yum -y install ansible  
B 打通管理节点到各个被管理节点的免密码ssh 
cat .ssh/id_rsa.pub  > .ssh/authorized_keys 
authorized_keys  分发给各个server
C 添加主机配置 
vi /etc/hosts
192.168.31.182 centos7_1
192.168.31.22  centos7_2
192.168.31.189 centos7_3
192.168.31.193 centos7_4
 
vi /etc/ansible/hosts
[testdb]
centos7_1
centos7_2
centos7_3
centos7_4
 
# 测试是否能 ping 通
ansible all -m ping
 
# 在所有节点上执行命令
ansible all -a "hostname"
 
使用 paybook 语言
 
将目标机器进行分组
 
[testdb]
test1
test2
test3
 
 
对不同的组执行命令
ansible testdb -a "hostname"
ansible ywdb -a "hostname"
 
ansible <pattern_goes_here> -m <module_name> -a <arguments>
ansible webservers -m service -a "name=httpd state=restarted"
 
组里面也可以进一步定义 包含哪些机器 不包含哪些机器
webservers:dbservers:&staging:!phoenix
webservers:!{{excluded}}:&{{required}}
 
# 在指定的节点上 或者多个节点上
[root@test12 ~]# ansible testdb[0] -a "hostname"
test1 | SUCCESS | rc=0 >>
test1
 
[root@test12 ~]# ansible testdb[0:8] -a "hostname"
test4 | SUCCESS | rc=0 >>
test4
test5 | SUCCESS | rc=0 >>
test5
test3 | SUCCESS | rc=0 >>
test3
 
# 拷贝文件
ansible atlanta -m copy -a "src=/etc/hosts dest=/tmp/hosts"
 
# 修改文件权限
ansible webservers -m file -a "dest=/srv/foo/a.txt mode=600"
ansible webservers -m file -a "dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan"
 
ansible testdb[0] -m file -a "dest=/tmp/hosts mode=600"
ansible testdb[0] -m file -a "dest=/tmp/hosts mode=600 owner=mysql group=dba"
 
# 创建目录
ansible webservers -m file -a "dest=/path/to/c mode=755 owner=mdehaan group=mdehaan state=directory"
ansible testdb[0] -m file -a "dest=/tmp/hostsss mode=755 owner=mysql group=dba state=directory"
 
# 删除目录
ansible webservers -m file -a "dest=/path/to/c state=absent"
ansible  testdb[0] -m file -a "dest=/tmp/hostsss state=absent"
 
# 安装软件
## 确认包是否安装
ansible webservers -m yum -a "name=acme state=present"
ansible testdb[0] -m yum -a "name=python state=present"
 
## 确认安装包的版本、
ansible webservers -m yum -a "name=acme-1.5 state=present"
ansible testdb[0] -m yum -a "name=python-2.7 state=present"
 
## 确认一个包还没有安装
ansible webservers -m yum -a "name=acme state=absent"
ansible testdb[0] -m yum -a "name=python3 state=absent"
 
## 管理用户
ansible all -m user -a "name=foo password=<crypted password here>"
ansible all -m user -a "name=foo state=absent"
 
## git 部署webapp
ansible webservers -m git -a "repo=git://foo.example.org/repo.git dest=/srv/myapp version=HEAD"
 
## 服务管理
ansible testdb[0] -m service -a "name=ntpd state=started"
ansible testdb[0] -m service -a "name=ntpd state=stoped"
ansible testdb[0] -m service -a "name=ntpd state=started"
 
 
 
 
 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!