Ansible-playbook install Zabbix_agent python
1、建立目錄linux
在下面顯示的目錄結構中,包含了zabbix安裝、卸載和配置三個角色(roles),以及每個roles的全部tasks列表、vars變量和zabbix配置文件。其中安裝和卸載分別經過zabbix_install.yml、zabbix_delete.yml主任務程序的入口文件調用shell
規劃以下:vim
[root@ansible /etc/ansible ]# tree zabbix_rhel/bash
[root@ansible /etc/ansible ]# tree zabbix_rhel/ zabbix_rhel/ └── zabbix_agent ├── roles │ ├── common │ │ ├── files │ │ ├── handlers │ │ ├── meta │ │ ├── tasks │ │ │ └── main.yml │ │ ├── templates │ │ └── vars │ ├── configure │ │ ├── files │ │ ├── handlers │ │ ├── meta │ │ ├── tasks │ │ ├── templates │ │ └── vars │ ├── install │ │ ├── files │ │ │ └── ansible-zabbix-3.0.8.tar.gz │ │ ├── handlers │ │ │ └── main.yml │ │ ├── meta │ │ ├── tasks │ │ │ ├── 01-create-user.yml │ │ │ ├── 02-copy-code.yml │ │ │ ├── 03-start-zabbix.yml │ │ │ ├── 04-add-iptables.yml │ │ │ └── main.yml │ │ ├── templates │ │ │ ├── zabbix_agentd │ │ │ └── zabbix_agentd.conf │ │ └── vars │ │ └── main.yml │ └── uninstall │ ├── files │ ├── handlers │ │ └── main.yml │ ├── meta │ ├── tasks │ │ ├── del_iptables.yml │ │ ├── main.yml │ │ └── uninstall_zabbix.yml │ ├── templates │ └── vars │ └── main.yml ├── zabbix_delete.yml └── zabbix_install.yml
經過以下命令建立less
[root@ansible /etc/ansible ]# mkdir zabbix_rhel [root@ansible /etc/ansible ]# mkdir zabbix_rhel/zabbix_agent [root@ansible /etc/ansible ]# mkdir zabbix_rhel/zabbix_agent/roles [root@ansible /etc/ansible ]# mkdir zabbix_rhel/zabbix_agent/roles [root@ansible /etc/ansible ]# mkdir zabbix_rhel/zabbix_agent/roles/{common,install,uninstall,configure}/{handlers,files,meta,tasks,templates,vars} -p
主機列表/etc/ansible/hostsdom
[testhosts] 10.17.83.33 10.17.83.34
2、安裝程序的tasks任務列表curl
1、定義安裝程序入口文件zabbix_install.yml tcp
[root@ansible /etc/ansible/zabbix_rhel/zabbix_agent ]# vim zabbix_install.yml --- - hosts: testhosts remote_user: root gather_facts: True roles: - common - install
二、定義安裝程序-建立用戶任務01-create-user.ymlide
[root@ansible /etc/ansible/zabbix_rhel/zabbix_agent ]# vim roles/install/tasks/01-create-user.yml --- - name: Create zabbix user user: name={{ zabbix_user }} state=present createhome=no shell=/sbin/nologin
三、定義安裝程序-拷貝安裝文件任務02-copy-code.yml
[root@ansible /etc/ansible/zabbix_rhel/zabbix_agent ]# vim roles/install/tasks/02-copy-code.yml --- - name: Copy zabbix agentd code file to clients copy: src=ansible-zabbix-{{ zabbix_version }}.tar.gz dest=/usr/local/src/ansible-zabbix-{{ zabbix_version }}.tar.gz owner=root group=root - name: Uncompression ansible-zabbix-{{ zabbix_version }}.tar.gz shell: tar zxf /usr/local/src/ansible-zabbix-{{ zabbix_version }}.tar.gz -C /usr/local - name: Copy zabbix start script template: src=zabbix_agentd dest=/etc/init.d/zabbix_agentd owner=root group=root mode=0755 - name: Copy zabbix config file template: src=zabbix_agentd.conf dest={{ zabbix_dir }}/etc/zabbix_agentd.conf owner={{ zabbix_user }} group={{ zabbix_user }} mode=0644 - name: Modify zabbix basedir permisson file: path={{ zabbix_dir }} owner={{ zabbix_user }} group={{ zabbix_user }} mode=0755 recurse=yes - name: Link zabbix_agentd command shell: ln -s {{ zabbix_dir }}/sbin/zabbix_agentd /usr/local/sbin/zabbix_agentd - name: Delete ansible-zabbix-{{ zabbix_version }}.tar.gz source file shell: rm -rf /usr/local/src/ansible-zabbix-{{ zabbix_version }}.tar.gz
4、定義安裝程序-啓動zabbix_agentd服務任務03-start-zabbix.yml
[root@ansible /etc/ansible/zabbix_rhel/zabbix_agent ]# vim roles/install/tasks/03-start-zabbix.yml --- - name: Start zabbix service shell: /etc/init.d/zabbix_agentd start - name: Add boot start zabbix service shell: chkconfig --level 345 zabbix_agentd on
五、定義安裝程序-添加iptable規則04-add-iptables.yml
[root@ansible /etc/ansible/zabbix_rhel/zabbix_agent ]# vim roles/install/tasks/04-add-iptables.yml --- - name: insert iptables rule for zabbix lineinfile: dest=/etc/sysconfig/iptables create=yes state=present regexp="{{ zabbix_agentd_port }}" insertafter="^:OUTPUT " line="-A INPUT -p tcp --dport {{ zabbix_agentd_port }} -s {{ zabbix_server_ip }} -j ACCEPT" notify: restart iptables
六、定義安裝程序-tasks任務列表的主調用接口文件main.yml
[root@ansible /etc/ansible/zabbix_rhel/zabbix_agent ]# vim roles/install/tasks/main.yml --- - include: 01-create-user.yml - include: 02-copy-code.yml - include: 03-start-zabbix.yml - include: 04-add-iptables.yml
tasks任務列表說明:
Playbook容許用戶將tasks任務細分爲多個任務列表,經過一個main任務來調用。固然你也能夠將涉及的全部任務所有寫到main.yml文件中。
七、定義common任務
該任務爲安裝一些依賴軟件包
[root@ansible /etc/ansible/zabbix_rhel/zabbix_agent ]# vim roles/common/tasks/main.yml - name: Install initializtion require software yum: name={{ item }} state=latest with_items: - libselinux-python - libcurl-devel
3、安裝程序的vars變量文件
在roles/install/vars/目錄中建立main.yml文件,並定義安裝過程當中使用到變量
[root@ansible /etc/ansible/zabbix_rhel/zabbix_agent ]# vim roles/install/vars/main.yml zabbix_dir: /usr/local/zabbix zabbix_version: 3.0.8 zabbix_user: zabbix zabbix_server_port: 10051 zabbix_agentd_port: 10050 zabbix_server_ip: 10.17.81.120
4、安裝程序的handlers任務定義
handlers目錄中爲tasks任務中notify調用的動做(當文件發生改變時,經過notify執行相關的操做,好比修改了配置文件後,須要重啓相應的服務)
[root@ansible /etc/ansible/zabbix_rhel/zabbix_agent ]# vim roles/install/handlers/main.yml --- - name: restart iptables service: name=iptables state=restarted
5、安裝程序的zabbix安裝文件
將預先編譯好的zabbix打包後,放到files/目錄中,Playbook在執行操做過程當中會根據tasks任務將文件發送到目標主機上
[root@ansible /etc/ansible/zabbix_rhel/zabbix_agent ]# ls roles/install/files/ ansible-zabbix-3.0.8.tar.gz
6、安裝程序的zabbix配置文件
使用Playbook安裝zabbix涉及的全部配置文件都經過templates模塊去同步
[root@ansible /etc/ansible/zabbix_rhel/zabbix_agent ]# ls roles/install/templates/ zabbix_agentd zabbix_agentd.conf
在安裝任務中,zabbix_agentd.conf配置文件主要涉及zabbix服務端IP地址的修改,能夠經過預約義的變量來替換
zabbix_agentd.conf文件以下
# This is a configuration file for Zabbix agent daemon (Unix) # To get more information about Zabbix, visit http://www.zabbix.com ############ GENERAL PARAMETERS ################# ### Option: PidFile # Name of PID file. # # Mandatory: no # Default: # PidFile=/tmp/zabbix_agentd.pid ### Option: LogType # Specifies where log messages are written to: # system - syslog # file - file specified with LogFile parameter # console - standard output # # Mandatory: no # Default: # LogType=file ### Option: LogFile # Log file name for LogType 'file' parameter. # # Mandatory: no # Default: # LogFile= LogFile=/tmp/zabbix_agentd.log ### Option: LogFileSize # Maximum size of log file in MB. # 0 - disable automatic log rotation. # # Mandatory: no # Range: 0-1024 # Default: # LogFileSize=1 ### Option: DebugLevel # Specifies debug level: # 0 - basic information about starting and stopping of Zabbix processes # 1 - critical information # 2 - error information # 3 - warnings # 4 - for debugging (produces lots of information) # 5 - extended debugging (produces even more information) # # Mandatory: no # Range: 0-5 # Default: # DebugLevel=3 ### Option: SourceIP # Source IP address for outgoing connections. # # Mandatory: no # Default: # SourceIP= ### Option: EnableRemoteCommands # Whether remote commands from Zabbix server are allowed. # 0 - not allowed # 1 - allowed # # Mandatory: no # Default: # EnableRemoteCommands=0 ### Option: LogRemoteCommands # Enable logging of executed shell commands as warnings. # 0 - disabled # 1 - enabled # # Mandatory: no # Default: # LogRemoteCommands=0 ##### Passive checks related ### Option: Server # List of comma delimited IP addresses (or hostnames) of Zabbix servers. # Incoming connections will be accepted only from the hosts listed here. # If IPv6 support is enabled then '127.0.0.1', '::127.0.0.1', '::ffff:127.0.0.1' are treated equally. # # Mandatory: no # Default: # Server= Server={{ zabbix_server_ip }} ### Option: ListenPort # Agent will listen on this port for connections from the server. # # Mandatory: no # Range: 1024-32767 # Default: # ListenPort=10050 ### Option: ListenIP # List of comma delimited IP addresses that the agent should listen on. # First IP address is sent to Zabbix server if connecting to it to retrieve list of active checks. # # Mandatory: no # Default: # ListenIP=0.0.0.0 ### Option: StartAgents # Number of pre-forked instances of zabbix_agentd that process passive checks. # If set to 0, disables passive checks and the agent will not listen on any TCP port. # # Mandatory: no # Range: 0-100 # Default: # StartAgents=3 ##### Active checks related ### Option: ServerActive # List of comma delimited IP:port (or hostname:port) pairs of Zabbix servers for active checks. # If port is not specified, default port is used. # IPv6 addresses must be enclosed in square brackets if port for that host is specified. # If port is not specified, square brackets for IPv6 addresses are optional. # If this parameter is not specified, active checks are disabled. # Example: ServerActive=127.0.0.1:20051,zabbix.domain,[::1]:30051,::1,[12fc::1] # # Mandatory: no # Default: # ServerActive= ServerActive={{ zabbix_server_ip }}:10051 ### Option: Hostname # Unique, case sensitive hostname. # Required for active checks and must match hostname as configured on the server. # Value is acquired from HostnameItem if undefined. # # Mandatory: no # Default: # Hostname= Hostname={{ ansible_default_ipv4.address }} ### Option: HostnameItem # Item used for generating Hostname if it is undefined. Ignored if Hostname is defined. # Does not support UserParameters or aliases. # # Mandatory: no # Default: # HostnameItem=system.hostname ### Option: HostMetadata # Optional parameter that defines host metadata. # Host metadata is used at host auto-registration process. # An agent will issue an error and not start if the value is over limit of 255 characters. # If not defined, value will be acquired from HostMetadataItem. # # Mandatory: no # Range: 0-255 characters # Default: # HostMetadata= ### Option: HostMetadataItem # Optional parameter that defines an item used for getting host metadata. # Host metadata is used at host auto-registration process. # During an auto-registration request an agent will log a warning message if # the value returned by specified item is over limit of 255 characters. # This option is only used when HostMetadata is not defined. # # Mandatory: no # Default: # HostMetadataItem= ### Option: RefreshActiveChecks # How often list of active checks is refreshed, in seconds. # # Mandatory: no # Range: 60-3600 # Default: # RefreshActiveChecks=120 ### Option: BufferSend # Do not keep data longer than N seconds in buffer. # # Mandatory: no # Range: 1-3600 # Default: # BufferSend=5 ### Option: BufferSize # Maximum number of values in a memory buffer. The agent will send # all collected data to Zabbix Server or Proxy if the buffer is full. # # Mandatory: no # Range: 2-65535 # Default: # BufferSize=100 ### Option: MaxLinesPerSecond # Maximum number of new lines the agent will send per second to Zabbix Server # or Proxy processing 'log' and 'logrt' active checks. # The provided value will be overridden by the parameter 'maxlines', # provided in 'log' or 'logrt' item keys. # # Mandatory: no # Range: 1-1000 # Default: # MaxLinesPerSecond=20 ############ ADVANCED PARAMETERS ################# ### Option: Alias # Sets an alias for an item key. It can be used to substitute long and complex item key with a smaller and simpler one. # Multiple Alias parameters may be present. Multiple parameters with the same Alias key are not allowed. # Different Alias keys may reference the same item key. # For example, to retrieve the ID of user 'zabbix': # Alias=zabbix.userid:vfs.file.regexp[/etc/passwd,^zabbix:.:([0-9]+),,,,\1] # Now shorthand key zabbix.userid may be used to retrieve data. # Aliases can be used in HostMetadataItem but not in HostnameItem parameters. # # Mandatory: no # Range: # Default: ### Option: Timeout # Spend no more than Timeout seconds on processing # # Mandatory: no # Range: 1-30 # Default: # Timeout=3 ### Option: AllowRoot # Allow the agent to run as 'root'. If disabled and the agent is started by 'root', the agent # will try to switch to the user specified by the User configuration option instead. # Has no effect if started under a regular user. # 0 - do not allow # 1 - allow # # Mandatory: no # Default: # AllowRoot=0 ### Option: User # Drop privileges to a specific, existing user on the system. # Only has effect if run as 'root' and AllowRoot is disabled. # # Mandatory: no # Default: # User=zabbix ### Option: Include # You may include individual files or all files in a directory in the configuration file. # Installing Zabbix will create include directory in /usr/local/etc, unless modified during the compile time. # # Mandatory: no # Default: # Include= # Include=/usr/local/etc/zabbix_agentd.userparams.conf # Include=/usr/local/etc/zabbix_agentd.conf.d/ # Include=/usr/local/etc/zabbix_agentd.conf.d/*.conf ####### USER-DEFINED MONITORED PARAMETERS ####### ### Option: UnsafeUserParameters # Allow all characters to be passed in arguments to user-defined parameters. # The following characters are not allowed: # \ ' " ` * ? [ ] { } ~ $ ! & ; ( ) < > | # @ # Additionally, newline characters are not allowed. # 0 - do not allow # 1 - allow # # Mandatory: no # Range: 0-1 # Default: # UnsafeUserParameters=0 ### Option: UserParameter # User-defined parameter to monitor. There can be several user-defined parameters. # Format: UserParameter=<key>,<shell command> # See 'zabbix_agentd' directory for examples. # # Mandatory: no # Default: # UserParameter= ####### LOADABLE MODULES ####### ### Option: LoadModulePath # Full path to location of agent modules. # Default depends on compilation options. # # Mandatory: no # Default: # LoadModulePath=${libdir}/modules ### Option: LoadModule # Module to load at agent startup. Modules are used to extend functionality of the agent. # Format: LoadModule=<module.so> # The modules must be located in directory specified by LoadModulePath. # It is allowed to include multiple LoadModule parameters. # # Mandatory: no # Default: # LoadModule= ####### TLS-RELATED PARAMETERS ####### ### Option: TLSConnect # How the agent should connect to server or proxy. Used for active checks. # Only one value can be specified: # unencrypted - connect without encryption # psk - connect using TLS and a pre-shared key # cert - connect using TLS and a certificate # # Mandatory: yes, if TLS certificate or PSK parameters are defined (even for 'unencrypted' connection) # Default: # TLSConnect=unencrypted ### Option: TLSAccept # What incoming connections to accept. # Multiple values can be specified, separated by comma: # unencrypted - accept connections without encryption # psk - accept connections secured with TLS and a pre-shared key # cert - accept connections secured with TLS and a certificate # # Mandatory: yes, if TLS certificate or PSK parameters are defined (even for 'unencrypted' connection) # Default: # TLSAccept=unencrypted ### Option: TLSCAFile # Full pathname of a file containing the top-level CA(s) certificates for # peer certificate verification. # # Mandatory: no # Default: # TLSCAFile= ### Option: TLSCRLFile # Full pathname of a file containing revoked certificates. # # Mandatory: no # Default: # TLSCRLFile= ### Option: TLSServerCertIssuer # Allowed server certificate issuer. # # Mandatory: no # Default: # TLSServerCertIssuer= ### Option: TLSServerCertSubject # Allowed server certificate subject. # # Mandatory: no # Default: # TLSServerCertSubject= ### Option: TLSCertFile # Full pathname of a file containing the agent certificate or certificate chain. # # Mandatory: no # Default: # TLSCertFile= ### Option: TLSKeyFile # Full pathname of a file containing the agent private key. # # Mandatory: no # Default: # TLSKeyFile= ### Option: TLSPSKIdentity # Unique, case sensitive string used to identify the pre-shared key. # # Mandatory: no # Default: # TLSPSKIdentity= ### Option: TLSPSKFile # Full pathname of a file containing the pre-shared key. # # Mandatory: no # Default: # TLSPSKFile=
7、執行安裝zabbix_agentd
執行過程輸出信息
[root@ansible /etc/ansible/zabbix_rhel/zabbix_agent ]# ansible-playbook zabbix_install.yml PLAY [testhosts] *************************************************************** TASK [setup] ******************************************************************* ok: [10.17.83.33] ok: [10.17.83.34] TASK [common : Install initializtion require software] ************************* ok: [10.17.83.33] => (item=[u'libselinux-python', u'libcurl-devel']) ok: [10.17.83.34] => (item=[u'libselinux-python', u'libcurl-devel']) TASK [install : Create zabbix user] ******************************************** changed: [10.17.83.33] changed: [10.17.83.34] TASK [install : Copy zabbix agentd code file to clients] *********************** changed: [10.17.83.33] changed: [10.17.83.34] TASK [install : Uncompression ansible-zabbix-3.0.8.tar.gz] ********************* changed: [10.17.83.34] [WARNING]: Consider using unarchive module rather than running tar changed: [10.17.83.33] TASK [install : Copy zabbix start script] ************************************** changed: [10.17.83.33] changed: [10.17.83.34] TASK [install : Copy zabbix config file] *************************************** changed: [10.17.83.33] changed: [10.17.83.34] TASK [install : Modify zabbix basedir permisson] ******************************* changed: [10.17.83.33] changed: [10.17.83.34] TASK [install : Link zabbix_agentd command] ************************************ changed: [10.17.83.34] [WARNING]: Consider using file module with state=link rather than running ln changed: [10.17.83.33] TASK [install : Delete ansible-zabbix-3.0.8.tar.gz source file] **************** changed: [10.17.83.33] [WARNING]: Consider using file module with state=absent rather than running rm changed: [10.17.83.34] TASK [install : Start zabbix service] ****************************************** changed: [10.17.83.33] changed: [10.17.83.34] TASK [install : Add boot start zabbix service] ********************************* changed: [10.17.83.33] changed: [10.17.83.34] TASK [install : insert iptables rule for zabbix] ******************************* changed: [10.17.83.33] changed: [10.17.83.34] RUNNING HANDLER [install : restart iptables] *********************************** changed: [10.17.83.34] changed: [10.17.83.33] PLAY RECAP ********************************************************************* 10.17.83.33 : ok=14 changed=12 unreachable=0 failed=0 10.17.83.34 : ok=14 changed=12 unreachable=0 failed=0
Ansible-playbook uninstall Zabbix_agent
1、卸載程序的tasks任務列表
1、定義刪除程序入口文件zabbix_delete.yml
[root@ansible /etc/ansible/zabbix_rhel/zabbix_agent ]# vim zabbix_delete.yml --- - hosts: testhosts remote_user: root gather_facts: True roles: - uninstall
二、定義刪除zabbix文件任務uninstall_zabbix.yml
[root@ansible /etc/ansible/zabbix_rhel/zabbix_agent ]# vim roles/uninstall/tasks/uninstall_zabbix.yml --- - name: stop zabbix agentd service shell: /etc/init.d/zabbix_agentd stop - name: delete zabbix start script shell: rm -rf /etc/init.d/zabbix_agentd - name: delete zabbix_agentd script shell: rm -rf /usr/local/sbin/zabbix_agentd - name: delete zabbix agentd basedir shell: rm -rf {{ zabbix_basedir }} - name: delete zabbix agent logfile shell: rm -rf /tmp/zabbix_agentd.log - name: delete zabbix user user: name=zabbix state=absent remove=yes
三、定義刪除iptables防火牆規則任務del_iptables.yml
[root@ansible /etc/ansible/zabbix_rhel/zabbix_agent ]# vim roles/uninstall/tasks/del_iptables.yml --- - name: delete iptables rule for zabbix lineinfile: dest=/etc/sysconfig/iptables state=absent line="-A INPUT -p tcp --dport {{ zabbix_agentd_port }} -s {{ zabbix_server_ip }} -j ACCEPT" notify: restart iptables
四、定義刪除主task任務調用文件main.yml
[root@ansible /etc/ansible/zabbix_rhel/zabbix_agent ]# vim roles/uninstall/tasks/main.yml --- - include: uninstall_zabbix.yml - include: del_iptables.yml
五、定義刪除程序的handlers任務
[root@ansible /etc/ansible/zabbix_rhel/zabbix_agent ]# vim roles/uninstall/handlers/main.yml --- - name: restart iptables service: name=iptables state=restarted
六、定義刪除程序的vars變量文件
[root@ansible /etc/ansible/zabbix_rhel/zabbix_agent ]# vim roles/uninstall/vars/main.yml zabbix_basedir: /usr/local/zabbix zabbix_agentd_port: 10050 zabbix_server_ip: 10.17.81.120
2、執行刪除zabbix_agentd
執行刪除輸出信息
[root@ansible /etc/ansible/zabbix_rhel/zabbix_agent ]# ansible-playbook zabbix_delete.yml PLAY [testhosts] *************************************************************** TASK [setup] ******************************************************************* ok: [10.17.83.33] ok: [10.17.83.34] TASK [uninstall : stop zabbix agentd service] ********************************** changed: [10.17.83.34] changed: [10.17.83.33] TASK [uninstall : delete zabbix start script] ********************************** changed: [10.17.83.33] [WARNING]: Consider using file module with state=absent rather than running rm changed: [10.17.83.34] TASK [uninstall : delete zabbix_agentd script] ********************************* changed: [10.17.83.34] changed: [10.17.83.33] TASK [uninstall : delete zabbix agentd basedir] ******************************** changed: [10.17.83.33] changed: [10.17.83.34] TASK [uninstall : delete zabbix agent logfile] ********************************* changed: [10.17.83.33] changed: [10.17.83.34] TASK [uninstall : delete zabbix user] ****************************************** changed: [10.17.83.34] changed: [10.17.83.33] TASK [uninstall : delete iptables rule for zabbix] ***************************** changed: [10.17.83.33] changed: [10.17.83.34] RUNNING HANDLER [uninstall : restart iptables] ********************************* changed: [10.17.83.34] changed: [10.17.83.33] PLAY RECAP ********************************************************************* 10.17.83.33 : ok=9 changed=8 unreachable=0 failed=0 10.17.83.34 : ok=9 changed=8 unreachable=0 failed=0