ansible自动部署脚本

1、yum install epel-release

yum -y install ansible    #安装ansible


2、vi /etc/ansible/hosts      #配置目标主机

[testhosts]    #主机组

主机ip地址1 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass=root

主机ip地址2 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass=root

……


3、vi /etc/ansible/ansible.cfg #修改/etc/ansible/ansible.cfg配置文件来跳过这个主机密钥检查,文本中原本就有,去掉其注释状态即可

host_key_checking = False


4、执行ansible testhosts -m ping来检测主机组执行脚本主机的连通性


5、zabbix playboot批量安装zabbix agent部署脚本zabbix_agent.yml(zabbix_agent.yml是编辑的安装agent和配置zabbix_agentd.conf的脚本文件)

---

 - hosts: "{{ server }}"

   remote_user: root

   vars:

   -  zabbix_agent_ip: "{{ ansible_em1['ipv4']['address'] }}"

   -  zabbix_server_ip: xxx.xxx.xxx.xxx

      remote_user: root

   tasks: 

       #当系统为 centos 7 的时候安装centos 7 的rpm 包

   -  name: Install zabbix rpm source when OS version eq 7

      shell: "rpm -ivh https://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-2.el7.noarch.rpm"

      when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "7"

      #当系统为centos 6 的时候安装centos 6的rpm 包

   -  name: Install zabbix rpm source when OS version eq 6

      shell: "rpm -ivh https://repo.zabbix.com/zabbix/3.4/rhel/6/x86_64/zabbix-release-3.4-1.el6.noarch.rpm"

      when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "6" 

       #安装zabbix agent

   -  name: Install zabbix agent

      shell: "yum -y install zabbix-agent"

        #通过变量替换默认配置中的zabbix server地址

   -  name: modify zabbix server ip address

      shell: "sed -i 's#Server=127.0.0.1#Server='{{zabbix_server_ip}}'#g' /etc/zabbix/zabbix_agentd.conf"

   -  name: modify zabbix server active ip addr

      shell: "sed -i 's/ServerActive=127.0.0.1/ServerActive='{{zabbix_server_ip}}'/g' /etc/zabbix/zabbix_agentd.conf"

         #开启远程执行命令功能,这个功能是zabbix

   -  name: Enable remote command execution

      shell: "sed -i 's/# EnableRemoteCommands=0/EnableRemoteCommands=1'/g /etc/zabbix/zabbix_agentd.conf"

   -  name: Enable remote command logs

      shell: "sed -i 's/# LogRemoteCommands=0/LogRemoteCommands=1'/g /etc/zabbix/zabbix_agentd.conf"

        #引用变量zabbix_agent_ip 设置agent hostname

   -  name: modify zabbix agent hostname

      shell: "sed -i 's/Hostname=Zabbix server/Hostname='{{zabbix_agent_ip}}'/g' /etc/zabbix/zabbix_agentd.conf"

        #启动agent 同时加入开机启动项

      service: name=zabbix-agent enabled=yes state=started


6、ansible-playbook zabbix_agent.yml密码    #执行zabbix-agent自动配置脚本

注意:yml文件的编写需要遵循严格的格式,可以到https://old.qqe2.com/jsontool/yaml.php这个yml格式在线校验网站进行在线校验后再执行

评论

© 糊涂虫的博客 | Powered by LOFTER