Guest User

Untitled

a guest
May 21st, 2021
77
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #get_mik_ips.py
  2. from ast import literal_eval
  3. import json
  4. from pyzabbix import ZabbixAPI
  5. zapi = ZabbixAPI("http://zabbix.sogaz-med.ru/zabbix")
  6. zapi.login("login", "password")
  7. myfile = open ('/usr/scripts/mik_strs', 'w')
  8. myfile.write( " if [host] in [\"10.1.1.254\"")
  9. for host in zapi.host.get(output = ['hostid','host'], selectInterfaces=['ip'],groupids=[65]):
  10. myfile.write(", \""+host['interfaces'][0]['ip']+ "\"")
  11. myfile.write( "]{" +'\n')
  12. #------------------------------
  13. Вот так выглядит /etc/logstash/conf.d/filter.conf до первой работы анзибля
  14. filter {
  15. #MIKMIKMIK
  16. mutate {
  17. add_tag => [ "mikrotik"]
  18. }
  19. }
  20.  
  21. date {
  22. match => [ "timestamp" , "dd/MMM/YYYY:HH:mm:ss Z" ]
  23. }
  24. }
  25. ----------------------------------
  26. #playbook
  27. - hosts: localhost
  28. tasks:
  29. - name: get ips from zabbix
  30. ansible.builtin.script: /usr/scripts/get_mik_ips.py
  31.  
  32. - name: get lostash string from file
  33. ansible.builtin.shell: cat /usr/scripts/mik_strs
  34. register: logstash_str
  35.  
  36. - hosts: LogServer
  37. become: true
  38. become_method: su
  39. become_user: root
  40.  
  41. tasks:
  42. - name: add block to logstash
  43. blockinfile:
  44. path: /etc/logstash/conf.d/filter.conf
  45. insertafter: "#MIKMIKMIK"
  46. block: |
  47. {{ hostvars['localhost']['logstash_str'].stdout_lines[0] }}
  48. register: logstash_conf
  49. - name: restart logstash
  50. ansible.builtin.shell: systemctl restart logstash.service
  51. when: logstash_conf.changed
  52. -------------------------------
  53. #включит отсылку логов на всех микротиках
  54. - hosts: Mikrotiks-test
  55. vars_files:
  56. - /etc/ansible/vars/new_mik_pass_vault.yml
  57. connection: network_cli
  58. gather_facts: false
  59. tasks:
  60. - set_fact: command_line="action set 3 remote=10.222.180.100 remote-port=5045 src-address= {{ inventory_hostname }}"
  61. - name: do command
  62. routeros_command:
  63. commands:
  64. - /system logging {{ command_line }}
  65.  
RAW Paste Data