ansible处理多台机器部署基础环境

本次以多台机器需部署zabbix客户端为例:

机器先做免密互信,ansible主机上执行ssh-keygen,一路回车,然后将公钥发送给需管理的主机:

ssh-copy-id root@IP

1、编辑hosts文件,添加需配置的主机IP,并测试连通性

[root@oxidized ansible]# vim /etc/ansible/hosts
[all]
10.10.80.110
10.10.80.111
10.10.80.112
10.10.80.114
10.10.80.115
10.10.80.116
[root@oxidized ansible]# ansible all -m ping
10.10.80.111 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
。。。
。。。
。。。

2、编写playbook剧本

[root@oxidized ansible]# cat zabbix.yml
- name: install zabbix
  hosts: all
  tasks:
    - name: copy zabbix-agent2_rpm
      copy:
        src: /root/zabbix-agent2-6.4.0-release1.el7.x86_64.rpm
        dest: /root/zabbix-agent2-6.4.0-release1.el7.x86_64.rpm
    - name: copy zabbix.sh
      copy:
        src: /etc/ansible/zabbix-install.sh
        dest: /root/zabbix-install.sh
    - name: install zabbix_agent2
      shell: sh /root/zabbix-install.sh
[root@oxidized ansible]# cat zabbix-install.sh
#!/bin/bash
cd /etc/yum.repos.d/ && mkdir bak
mv *.repo bak/
cat >>/etc/yum.repos.d/local.repo <<EOF
[base]
name=Nexus
baseurl=http://10.10.200.20:8081/repository/yumHosted/
enabled=1
gpgcheck=0
EOF

yum makecache fast
cd /root && yum install -y zabbix-agent2-6.4.0-release1.el7.x86_64.rpm
systemctl enable zabbix-agent2 && systemctl start zabbix-agent2
systemctl status zabbix-agent2
if [ $? = 0 ];then
        echo 'zabbix has installed sucessfully!'
else
        echo 'zabbix is not running,please check it!'
        exit 1
fi
sed -i 's#Server=127.0.0.1#Server=10.10.80.101#g' /etc/zabbix/zabbix_agent2.conf
sed -i 's#ServerActive=127.0.0.1#ServerActive=10.10.80.101#g' /etc/zabbix/zabbix_agent2.conf
sed -i 's#Hostname=Zabbix server#Hostname=localhost#g' /etc/zabbix/zabbix_agent2.conf
systemctl restart zabbix-agent2
if [ $? = 0 ];then
        echo 'zabbix has restarted sucessfully!'
else
        echo 'zabbix has restarted failed,please check it!'
        exit 1
fi

3、执行playbook

[root@oxidized ansible]# ansible-playbook zabbix.yml
查看执行结果0fails即成功。

  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值