Ansible的安装与使用

Ansible的安装

1.安装源

yum search ansible
yum -y install centos-release-ansible-29

2.安装ansible、

yum -y install ansible

3.添加被管理服务器IP到清单

vim /etc/ansible/hosts
#[webservers]
#192.168.50.162
#192.168.50.164

4.配置免密

ssh-keygen  #一直按回车
ssh-copy-id root@192.168.50.162
ssh-copy-id root@192.168.50.164

5.测试被管理主机是否可达

ansible webservers -m ping

常用的模块

  • command 默认模块

  • shell 执行shell命令的,它可以支持一些特殊符号的解释,,|&&

  • yum 使用yum工具安装卸载软件

    • 参数
      name :指定要管理的服务名
      state :指定要进行的操作
      • 安装:installed / latest
      • 卸载:removed / absent
    • 示例
      ansible webservers -m yum -a ‘name=httpd state=installed’
      ansible webservers -m yum -a ‘name=httpd state=removed’
  • systemd 管理服务启动、停止、重启

    • 参数
      name :指定要管理的用户名
      state :指定要操作动作

      • 启动:started
      • 关闭:stopped
      • 重启:restarted
      • 重载:reloaded
      • 开机自启:enabled(true为开机自启)
  • 实例

#批量安装httpd
ansible webservers -m yum -a 'name=httpd state=installed'
#批量启动所有主机的httpd
ansible webservers -m systemd -a 'name=httpd state=started enabled=true'
#查看被管理节点的80端口是否被监听
ansible webservers -m shell -a 'ss -ntl | grep 80'
#创建一个httpd的测试页面
ansible webservers -m shell -a 'echo test > /var/www/html/index.html'
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值