{"id":5659,"date":"2024-02-15T21:28:00","date_gmt":"2024-02-16T03:28:00","guid":{"rendered":"https:\/\/www.incredigeek.com\/home\/?p=5659"},"modified":"2024-02-27T17:28:49","modified_gmt":"2024-02-27T23:28:49","slug":"ansible-playbook-for-linux-snmp","status":"publish","type":"post","link":"https:\/\/www.incredigeek.com\/home\/ansible-playbook-for-linux-snmp\/","title":{"rendered":"Ansible Playbook for Linux SNMP"},"content":{"rendered":"\n<p>This playbook is for installing and configuring SNMP on Ubuntu or RedHat machines<\/p>\n\n\n\n<p>Change the snmp_location and snmp_contact etc. variables.  Or define them in the inventory file, or pass them in as &#8211;extra-vars.  &#8211;extra-vars=&#8221;snmp_location=&#8217;location address&#8217; snmpv3_user=incredigeek &#8230;etc&#8221;<\/p>\n\n\n\n<p>Couple of notes<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We check to see if a read only SNMPv3 user has been created.  If so, we don&#8217;t create a new one.<\/li>\n\n\n\n<li>The snmpd service is stopped and started each time this is run<\/li>\n\n\n\n<li>You will still need to allow SNMP through the firewall.  <a href=\"https:\/\/www.incredigeek.com\/home\/configure-ufw-firewall-on-ubuntu\/\">Ubuntu<\/a> or <a href=\"https:\/\/www.incredigeek.com\/home\/setting-up-snmp-v3-on-centos\/\">Fedora<\/a><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code has-dark-gray-background-color has-background\"><code lang=\"yaml\" class=\"language-yaml line-numbers\">---\n- name: Linux SNMP Config\n  hosts: all\n  gather_facts: yes\n  become: yes\n\n# Install SNMPv3 on RHEL or Debian\/Ubuntu\n# Disable SNMP v1 and v2 on RHEL\n# Configure SNMPv3 user\n\n  vars:\n\n    # Change these!\n    snmp_location: My SNMP location\n    snmp_contact: My SNMP contact info\n    snmpv3_pass: mypassword\n    snmpv3_user: incredigeek\n\n    # These are used to disable the default public community.\n    cmnt: '#'\n    cmnt_lines:\n      - com2sec notConfigUser\n      - group   notConfigGroup\n      - view    systemview\n      - access  notConfigGroup \n\n\n  tasks: \n\n    - name: Check if SNMPv3 user exists\n      ansible.builtin.lineinfile:\n        path: \/etc\/snmp\/snmpd.conf\n        regexp: '^rouser'\n        state: absent\n      check_mode: yes\n      changed_when: false\n      register: snmpv3_user_exists\n\n    - name: Stop SNMPD Service\n      ansible.builtin.service:\n        name: snmpd\n        state: stopped\n\n    - name: RHEL SNMP Config\n      block:\n        - name: Install SNMP RHEL\n          ansible.builtin.dnf:\n            name:\n              - net-snmp\n              - net-snmp-utils\n            state: present\n\n        - name: Disable public snmp community RHEL\n          replace:\n            path: \/etc\/snmp\/snmpd.conf\n            regexp: '^{{ item }}'\n            replace: '{{ cmnt }} {{ item }}'\n          loop: \"{{ cmnt_lines }}\"\n        - name: Set SNMP Location\n          ansible.builtin.lineinfile:\n            path: \/etc\/snmp\/snmpd.conf\n            regexp: '^syslocation.*'\n            line: \"syslocation {{ snmp_location }}\"\n\n        - name: Set SNMP Contact\n          ansible.builtin.lineinfile:\n            path: \/etc\/snmp\/snmpd.conf\n            regexp: '^syscontact.*'\n            line: \"syscontact {{ snmp_contact }}\"\n        - name: Setup SNMPv3 user for RHEL\n          shell: net-snmp-create-v3-user -ro -a SHA -A '{{ snmpv3_pass }}' -x '{{ snmpv3_pass }}' -X AES {{ snmpv3_user }}\n          when: not snmpv3_user_exists.found\n\n      when: ansible_os_family == \"RedHat\"\n\n    - name: Debian SNMP Config\n      block:\n        - name: Install SNMP on Debian\n          ansible.builtin.apt:\n            pkg:\n            - snmp\n            - snmpd\n            - libsnmp-dev\n\n        - name: Modify available from address\n          ansible.builtin.lineinfile:\n            path: \/etc\/snmp\/snmpd.conf\n            regexp: '^agentAddress udp:127\\.0\\.0\\.1:161'\n            line: 'agentAddress udp:161,udp6:[::1]:161'\n\n        - name: Set SNMP Location\n          ansible.builtin.lineinfile:\n            path: \/etc\/snmp\/snmpd.conf\n            regexp: '^sysLocation.*'\n            line: \"sysLocation {{ snmp_location }}\"\n\n        - name: Set SNMP Contact\n          ansible.builtin.lineinfile:\n            path: \/etc\/snmp\/snmpd.conf\n            regexp: '^sysContact.*'\n            line: \"sysContact {{ snmp_contact }}\"\n        - name: Setup SNMPv3 user for Debian\n          shell: net-snmp-config --create-snmpv3-user -ro -a SHA -A '{{ snmpv3_pass }}' -x '{{ snmpv3_pass }}' -X AES {{ snmpv3_user }}\n          when: not snmpv3_user_exists.found\n\n      when: ansible_os_family == \"Debian\"\n\n    - name: Enable SNMPD Service\n      ansible.builtin.service:\n        name: snmpd\n        enabled: true\n\n    - name: Start SNMPD Service\n      ansible.builtin.service:\n        name: snmpd\n        state: started\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This playbook is for installing and configuring SNMP on Ubuntu or RedHat machines Change the snmp_location and snmp_contact etc. variables. Or define them in the inventory file, or pass them in as &#8211;extra-vars. &#8211;extra-vars=&#8221;snmp_location=&#8217;location address&#8217; snmpv3_user=incredigeek &#8230;etc&#8221; Couple of notes<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5659","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5659","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/comments?post=5659"}],"version-history":[{"count":4,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5659\/revisions"}],"predecessor-version":[{"id":5728,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5659\/revisions\/5728"}],"wp:attachment":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/media?parent=5659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/categories?post=5659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/tags?post=5659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}