Skip to content

Instantly share code, notes, and snippets.

@neofob
Forked from rothgar/main.yml
Last active July 24, 2019 20:47
Show Gist options
  • Save neofob/285f3e6061d53fcba6d19a087780c50e to your computer and use it in GitHub Desktop.
Save neofob/285f3e6061d53fcba6d19a087780c50e to your computer and use it in GitHub Desktop.
Generate /etc/hosts with Ansible
# 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
- name: "Build hosts file"
lineinfile: dest=/etc/hosts regexp='.*{{ item }}$' line="{{ hostvars[item]['ansible_default_ipv4']['address'] }} {{item}}" state=present
when: hostvars[item]['ansible_facts']['default_ipv4'] is defined
with_items:
- "{{ groups['all'] }}"
@neofob
Copy link
Author

neofob commented May 17, 2019

# Debug vars
---
- hosts: '{{ target }}'
  tasks:
  - name: Display all variables/facts known for a host
    debug:
      var: hostvars[inventory_hostname]
      verbosity: 1

@neofob
Copy link
Author

neofob commented May 20, 2019

@neofob
Copy link
Author

neofob commented Jul 24, 2019

---
# filename: debug_vars.yml
# A debug playbook to print all vars
# ansible-playbook -i vagrant.py --limit=special_vm ./debug_vars.yml
# ansible-playbook -i digital_ocean.py --limit=[that_vm|vm_tag] ./debug_vars.yml
- hosts: all
  tasks:
    - name: print out debug info
      debug:
        var: groups

    - name: print out debug info
      debug:
        var: hostvars

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment