-
-
Save slmingol/88fe21fdbb4d37d96a639ce0a1295fec to your computer and use it in GitHub Desktop.
Generate /etc/hosts with Ansible
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- | |
| # Idempotent way to build a /etc/hosts file with Ansible using your Ansible hosts inventory for a source. | |
| # Will include all hosts the playbook is run on. | |
| # Inspired from http://xmeblog.blogspot.com/2013/06/ansible-dynamicaly-update-etchosts.html | |
| # https://gist.github.com/rothgar/8793800 | |
| - hosts: all | |
| tasks: | |
| - name: Build HBase /etc/hosts file | |
| lineinfile: | |
| dest: /etc/hosts | |
| regexp: '\d+(\.\d+){3}.*{{ item }}$' | |
| line: "{{ hostvars[item].ansible_default_ipv4.address }} \ | |
| {{ item }}.td.local \ | |
| {{ item }}.td.tda.com \ | |
| {{ item }}.tdv.com \ | |
| {{ item }}" | |
| state: present | |
| when: "(hostvars[item].ansible_default_ipv4 is defined) \ | |
| and (hostvars[item].ansible_default_ipv4.address is defined) \ | |
| and (ansible_virtualization_type != 'virtualbox')" | |
| with_items: "{{ groups['hbase-nodes']}}" | |
| become: yes | |
| - name: Build HBase /etc/hosts file | |
| lineinfile: | |
| dest: /etc/hosts | |
| regexp: '\d+(\.\d+){3}.*{{ item }}$' | |
| line: "{{ hostvars[item].ansible_all_ipv4_addresses[0] }} \ | |
| {{ item }}.td.local \ | |
| {{ item }}.td.tda.com \ | |
| {{ item }}.tdv.com \ | |
| {{ item }}" | |
| state: present | |
| when: "(hostvars[item].ansible_default_ipv4 is undefined) \ | |
| and (hostvars[item].ansible_all_ipv4_addresses is defined) \ | |
| and (ansible_virtualization_type != 'virtualbox')" | |
| with_items: "{{ groups['hbase-nodes']}}" | |
| become: yes | |
| - name: Build Appserver /etc/hosts file (AWS, AZure VMWare) | |
| lineinfile: | |
| dest: /etc/hosts | |
| regexp: '\d+(\.\d+){3}.*{{ item.split(".")[0] }}$' | |
| line: "{{ hostvars[item].ansible_default_ipv4.address }} \ | |
| {{ item.split('.')[0] }}.td.local \ | |
| {{ item.split('.')[0] }}.td.tda.com \ | |
| {{ item.split('.')[0] }}.tdv.com \ | |
| {{ item.split('.')[0] }}" | |
| state: present | |
| when: "(hostvars[item].ansible_default_ipv4 is defined) \ | |
| and (hostvars[item].ansible_default_ipv4.address is defined) \ | |
| and (ansible_virtualization_type != 'virtualbox') \ | |
| and ('hbase-nodes' not in group_names)" | |
| with_items: "{{ groups['all'] | difference(groups['hbase-nodes']) }}" | |
| become: yes | |
| - name: Build Appserver /etc/hosts file (AWS, AZure, VMWare, Openstack) | |
| lineinfile: | |
| dest: /etc/hosts | |
| regexp: '\d+(\.\d+){3}.*{{ item.split(".")[0] }}$' | |
| line: "{{ hostvars[item].ansible_all_ipv4_addresses[0] }} \ | |
| {{ item.split('.')[0] }}.td.local \ | |
| {{ item.split('.')[0] }}.td.tda.com \ | |
| {{ item.split('.')[0] }}.tdv.com \ | |
| {{ item.split('.')[0] }}" | |
| state: present | |
| when: "(hostvars[item].ansible_default_ipv4 is undefined) \ | |
| and (hostvars[item].ansible_all_ipv4_addresses is defined) \ | |
| and (ansible_virtualization_type != 'virtualbox') \ | |
| and ('hbase-nodes' not in group_names)" | |
| with_items: "{{ groups['all'] | difference(groups['hbase-nodes']) }}" | |
| become: yes | |
| - name: Build Appserver /etc/hosts file (Virtualbox/Vagrant) | |
| lineinfile: | |
| dest: /etc/hosts | |
| regexp: '\d+(\.\d+){3}.*{{ item.split(".")[0] }}$' | |
| line: "{{ hostvars[item].ansible_default_ipv4.address }} \ | |
| {{ item.split('.')[0] }}.td.local \ | |
| {{ item.split('.')[0] }}.td.tda.com \ | |
| {{ item.split('.')[0] }}.tdv.com \ | |
| {{ item.split('.')[0] }}" | |
| state: present | |
| when: "(hostvars[item].ansible_default_ipv4 is defined) \ | |
| and (hostvars[item].ansible_default_ipv4.addresses is defined) \ | |
| and (ansible_virtualization_type == 'virtualbox') \ | |
| and ('hbase-nodes' not in group_names)" | |
| with_items: "{{ groups['hbase-nodes'] }}" | |
| become: yes | |
| - name: Replace IPs defined with ansible_host | |
| lineinfile: | |
| dest: /etc/hosts | |
| regexp: '\d+(\.\d+){3}.*{{ item }}$' | |
| line: "{{ hostvars[item].ansible_host }} \ | |
| {{ item }}.td.local \ | |
| {{ item }}.td.tda.com \ | |
| {{ item }}.tdv.com \ | |
| {{ item }}" | |
| state: present | |
| when: "(hostvars[item].ansible_host is defined) and (ansible_virtualization_type == 'virtualbox') and ('hbase-nodes' not in group_names)" | |
| with_items: "{{ groups['hbase-nodes'] }}" | |
| become: yes | |
| ## ## vagrant - int db ############################################################################## | |
| ## ... | |
| ## ... | |
| ## ## vagrant-hostmanager-start | |
| ## 172.28.128.3 node-app1.td.local | |
| ## 172.28.128.4 node-app2.td.local | |
| ## ## vagrant-hostmanager-end | |
| ## | |
| ## 192.1.1.1 hbase-hb01.td.local hbase-hb01.td.tda.com hbase-hb01.tdv.com hbase-hb01 | |
| ## 192.1.1.2 hbase-hb02.td.local hbase-hb02.td.tda.com hbase-hb02.tdv.com hbase-hb02 | |
| ## ################################################################################################## | |
| ## | |
| ## | |
| ## ## vagrant - ext db (ansible_host) ############################################################### | |
| ## ... | |
| ## ... | |
| ## ## vagrant-hostmanager-start | |
| ## 172.28.128.3 node-app1.td.local | |
| ## 172.28.128.4 node-app2.td.local | |
| ## ## vagrant-hostmanager-end | |
| ## | |
| ## 10.153.1.1 hbase-hb01.td.local hbase-hb01.td.tda.com hbase-hb01.tdv.com hbase-hb01 | |
| ## 10.153.1.2 hbase-hb02.td.local hbase-hb02.td.tda.com hbase-hb02.tdv.com hbase-hb02 | |
| ## ################################################################################################## | |
| ## | |
| ## | |
| ## ## aws - int db ################################################################################## | |
| ## ... | |
| ## ... | |
| ## 192.168.1.1 node-app1.ec2.local | |
| ## 192.168.1.2 node-app2.ec2.local | |
| ## | |
| ## 192.1.1.1 hbase-hb01.ec2.local hbase-hb01 | |
| ## 192.1.1.2 hbase-hb02.ec2.local hbase-hb02 | |
| ## ################################################################################################## | |
| ## | |
| ## | |
| ## ## aws - ext db (ansible_host) ################################################################### | |
| ## ... | |
| ## ... | |
| ## 10.50.0.1 node-app1.ec2.local | |
| ## 10.50.0.2 node-app2.ec2.local | |
| ## | |
| ## 10.153.1.1 hbase-hb01.ec2.local hbase-hb01.td.tda.com hbase-hb01.tdv.com hbase-hb01 | |
| ## 10.153.1.2 hbase-hb02.ec2.local hbase-hb01.td.tda.com hbase-hb01.tdv.com hbase-hb02 | |
| ## ################################################################################################## | |
| ## | |
| ## | |
| ## ## vmw - ext db ################################################################################## | |
| ## ... | |
| ## ... | |
| ## 10.153.1.1 node-app1.tdv.com node-app1 | |
| ## 10.153.1.2 node-app2.tdv.com node-app2 | |
| ## | |
| ## 10.153.1.3 hbase-hb01.tdv.com hbase-hb01 | |
| ## 10.153.1.4 hbase-hb02.tdv.com hbase-hb02 | |
| ## ################################################################################################## | |
| # vim: ft=ansible: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment