ansible实现CentOS6模版机克隆后的标准化部署

版权声明:本文为原创文章,转载请标明出处。 https://blog.csdn.net/zwjzqqb/article/details/84400429

依据《CentOS6重置网卡克隆后的自适应脚本》改造模板机,并克隆出试验机
试验机的vlan IP为192.168.77.10,需要进行的配置如下:

  • 配置主机名为:web
  • 配置IP地址为:192.168.77.200
  • 部署zabbix agent 并启动

依据《CentOS7 python3.6 ansible2.5 源码安装》部署ansible主机,IP:192.168.77.100

192.168.77.100操作:

yum -y install sshpass
su - deploy
ssh-keygen
RootDefaultPass=vincent
# 需要提前获取模板机的root密码
SampleIP=192.168.77.10
# 设置试验机的IP
rm ~/.ssh/known_hosts
sshpass -p ${RootDefaultPass} ssh-copy-id -o StrictHostKeyChecking=no root@${SampleIP}
ssh root@${SampleIP} date
echo ${SampleIP}>/tmp/hosts
ansible -i /tmp/hosts ${SampleIP} -u root -m ping
rm -rf /tmp/hosts

mkdir -pv os_init/{inventory,roles}
cat >os_init/inventory/proenv<<EOF
${SampleIP}
[Server]
${SampleIP}
[Server:vars]
Host_name=web
Host_IP=192.168.77.200
Ethernet=eth0
ZabbixServer=192.168.77.100
ZabbixAgentRpm=zabbix-agent-3.0.23-1.el6.x86_64.rpm
EOF

mkdir -pv os_init/roles/os/{files,tasks,templates}
# 上传zabbix-agent-3.0.23-1.el6.x86_64.rpm到os_init/roles/os/files目录之下

cat >os_init/roles/os/templates/os.sh.j2<<EOF
#!/bin/bash
source ~/.bash_profile
hostname {{ Host_name }}
echo "\$(grep -E '127|::1' /etc/hosts)">/etc/hosts
echo "{{ Host_IP }} {{ Host_name }}">>/etc/hosts
sed -i "s/^HOSTNAME.*$/HOSTNAME={{ Host_name }}/g" /etc/sysconfig/network
sed -i 's/\(IPADDR=\).*/\1{{ Host_IP }}/g' /etc/sysconfig/network-scripts/ifcfg-{{ Ethernet }}
yum -y localinstall /tmp/{{ ZabbixAgentRpm }}
sed -i 's/^Server=127.0.0.1/Server={{ ZabbixServer }}/g' /etc/zabbix/zabbix_agentd.conf
sed -i 's/^ServerActive=127.0.0.1/ServerActive={{ ZabbixServer }}:10051/g' /etc/zabbix/zabbix_agentd.conf
sed -i "s/^\(Hostname=\).*/\1{{ Host_name }}/g" /etc/zabbix/zabbix_agentd.conf
chkconfig zabbix-agent on
/etc/init.d/zabbix-agent stop
EOF

cat >os_init/roles/os/tasks/main.yml<<EOF
- name: rsync os.sh
  template: src=roles/os/templates/os.sh.j2 dest=/tmp/os.sh
- name: rsync zabbix agent rpm
  copy: "remote_src=no src=roles/os/files/{{ ZabbixAgentRpm }} dest=/tmp/{{ ZabbixAgentRpm }} mode=0644"
- name: setup hosts
  shell: "bash /tmp/os.sh"
- name: update os
  shell: "yum -y update warn=False"
- name: reboot linux
  shell: "reboot"
EOF


cat >os_init/deploy.yml<<EOF
- hosts: "Server"
  gather_facts: true
  remote_user: root
  roles:
    - os
EOF

cd os_init
ansible-playbook -i inventory/proenv ./deploy.yml
cd

[TOC]

猜你喜欢

转载自blog.csdn.net/zwjzqqb/article/details/84400429