ansible集中化自动管理(部署LAMP环境)
##ansible集中化自动管理目标:1、生成公钥,并上传ssh的公钥到被控端主机
2、在ansible的主控端配置本地yum源和网络yum源
3、安装ansible,用ansible上传yum源目录到被控端主机。
4、用ansible管理被控端主机的系统、软件和服务。
5、用playbooks剧本(yaml脚本文件)来管理被控端。
各种网络yum仓库:
6zabbix-2.4: rpm -ivh http://repo.zabbix.com/zabbix/2.4/rhel/6/x86_64/zabbix-release-2.4-1.el6.noarch.rpm
6zabbix-3.2(兼容性不好,可能无法安装): http://repo.zabbix.com/zabbix/3.4/rhel/6/x86_64/
7zabbix-2.4: rpm -ivh http://repo.zabbix.com/zabbix/2.4/rhel/7/x86_64/zabbix-release-2.4-1.el7.noarch.rpm
7zabbix-3.2: rpm -ivh http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-1.el7.centos.noarch.rpm
centos6: wget -O /etc/yum.repos.d/6CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
6epel源:wget -O /etc/yum.repos.d/6epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
centos7: wget -O /etc/yum.repos.d/7CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
7epel源:wget -O /etc/yum.repos.d/7epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
网络环境:
asible主控端:192.168.10.1
ansible被控端:192.168.10.10~192.168.10.20
具体实施:
1、生成公钥,并上传ssh的公钥到被控端主机
第1步,在asible主控端生成公钥。
ssh-keygen-trsa-f~/.ssh/id_rsa-N''
yuminstall-yexpect
第2步,批量上传公钥到被控端。
foriin11
do
ssh-copy-idroot@192.168.10.$i
sshroot@192.168.10.$iipa
done
ssh-add
sed-ri'/^#UseDNS/c\UseDNSno'/etc/ssh/sshd_config
sed-ri'/^GSSAPIAuthentication/c\GSSAPIAuthenticationno'/etc/ssh/sshd_config
grep-En'^UseDNS|^GSSAPIAuth'/etc/ssh/sshd_config
2、在ansible的主控端配置本地yum源和网络yum源。
cd/etc/yum.repos.d
mkdir-pv bak
mv-vf*.repobak/
wget -O /etc/yum.repos.d/6epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
wget -O /etc/yum.repos.d/6CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
rpm -ivh http://repo.zabbix.com/zabbix/2.4/rhel/6/x86_64/zabbix-release-2.4-1.el6.noarch.rpm
sed-ri's/\$releasever/6/g' 6CentOS-Base.repo
cat> rhel6.5.repo <<-EOF
name=Red Hat Enterprise Linux $releasever - $basearch - Source
baseurl=file:///dvd
enabled=1
gpgcheck=0
EOF
yumcleanall
yummakecachefast
yumlistzabbixansible
yum install zabbix-server-mysqlzabbix-web-mysqlzabbix-agent --enablerepo=zabbix-y
rpm-qa|grepzabbix
3、安装ansible,用ansible上传yum源到被控端主机。
yuminstall-yansible
yuminstall-ycurlelinkslynxcreaterepo
grep -b2 '^\'/etc/ansible/hosts || echo -e '\n192.168.10.11\n192.168.10.12'>> /etc/ansible/hosts
ansibletest-mping
ansibletest-mcopy-a'src=/etc/ssh/sshd_configdest=/etc/ssh/'
ansibletest-mshell -a'servicesshdrestart'
ansibletest-mshell-a'rm-rf/etc/yum.repos.d/*;ls/etc/yum.repos.d/'
ansibletest-mcopy-a'src=/etc/yum.repos.d/dest=/etc/yum.repos.d/force=yes mode=755'
ansibletest-mshell-a'ls/etc/yum.repos.d'
4、用ansible管理被控端主机的系统、软件和服务。
ansibletest-mshell-a'rpm-qhttpdmysql-server php'
ansibletest-myum-a'name=httpdstate=present'
ansibletest-myum-a'name=mysql-serverstate=present'
ansibletest-myum-a'name=phpstate=present'
ansibletest-mshell-a'rpm-qhttpdmysql-server php'
ansibletest-mservice-a'name=httpdstate=restartedenabled=1'
ansibletest-mservice-a'name=mysqldstate=restartedenabled=1'
ansibletest-mshell-a'yuminstall-ycurlelinkslynxcreaterepo--enablerepo=rhel6.5'
ansibletest-mshell-a'rpm-q curlelinkslynxcreaterepo'
ansibletest-mshell-a"echo'<?phpphpinfo()?>' > /var/www/html/p.php"
ansibletest-mshell-a"echo'apache test' > /var/www/html/a.html"
ansibletest-mshell-a'curl127.0.0.1/a.html'
ansibletest-mshell-a'mysql-e "grantall on *.* toadminidentifiedby 'adminwithgrant option;flushprivileges'"'
ansibletest-mshell-a'mysql -uadmin-padmin -e "showdatabases;selectuser,host,passwordfrommysql.user;"'
5、用playbooks剧本(yaml脚本文件)来管理被控端。
目标1:编写一个playbooks剧本install_lamp.yaml,实现全自动部署LAMP环境。
viminstall_lamp.yaml
- hosts: all
vars:
http_port: 80
remote_user: root
tasks:
- name: apache
yum: pkg=httpdstate=present
notify:
- apache restart
- name: mysql-server
yum: pkg=mysql-serverstate=present
notify:
- mysqld restart
- name: php
yum: pkg=phpstate=present
handlers:
- name: apache restart
service: name=httpdstate=restarted
- name: mysqld restart
service: name=mysqldstate=restarted
运行剧本:ansible-playbookinstall_lamp.yaml
验证:ansibletest-mshell-a'rpm-qhttpdmysql-serverphp'
目标2:编写一个playbooks剧本remove_lamp.yaml,实现全自动卸载LAMP环境。
vimremove_lamp.yaml
- hosts: all
vars:
http_port: 80
remote_user: root
tasks:
- name: apache
yum: pkg=httpdstate=absent
- name: mysql-server
yum: pkg=mysql-serverstate=absent
- name: php
yum: pkg=phpstate=absent
运行剧本:ansible-playbookremove_lamp.yaml
验证:ansibletest-mshell-a'rpm-qhttpdmysql-serverphp'
页:
[1]