Skip to content

Instantly share code, notes, and snippets.

@INDIAN2020
Last active June 20, 2024 05:30
Show Gist options
  • Save INDIAN2020/dacce18c8883d84fc5428dd5aef493e3 to your computer and use it in GitHub Desktop.
Save INDIAN2020/dacce18c8883d84fc5428dd5aef493e3 to your computer and use it in GitHub Desktop.
Its my snippets for all and SREs
---
# Ansible: Add IPs from inventory to /etc/hosts of all nodes:
- name: Add IP address of all hosts to all hosts
lineinfile:
dest: /etc/hosts
regexp: '.*{{ item }}$'
line: "{{ hostvars[item].ansible_host }} {{item}}"
state: present
when: hostvars[item].ansible_host is defined
with_items: "{{ groups.all }}"
# OR
- name: Add IP address of all hosts to all hosts
lineinfile:
dest: /etc/hosts
regexp: '.*{{ item }}$'
line: "{{ hostvars[item].ansible_host }} {{item}}"
state: present
when: hostvars[item].ansible_host is defined
with_items: "{{ groups.all }}"
---
# Ansible - check if disk is mounted, if not format it
- name: Create /data
ansible.builtin.file:
path: /data
state: directory
owner: root
group: root
mode: '0751'
- name: Create an xfs filesystem on /dev/sda5
community.general.filesystem:
fstype: xfs
state: present
dev: /dev/sda5
- name: Fetch the UUID of /dev/sda5
command: blkid -s UUID -o value /dev/sda5
changed_when: false
register: blkid_out
- name: Mount /dev/sda5 by UUID
ansible.posix.mount:
path: /data
src: UUID={{ blkid_out.stdout }}
fstype: xfs
opts: defaults
state: mounted
---
# installs and configures an Nginx web server
- name: Configure nginx web server
hosts: servers
become: true
become_user: root
become_method: sudo
tasks:
- name: Install nginx
ansible.builtin.apt:
name: nginx
state: latest
- name: Comment out specific line in nginx config
ansible.builtin.lineinfile:
path: /etc/nginx/sites-available/default
regexp: '^(\s*listen\s+\[::\]:80\s+default_server;)'
line: "# {{ item }}"
loop:
- "listen [::]:80 default_server;"
- name: Restart Nginx service
ansible.builtin.service:
name: nginx
state: restarted
- name: Start Nginx
ansible.builtin.service:
name: nginx
state: started
enabled: yes
- name: Deploy web app
ansible.builtin.copy:
src: index.html
dest: /var/www/html/
---
# checks disk space on remote servers and deletes specified files if the space is above a certain threshold
- name: Storage
hosts: servers
become: yes
tasks:
- name: Check /tmp freespace
shell: sudo df | grep /dev/mapper/ubuntu--vg-ubuntu--lv | awk '{print $5}';
register: tmp_freespace
- name: Print the output of the shell command
debug:
var: tmp_freespace.stdout
- fail:
msg: '"{{ tmp_freespace.stdout }}" value'
when: tmp_freespace.stdout|float is gt 50
- name: Remove
shell: rm /home/krunal/*;
register: ls_output
- name: Print the output of the shell command
debug:
var: ls_output.stdout
---
# Formatting and mounting persistent disks on GCE with Ansible
- name: Check if disk exists
shell: "file -sL /dev/disk/by-id/google-"
register: disk_exists
- name: Format disk
shell: "mkfs.ext4 -m 0 -F -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/disk/by-id/google-"
when: "'UUID=' not in disk_exists.stdout"
- name: Create mount directory
file:
dest: "/mnt/disks/"
state: directory
owner: root
group: root
mode: 0755
- name: Mount drive
mount:
path: "/mnt/disks/"
src: "/dev/disk/by-id/google-"
fstype: ext4
opts: discard,defaults
state: mounted
---
# How to identify the hard drive with the OS installed using Ansible?
- name: display rootfs partition
hosts: all
become: yes
gather_facts: yes
tasks:
- name: print root partition
shell: echo "{{ ansible_cmdline.root }}" > /tmp/rootfs_part
---
- name: Create a csv file and put headers
lineinfile:
dest: "{{ output_path }}/{{ filename }}"
line: >
hostname,all_ipv4_addresses,all_ipv6_addresses,
model,serialnum,version,
image,iostype,filesystems,
filesystems_info_spacetotal_kb,filesystems_info_spacefree_kb,
memtotal_mb,memfree_mb,neighbors,
system,api,python_version
create: yes
state: present
when: inventory_hostname == groups.all[0]
#Write the device facts to CSV file
- name: Output Information to CSV file
lineinfile:
dest: "{{ output_path }}/{{ filename }}"
line: "{{ csv_data }}"
when: ansible_net_iostype == 'IOS-XE'
- name: Output Information to CSV file
lineinfile:
dest: "{{ output_path }}/{{ filename }}"
line: "{{ csv_data }}"
when: ansible_net_iostype == 'IOS'
#Remove any blank newlines from the file, only runs once on the first node
- name: Remove blank lines from csv file
lineinfile:
path: "./{{ output_path }}/{{ filename }}"
state: absent
regex: '^\s*$'
when: inventory_hostname == groups.all[0]
---
#
---
# ansible v2.11
- name: Write ios_facts into a csv file
hosts: c29k
connection: network_cli
gather_facts: yes
vars_files:
- vault.yml
vars:
output_path: "./reports/"
filename: "device_report_{{ date }}.csv"
tasks:
- name: CSV - Generate output filename
set_fact: date="{{lookup('pipe','date +%Y%m%d')}}"
run_once: true
- name: CSV - Create file and set the header
lineinfile:
dest: "{{ output_path }}/{{ filename }}"
line:
hostname,image,iostype,model,serialnum,system,version,spacefree_kb,spacetotal_kb
create: yes
state: present
- name: CSV - Get IOS devices facts
set_fact:
csv_tmp: >
{{ ansible_net_hostname }},{{ ansible_net_image }},{{ ansible_net_iostype }},{{ ansible_net_model }},{{ ansible_net_serialnum }},{{ ansible_net_system }},{{ ansible_net_version }},{{ ansible_net_filesystems_info['flash:']['spacetotal_kb'] }},{{ ansible_net_filesystems_info['flash:']['spacefree_kb'] }}
- name: CSV - Write information into .csv file
lineinfile:
insertafter: EOF
dest: "{{ output_path }}/{{ filename }}"
line: "{{ csv_tmp }}"
- name: CSV - Blank lines removal
lineinfile:
path: "./{{ output_path }}/{{ filename }}"
state: absent
regex: '^\s*$'
---
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment