Skip to content

Instantly share code, notes, and snippets.

@nshores
Last active November 30, 2020 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nshores/e72d0ad26fac4dc8a324c8744768d0e7 to your computer and use it in GitHub Desktop.
Save nshores/e72d0ad26fac4dc8a324c8744768d0e7 to your computer and use it in GitHub Desktop.
check_snmp_rev.yml
---
- hosts: test
become: yes
#Is sudo actually needed? Only use it when needed.
become_method: sudo
#Use variables whenever possible. You can place them in the task, or in a outside file at the inventory level.
vars:
snmp_path: "/etc/snmp/snmpd_conf/snmpd.conf"
backup_path: "/etc/snmp/snmpd_conf/snmpd.conf.bak"
tasks:
- name: Check if snmp.conf exits
stat:
path: "{{ snmp_path }}"
register: snmpconf
#Let's break when the file is missing since we can't proceed.
- name: Fail if file missing
fail: msg="snmp.conf missing"
when: snmpconf.stat.exists == false
#Use the copy module instead of the command module - always use native modules when possible
- name: Make Backup of snmpd.conf
copy:
src: "{{ snmp_path}}"
dest: "{{ backup_path }}"
#This is not needed because the playbook breaks if the file isn't there.
#when: snmpconf.stat.exists == true
#This is not needed - see below comments
# - name: Check if Solarwinds is allowed
# command: grep "172.27.242.0" {{ snmp_path }}
# register: exist
# ignore_errors: yes
# when: backup.rc == 0
- name: SNMP Config
lineinfile:
path: "{{ snmp_path }}"
insertafter: 'raleyssnmp'
line: com2sec myUser 172.27.242.0/23 raleyssnmp
#Lineinfile will only add the line if it doesn't exist, and won't re-add it if it already exists - that's how ansible works by default (itempotence)
# when: exist.rc != 0
- name: Restart SNMPD
service:
name: snmpd
state: restarted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment