Ansible安装使用教程

ansible简介

ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。

ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:
(1)、连接插件connection plugins:负责和被监控端实现通信;
(2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;
(3)、各种模块核心模块、command模块、自定义模块;
(4)、借助于插件完成记录日志邮件等功能;
(5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。

百度百科

ansible安装部署

环境

  • 两台Redhat7.1虚拟机,一台做server一台做client
  • 防火墙firewalld关闭
  • selinux关闭

配置ansible仓库

[root@server1 ~]# yum install epel-release -y

server1和server2上安装ansible

[root@server1 local]# yum install ansible -y
[root@server2 local]# yum install ansible -y
[root@server1 local]# rpm -qa |grep ansible
ansible-2.4.2.0-2.el7.noarch

配置免密登录
server端ssh公钥创建认证

[root@server1 local]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
/root/.ssh/id_rsa already exists.
Overwrite (y/n)?
[root@server1 local]# cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

将server端公钥发送给客户端

[root@server1 local]# ssh-copy-id 192.168.56.137

验证免密是否配置成功

[root@server1 local]# ssh server2.example.com
Last login: Tue Nov 12 21:33:03 2019 from server1.example.com		#配置成功,不需要输入密码就能连上client

修改配置文件

[root@server1 ~]# vim /etc/ansible/hosts
[server2]
192.168.56.137	#只有在hosts中的远程主机才能接收到ansible指令

ansible应用实例

查看远程主机的时间

[root@server1 ~]# ansible server2 -m command -a 'uptime'
192.168.56.137 | SUCCESS | rc=0 >>
 21:47:40 up 17:41,  4 users,  load average: 0.00, 0.01, 0.10

测试远程主机的运行状态

[root@server1 ~]# ansible server2 -m ping
192.168.56.137 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

为远程主机添加用户

[root@server1 ~]# ansible server2 -m command -a 'useradd ansible'
192.168.56.137 | SUCCESS | rc=0 >>

[root@server2 ~]# id ansible
uid=5001(ansible) gid=5001(ansible) groups=5001(ansible)

yum软件包管理

[root@server1 ~]# ansible server2 -m yum -a "name=httpd  state=latest"
192.168.56.137 | SUCCESS => {
    "changed": false, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "All packages providing httpd are up to date", 
        ""
    ]
}

执行定时任务

[root@server1 ~]# ansible  server2 -m cron -a "name='mkdir' minute=5 hour=02 day=15 month=8 weekday=3 job='mkdir /tmp/a.txt'"
192.168.56.137 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "test job", 
        "mkdir"
    ]
}

管理远程主机系统服务

[root@server1 ~]# ansible server2 -m service -a "name=firewalld state=started"
192.168.56.137 | SUCCESS => {
    "changed": true, 
    "name": "firewalld", 
    "state": "started"
  }

远程sysctl设置
开启路由转发

[root@server1 ~]# ansible server2 -m sysctl -a "name=net.ipv4.ip_forward value=1 reload=yes"
192.168.56.137 | SUCCESS => {
    "changed": true
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值