Skip to content

Instantly share code, notes, and snippets.

@mrlesmithjr
Created June 15, 2015 20:24
Show Gist options
  • Select an option

  • Save mrlesmithjr/7caa07432e46282b70dd to your computer and use it in GitHub Desktop.

Select an option

Save mrlesmithjr/7caa07432e46282b70dd to your computer and use it in GitHub Desktop.
---
- hosts: all
gather_facts: false
connection: local
vars:
- cleanup_temp: true
- gather_dir: ~/gather_info
- host_vars_dir: ~/ansible/host_vars
- pysphere_script_home: library
- ssh_known_hosts_command: ssh-keyscan
- ssh_known_hosts_dir: ~/ssh_known_hosts
- ssh_known_hosts_file: ssh_known_hosts
# - datacenter: '' #defined in group_vars/all/configs
# - esxi_host: '' #defined in group_vars/all/configs
# - notes: Created by Ansible
# - vcenter_cluster: '' #defined in group_vars/all/configs
# - vcenter_hostname: '' #defined in group_vars/all/configs
# - vcenter_pass: '' #defined in group_vars/all/accounts
# - vcenter_resource_pool: '' #defined in group_vars/all/configs
# - vcenter_user: '' #defined in group_vars/all/accounts
# - vm_state: present
tasks:
- name: removing temp folder created
file: path='{{ gather_dir }}' state=absent
run_once: true
when: cleanup_temp
- name: cleaning up temp files
file: path='{{ssh_known_hosts_dir}}/{{item}}' state=absent
run_once: true
with_items:
- ssh_known_hosts.clean
when: cleanup_temp
- name: Making sure ssh_known_hosts_dir exists
file: path='{{ ssh_known_hosts_dir }}' state=directory
run_once: true
- name: Make sure the ssh_known_hosts_file exists
file: path='{{ ssh_known_hosts_dir }}/{{ ssh_known_hosts_file }}' state=touch
run_once: true
- name: Making sure gather_dir exists
file: path='{{ gather_dir }}' state=directory
run_once: true
- name: making sure host_vars/inventory_hostname file exists
stat: path='{{host_vars_dir}}/{{inventory_hostname}}'
register: inventory_hostname_file_results
- name: creating missing host_vars/inventory file
file: path='{{host_vars_dir}}/{{inventory_hostname}}' state=touch
when: not inventory_hostname_file_results.stat.exists
- name: making sure inventory_hostname folder exists
file: path='{{gather_dir}}/{{inventory_hostname}}' state=directory
- name: capturing ip addresses
script: "{{ pysphere_script_home }}/pysphere-get-vm-ips.py -s {{ vcenter_hostname }} -u {{ vcenter_user }} -p {{ vcenter_pass }} -n {{ inventory_hostname }} >> {{ gather_dir }}/vm_ip_info"
- name: capturing ip addresses for host_vars/inventory_hostname
script: "{{ pysphere_script_home }}/pysphere-get-vm-ips.py -s {{ vcenter_hostname }} -u {{ vcenter_user }} -p {{ vcenter_pass }} -n {{ inventory_hostname }} >> {{gather_dir}}/{{ inventory_hostname }}/vm_ip_info"
- name: cleaning up existing ansible_ssh_host value in host_vars/inventory_hostname
lineinfile: dest='{{host_vars_dir}}/{{inventory_hostname}}' regexp='^ansible_ssh_host=' state=absent
- name: cleaning up inventory_hostname folder vm_ip_info
shell: "sed -i -e 's|:||' {{ gather_dir }}/{{inventory_hostname}}/vm_ip_info"
- name: cleaning up inventory_hostname folder vm_ip_info
shell: "sed -i -e 's|None;|ansible_ssh_host=|' {{ gather_dir }}/{{inventory_hostname}}/vm_ip_info"
- name: adding current ansible_ssh_host to host_vars/inventory_hostname
shell: "cat {{gather_dir}}/{{inventory_hostname}}/vm_ip_info >> {{host_vars_dir}}/{{inventory_hostname}}"
- name: cleaning up bad lines in host_vars/inventory_hostname
lineinfile: dest='{{host_vars_dir}}/{{inventory_hostname}}' regexp='^{{inventory_hostname}}' state=absent
- name: copying vm_ip_info to ip_addys
shell: "cp {{ gather_dir }}/vm_ip_info {{ gather_dir }}/ip_addys"
run_once: true
- name: cleaning up vm_ip_info
lineinfile: dest='{{ gather_dir }}/vm_ip_info' regexp='^ERROR' state=absent
run_once: true
- name: cleaning up vm_ip_info
shell: "sed -i -e 's|:||' {{ gather_dir }}/vm_ip_info"
run_once: true
- name: cleaning up vm_ip_info
shell: "sed -i -e 's|None;|ansible_ssh_host=|' {{ gather_dir }}/vm_ip_info"
run_once: true
- name: cleaning up ip_addys
shell: "sed -i -e 's|:||' {{ gather_dir }}/ip_addys"
run_once: true
- name: cleaning up ip_addys
shell: "sed -i -e 's|None;||' {{ gather_dir }}/ip_addys"
run_once: true
- name: cleaning up ip_addys
lineinfile: dest='{{ gather_dir }}/ip_addys' regexp='^ERROR' state=absent
run_once: true
- name: cleaning up ip_addys
shell: "awk '{print $2}' {{ gather_dir }}/ip_addys > {{ gather_dir }}/ip_hosts.tmp"
run_once: true
- name: cleaning up ip_hosts
shell: "sed -i -e 's|;| |' {{ gather_dir }}/ip_hosts.tmp"
run_once: true
- name: cleaning up ip_hosts
shell: "awk '{print $1}' {{ gather_dir }}/ip_hosts.tmp > {{ gather_dir }}/ip_hosts"
run_once: true
- name: cleaning up ip_hosts
shell: "sort -u {{ gather_dir }}/ip_hosts > {{ gather_dir }}/ip_hosts.ready"
run_once: true
- name: copying ip_addys to ip_hostnames
shell: "cp {{ gather_dir }}/ip_addys {{ gather_dir }}/ip_hostnames"
run_once: true
- name: cleaning up ip_hostnames
shell: "awk '{print $1}' {{ gather_dir }}/ip_hostnames > {{ gather_dir }}/ip_hostnames.ready"
run_once: true
- name: Scan the public key
shell: "{{ ssh_known_hosts_command}} -f {{ gather_dir }}/ip_hosts.ready >> {{ ssh_known_hosts_dir }}/{{ ssh_known_hosts_file }}"
run_once: true
- name: sorting ssh_known_hosts and exporting to ssh_known_hosts.clean
shell: "sort -u {{ ssh_known_hosts_dir }}/{{ ssh_known_hosts_file }} > {{ ssh_known_hosts_dir }}/{{ ssh_known_hosts_file }}.clean"
run_once: true
- name: moving existing ssh_known_hosts to ssh_known_hosts.backup
shell: "mv {{ ssh_known_hosts_dir }}/{{ ssh_known_hosts_file }} {{ ssh_known_hosts_dir }}/{{ ssh_known_hosts_file }}.backup"
run_once: true
- name: copying ssh_known_hosts.clean to ssh_known_hosts
shell: "cp {{ ssh_known_hosts_dir }}/{{ ssh_known_hosts_file }}.clean {{ ssh_known_hosts_dir }}/{{ ssh_known_hosts_file }}"
run_once: true
- name: removing temp folder created
file: path='{{ gather_dir }}' state=absent
run_once: true
when: cleanup_temp
- name: cleaning up temp files
file: path='{{ssh_known_hosts_dir}}/{{item}}' state=absent
run_once: true
with_items:
- ssh_known_hosts.clean
when: cleanup_temp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment