一看必会系列:ansible 批量部署脚本 zabbix-agent — CCIE那点事

一看必会系列:ansible 批量部署脚本 zabbix-agent

来源:本站原创 Linux 超过679 views围观 0条评论

——–hosts配置———-
[root@ansible_master roles]# vim ../hosts

[zabbix-agent]
192.168.142.102 hostname=ansible_02  #给后面参数进行调用
192.168.142.103 hostname=ansible_03
192.168.142.104 hostname=ansible_04

 

├── zabbix-agent
│   ├── handlers
│   │   └── main.yaml
│   └── tasks
│       └── main.yaml
└── zabbix-agent.yaml   #这个是引用文件,放置在roles目录,运行这个文件即可.

————思路——————

rpm -i http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm
yum install zabbix-agent

vim /etc/zabbix/zabbix_agentd.conf
Server=127.0.0.1        改成Server=172.16.9.54
ServerActive=127.0.0.1  改成ServerActive=172.16.9.54
Hostname=Zabbix server  改成Hostname={hostname}   此处进行调用

——zabbix-agent.yaml  配置———
vim zabbix-agent.yaml

– hosts: zabbix-agent
  remote_user: root
  roles:
  – zabbix-agent

 

——-tasks配置脚本———-
vim zabbix-agent/tasks/main.yaml

– name: add iptables
  shell: iptables -I  INPUT 1 -p tcp –dport 10050 -j ACCEPT
– name: save iptables
  shell: service iptables save
– name: rpm i
#  shell: rpm -i http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm
  yum: name=http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm
– name: yum zabbix-agent
  #yum: name=zabbix-agent state=latest enable=yes
  yum: name=zabbix-agent state=latest
– name: enable service
  shell: systemctl enable zabbix-agent  #或使用 service: name=zabbix-agent enable=true
#  shell: yum install -y zabbix-agent
– name: change server ip
  shell: sed -i ‘s/Server=127.0.0.1/Server=172.16.9.54/g’ /etc/zabbix/zabbix_agentd.conf
– name: change server active ip
  shell: sed -i ‘s/ServerActive=127.0.0.1/ServerActive=172.16.9.54/g’ /etc/zabbix/zabbix_agentd.conf
– name: change server hostname
  shell: sed -i ‘s/Hostname=Zabbix server/Hostname={{hostname}}/g’ /etc/zabbix/zabbix_agentd.conf
  notify :
      – restart zabbix-agent

 

————-handlers脚本—————-

[root@ansible_master roles]# vim zabbix-agent/handlers/main.yaml

– name: restart zabbix-agent
  shell: systemctl restart zabbix-agent

——–正确的执行结果
[root@ansible_master roles]# ansible-playbook zabbix-agent.yaml -v
Using /etc/ansible/ansible.cfg as config file

PLAY [zabbix-agent] ******************************************************************************************

TASK [Gathering Facts] ***************************************************************************************
ok: [192.168.142.102]
ok: [192.168.142.104]
ok: [192.168.142.103]

TASK [zabbix-agent : add iptables] ***************************************************************************
changed: [192.168.142.102] => {"changed": true, "cmd": "iptables -I INPUT 1 -p tcp –dport 10050 -j ACCEPT", "delta": "0:00:00.007840", "end": "2018-02-28 17:16:59.680023", "rc": 0, "start": "2018-02-28 17:16:59.672183", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
changed: [192.168.142.104] => {"changed": true, "cmd": "iptables -I INPUT 1 -p tcp –dport 10050 -j ACCEPT", "delta": "0:00:00.008892", "end": "2018-02-28 17:16:59.700064", "rc": 0, "start": "2018-02-28 17:16:59.691172", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
changed: [192.168.142.103] => {"changed": true, "cmd": "iptables -I INPUT 1 -p tcp –dport 10050 -j ACCEPT", "delta": "0:00:00.007997", "end": "2018-02-28 17:16:59.698600", "rc": 0, "start": "2018-02-28 17:16:59.690603", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

TASK [zabbix-agent : save iptables] **************************************************************************
[WARNING]: Consider using service module rather than running service

changed: [192.168.142.103] => {"changed": true, "cmd": "service iptables save", "delta": "0:00:00.044004", "end": "2018-02-28 17:17:00.191499", "rc": 0, "start": "2018-02-28 17:17:00.147495", "stderr": "", "stderr_lines": [], "stdout": "iptables: Saving firewall rules to /etc/sysconfig/iptables: [  OK  ]", "stdout_lines": ["iptables: Saving firewall rules to /etc/sysconfig/iptables: [  OK  ]"]}
changed: [192.168.142.104] => {"changed": true, "cmd": "service iptables save", "delta": "0:00:00.043925", "end": "2018-02-28 17:17:00.225319", "rc": 0, "start": "2018-02-28 17:17:00.181394", "stderr": "", "stderr_lines": [], "stdout": "iptables: Saving firewall rules to /etc/sysconfig/iptables: [  OK  ]", "stdout_lines": ["iptables: Saving firewall rules to /etc/sysconfig/iptables: [  OK  ]"]}
changed: [192.168.142.102] => {"changed": true, "cmd": "service iptables save", "delta": "0:00:00.044992", "end": "2018-02-28 17:17:00.210450", "rc": 0, "start": "2018-02-28 17:17:00.165458", "stderr": "", "stderr_lines": [], "stdout": "iptables: Saving firewall rules to /etc/sysconfig/iptables: [  OK  ]", "stdout_lines": ["iptables: Saving firewall rules to /etc/sysconfig/iptables: [  OK  ]"]}

TASK [zabbix-agent : rpm i] **********************************************************************************
ok: [192.168.142.102] => {"changed": false, "msg": "", "rc": 0, "results": ["zabbix-release-3.2-1.el7.noarch providing /tmp/zabbix-release-3.2-1.el7.noarchgXuUSR.rpm is already installed"]}
ok: [192.168.142.103] => {"changed": false, "msg": "", "rc": 0, "results": ["zabbix-release-3.2-1.el7.noarch providing /tmp/zabbix-release-3.2-1.el7.noarch7GfY90.rpm is already installed"]}
ok: [192.168.142.104] => {"changed": false, "msg": "", "rc": 0, "results": ["zabbix-release-3.2-1.el7.noarch providing /tmp/zabbix-release-3.2-1.el7.noarchQBZp3k.rpm is already installed"]}

TASK [zabbix-agent : yum zabbix-agent] ***********************************************************************
changed: [192.168.142.102] => {"changed": true, "msg": "", "rc": 0, "results": ["Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: mirrors.aliyun.com\n * epel: mirrors.aliyun.com\n * extras: mirrors.aliyun.com\n * updates: mirrors.aliyun.com\nResolving Dependencies\n–> Running transaction check\n—> Package zabbix-agent.x86_64 0:3.2.11-1.el7 will be installed\n–> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package              Arch           Version               Repository      Size\n================================================================================\nInstalling:\n zabbix-agent         x86_64         3.2.11-1.el7          zabbix         348 k\n\nTransaction Summary\n================================================================================\nInstall  1 Package\n\nTotal download size: 348 k\nInstalled size: 1.3 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Installing : zabbix-agent-3.2.11-1.el7.x86_64                             1/1 \n  Verifying  : zabbix-agent-3.2.11-1.el7.x86_64                             1/1 \n\nInstalled:\n  zabbix-agent.x86_64 0:3.2.11-1.el7                                            \n\nComplete!\n"]}
changed: [192.168.142.104] => {"changed": true, "msg": "", "rc": 0, "results": ["Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: mirrors.aliyun.com\n * epel: mirrors.aliyun.com\n * extras: mirrors.aliyun.com\n * updates: mirrors.aliyun.com\nResolving Dependencies\n–> Running transaction check\n—> Package zabbix-agent.x86_64 0:3.2.11-1.el7 will be installed\n–> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package              Arch           Version               Repository      Size\n================================================================================\nInstalling:\n zabbix-agent         x86_64         3.2.11-1.el7          zabbix         348 k\n\nTransaction Summary\n================================================================================\nInstall  1 Package\n\nTotal download size: 348 k\nInstalled size: 1.3 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Installing : zabbix-agent-3.2.11-1.el7.x86_64                             1/1 \n  Verifying  : zabbix-agent-3.2.11-1.el7.x86_64                             1/1 \n\nInstalled:\n  zabbix-agent.x86_64 0:3.2.11-1.el7                                            \n\nComplete!\n"]}
changed: [192.168.142.103] => {"changed": true, "msg": "", "rc": 0, "results": ["Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: mirrors.aliyun.com\n * epel: mirrors.aliyun.com\n * extras: mirrors.aliyun.com\n * updates: mirrors.aliyun.com\nResolving Dependencies\n–> Running transaction check\n—> Package zabbix-agent.x86_64 0:3.2.11-1.el7 will be installed\n–> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package              Arch           Version               Repository      Size\n================================================================================\nInstalling:\n zabbix-agent         x86_64         3.2.11-1.el7          zabbix         348 k\n\nTransaction Summary\n================================================================================\nInstall  1 Package\n\nTotal download size: 348 k\nInstalled size: 1.3 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Installing : zabbix-agent-3.2.11-1.el7.x86_64                             1/1 \n  Verifying  : zabbix-agent-3.2.11-1.el7.x86_64                             1/1 \n\nInstalled:\n  zabbix-agent.x86_64 0:3.2.11-1.el7                                            \n\nComplete!\n"]}

TASK [zabbix-agent : change server ip] ***********************************************************************
[WARNING]: Consider using template or lineinfile module rather than running sed

changed: [192.168.142.102] => {"changed": true, "cmd": "sed -i ‘s/Server=127.0.0.1/Server=172.16.9.54/g’ /etc/zabbix/zabbix_agentd.conf", "delta": "0:00:00.005006", "end": "2018-02-28 17:17:29.458373", "rc": 0, "start": "2018-02-28 17:17:29.453367", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
changed: [192.168.142.103] => {"changed": true, "cmd": "sed -i ‘s/Server=127.0.0.1/Server=172.16.9.54/g’ /etc/zabbix/zabbix_agentd.conf", "delta": "0:00:00.006981", "end": "2018-02-28 17:17:29.516835", "rc": 0, "start": "2018-02-28 17:17:29.509854", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
changed: [192.168.142.104] => {"changed": true, "cmd": "sed -i ‘s/Server=127.0.0.1/Server=172.16.9.54/g’ /etc/zabbix/zabbix_agentd.conf", "delta": "0:00:00.006474", "end": "2018-02-28 17:17:29.523453", "rc": 0, "start": "2018-02-28 17:17:29.516979", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

TASK [zabbix-agent : change server active ip] ****************************************************************
changed: [192.168.142.102] => {"changed": true, "cmd": "sed -i ‘s/ServerActive=127.0.0.1/ServerActive=172.16.9.54/g’ /etc/zabbix/zabbix_agentd.conf", "delta": "0:00:00.007382", "end": "2018-02-28 17:17:29.960755", "rc": 0, "start": "2018-02-28 17:17:29.953373", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
changed: [192.168.142.103] => {"changed": true, "cmd": "sed -i ‘s/ServerActive=127.0.0.1/ServerActive=172.16.9.54/g’ /etc/zabbix/zabbix_agentd.conf", "delta": "0:00:00.004726", "end": "2018-02-28 17:17:29.978994", "rc": 0, "start": "2018-02-28 17:17:29.974268", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
changed: [192.168.142.104] => {"changed": true, "cmd": "sed -i ‘s/ServerActive=127.0.0.1/ServerActive=172.16.9.54/g’ /etc/zabbix/zabbix_agentd.conf", "delta": "0:00:00.006680", "end": "2018-02-28 17:17:30.006341", "rc": 0, "start": "2018-02-28 17:17:29.999661", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

TASK [zabbix-agent : change server hostname] *****************************************************************
changed: [192.168.142.102] => {"changed": true, "cmd": "sed -i ‘s/Hostname=Zabbix server/Hostname=ansible_02/g’ /etc/zabbix/zabbix_agentd.conf", "delta": "0:00:00.005474", "end": "2018-02-28 17:17:30.583521", "rc": 0, "start": "2018-02-28 17:17:30.578047", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
changed: [192.168.142.104] => {"changed": true, "cmd": "sed -i ‘s/Hostname=Zabbix server/Hostname=ansible_04/g’ /etc/zabbix/zabbix_agentd.conf", "delta": "0:00:00.004395", "end": "2018-02-28 17:17:30.619405", "rc": 0, "start": "2018-02-28 17:17:30.615010", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
changed: [192.168.142.103] => {"changed": true, "cmd": "sed -i ‘s/Hostname=Zabbix server/Hostname=ansible_03/g’ /etc/zabbix/zabbix_agentd.conf", "delta": "0:00:00.007179", "end": "2018-02-28 17:17:30.629389", "rc": 0, "start": "2018-02-28 17:17:30.622210", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

RUNNING HANDLER [zabbix-agent : restart zabbix-agent] ********************************************************
changed: [192.168.142.102] => {"changed": true, "cmd": "systemctl restart zabbix-agent", "delta": "0:00:00.066759", "end": "2018-02-28 17:17:31.157972", "rc": 0, "start": "2018-02-28 17:17:31.091213", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
changed: [192.168.142.104] => {"changed": true, "cmd": "systemctl restart zabbix-agent", "delta": "0:00:00.094599", "end": "2018-02-28 17:17:31.203785", "rc": 0, "start": "2018-02-28 17:17:31.109186", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
changed: [192.168.142.103] => {"changed": true, "cmd": "systemctl restart zabbix-agent", "delta": "0:00:00.069611", "end": "2018-02-28 17:17:31.203667", "rc": 0, "start": "2018-02-28 17:17:31.134056", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

PLAY RECAP ***************************************************************************************************
192.168.142.102            : ok=9    changed=7    unreachable=0    failed=0  
192.168.142.103            : ok=9    changed=7    unreachable=0    failed=0  
192.168.142.104            : ok=9    changed=7    unreachable=0    failed=0  

[root@ansible_master roles]#

———验证———————————–
ansible zabbix-agent  -m shell -a ‘cat /etc/zabbix/zabbix_agentd.conf |grep -v ^#|grep -v ^$’ -v
Using /etc/ansible/ansible.cfg as config file
192.168.142.104 | SUCCESS | rc=0 >>
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=127.0.0.1   ——–未成功
ServerActive=172.16.9.54
Hostname=ansible_04
Include=/etc/zabbix/zabbix_agentd.d/*.conf

192.168.142.103 | SUCCESS | rc=0 >>
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=127.0.0.1
ServerActive=172.16.9.54
Hostname=ansible_03
Include=/etc/zabbix/zabbix_agentd.d/*.conf

192.168.142.102 | SUCCESS | rc=0 >>
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=127.0.0.1
ServerActive=172.16.9.54
Hostname=ansible_02
Include=/etc/zabbix/zabbix_agentd.d/*.conf

————-以下是测试过程———————-

原始脚本如下 tasks/main.yaml  报错不能用
– name: add iptables
  #shell: iptables -I  INPUT 1 -p tcp –dport 10050 -j ACCEPT
– name: save iptables
  #shell: service iptables save
– name: rpm i
  raw: rpm -i http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm
– name: yum zabbix-agent
  yum: name=zabbix-agent state=latest enable=yes
– name: change server ip
  shell: sed S/Server=127.0.0.1/Server=172.16.9.54/g && sed S/ServerActive=127.0.0.1/ServerActive=172.16.9.54/g
  notify :
      – restart zabbix-agent

报这个错
不支持yum模块,实际查看是有的
      TASK [insetall zabbix-agent] *********************************************************************************
fatal: [192.168.142.102]: FAILED! => {"changed": false, "msg": "Unsupported parameters for (yum) module: enable Supported parameters include: allow_downgrade,conf_file,disable_gpg_check,disablerepo,enablerepo,exclude,install_repoquery,installroot,list,name,security,skip_broken,state,update_cache,validate_certs"}
fatal: [192.168.142.103]: FAILED! => {"changed": false, "msg": "Unsupported parameters for (yum) module: enable Supported parameters include: allow_downgrade,conf_file,disable_gpg_check,disablerepo,enablerepo,exclude,install_repoquery,installroot,list,name,security,skip_broken,state,update_cache,validate_certs"}
fatal: [192.168.142.104]: FAILED! => {"changed": false, "msg": "Unsupported parameters for (yum) module: enable Supported parameters include: allow_downgrade,conf_file,disable_gpg_check,disablerepo,enablerepo,exclude,install_repoquery,installroot,list,name,security,skip_broken,state,update_cache,validate_certs"}
    to retry, use: –limit @/etc/ansible/yamllist/zabbix-agent-all.retry

将脚本修改成
– hosts: zabbix-agent
  remote_user: root
  tasks:
    – name: insetall zabbix-agent
      shell: yum install -y zabbix-agent     —–换成这个OK
      #yum: name=zabbix-agent state=latest enable=yes

   
    [root@ansible_master roles]# ansible-playbook ../yamllist/zabbix-agent-all.yaml

PLAY [zabbix-agent] ******************************************************************************************

TASK [Gathering Facts] ***************************************************************************************
ok: [192.168.142.104]
ok: [192.168.142.102]
ok: [192.168.142.103]

TASK [insetall zabbix-agent] *********************************************************************************
[WARNING]: Consider using yum module rather than running yum

changed: [192.168.142.102]
changed: [192.168.142.103]
changed: [192.168.142.104]

PLAY RECAP ***************************************************************************************************
192.168.142.102            : ok=2    changed=1    unreachable=0    failed=0  
192.168.142.103            : ok=2    changed=1    unreachable=0    failed=0  
192.168.142.104            : ok=2    changed=1    unreachable=0    failed=0  

sed替换有问题换成这个解决
vim zabbix-agent/tasks/main.yaml
– name: change server ip
  shell: sed -i ‘s/Server=127.0.0.1/Server=172.16.9.54/g’ /etc/zabbix/zabbix_agentd.conf
  shell: sed -i ‘s/ServerActive=127.0.0.1/ServerActive=172.16.9.54/g’ /etc/zabbix/zabbix_agentd.conf
  shell: sed -i ‘s/Hostname=Zabbix server/Hostname={{hostname}}/g’ /etc/zabbix/zabbix_agentd.conf

TASK [zabbix-agent : change server ip] ***********************************************************************
[WARNING]: Consider using template or lineinfile module rather than running sed

changed: [192.168.142.102]
changed: [192.168.142.103]
changed: [192.168.142.104]
 
多行shell 并列测试
– name: change server ip
  shell: sed -i ‘s/Server=127.0.0.1/Server=172.16.9.54/g’ /etc/zabbix/zabbix_agentd.conf
  shell: sed -i ‘s/ServerActive=127.0.0.1/ServerActive=172.16.9.54/g’ /etc/zabbix/zabbix_agentd.conf
  shell: sed -i ‘s/Hostname=Zabbix server/Hostname={{hostname}}/g’ /etc/zabbix/zabbix_agentd.conf

—测试结果—————— 
      一个name只支持一个shell
[root@ansible_master roles]# ansible-playbook ../yamllist/changeip.yaml –syntax-check
[WARNING]: While constructing a mapping from /etc/ansible/yamllist/changeip.yaml, line 4, column 7, found a
duplicate dict key (shell). Using last defined value only.

修改后配置

– hosts: 192.168.142.102
  remote_user: root
  tasks:
  #测试显示 sed 可以采用 sed && sed 的方式联写,不能使用多个shell 并排
    – name: change server ip
      shell: sed -i ‘s/Server=127.0.0.1/Server=172.16.9.54/g’ /tmp/2
    – name: change servce active ip
      shell: sed -i ‘s/ServerActive=127.0.0.1/ServerActive=172.16.9.54/g’ /tmp/2
    – name: change hostname
      shell: sed -i ‘s/Hostname=Zabbix server/Hostname={{hostanme}}/g’ /tmp/2

————–以上是测试过程—————————–

文章出自:CCIE那点事 http://www.jdccie.com/ 版权所有。本站文章除注明出处外,皆为作者原创文章,可自由引用,但请注明来源。 禁止全文转载。
本文链接:http://www.jdccie.com/?p=3694转载请注明转自CCIE那点事
如果喜欢:点此订阅本站
下篇文章: