├── .gitignore ├── LICENSE.txt ├── Makefile ├── README.md ├── bin ├── ansible-playbook-vagrant ├── ansible-playbookv ├── ansible-vagrant ├── ansible-vagrant-update-hosts └── ansiblev └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.egg-info 2 | dist/ 3 | MANIFEST 4 | README.rst 5 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Michael Contento 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | doc: 2 | python -c 'import pypandoc; print pypandoc.convert("README.md", "rst")' > README.rst 3 | 4 | register: doc 5 | python setup.py register 6 | 7 | upload: doc 8 | python setup.py sdist upload 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [ansible-vagrant][] 2 | 3 | Tired of the rather limited `vagrant provision` with the [ansible][] provider? 4 | Tired of keeping your `hosts` file up to date? [ansible-vagrant][] to the rescue! 5 | 6 | ## Usage 7 | 8 | $ pip install ansible-vagrant 9 | $ cd /path/to/project/with/a/vagrantfile 10 | 11 | $ ansible-vagrant-update-hosts 12 | $ cat hosts_vagrant 13 | [vagrant] 14 | vagrant ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222 ansible_ssh_use... 15 | 16 | $ ansible-vagrant all -m ping 17 | # Same as: ansible-vagrant-update-hosts \ 18 | # && ansible all --inventory=./hosts_vagrant -m ping 19 | 20 | $ ansible-playbook-vagrant playbook.yml 21 | # Same as: ansible-vagrant-update-hosts \ 22 | # && ansible-playbook --extra-vars="vagrant_host=true" \ 23 | # --inventory=./hosts_vagrant playbook.yml 24 | 25 | ## Notes 26 | 27 | - All `*-vagrant` commands use `ANSIBLE_HOST_KEY_CHECKING=False` to prevent 28 | errors with `vagrant destroy && vagrant up` 29 | - It's a good idea to put `hosts_vagrant` under `.gitignore` 30 | - `ansible-playbook-vagrant` adds a global variable named `vagrant` 31 | - Use `when: vagrant_host is (not) defined` to run stuff based on the current 32 | environment 33 | 34 | ## Shortcuts 35 | 36 | * `ansiblev` for `ansible-vagrant` 37 | * `ansible-playbookv` for `ansible-playbook-vagrant` 38 | 39 | [ansible]: https://github.com/ansible/ansible 40 | [ansible-vagrant]: https://github.com/michaelcontento/ansible-vagrant 41 | -------------------------------------------------------------------------------- /bin/ansible-playbook-vagrant: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -e 3 | 4 | ansible-vagrant-update-hosts 5 | ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook \ 6 | --extra-vars="vagrant_host=true" \ 7 | --inventory=./hosts_vagrant "$@" 8 | -------------------------------------------------------------------------------- /bin/ansible-playbookv: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ansible-playbook-vagrant "$@" 4 | -------------------------------------------------------------------------------- /bin/ansible-vagrant: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -e 3 | 4 | ansible-vagrant-update-hosts 5 | ANSIBLE_HOST_KEY_CHECKING=False ansible all --inventory=./hosts_vagrant "$@" 6 | -------------------------------------------------------------------------------- /bin/ansible-vagrant-update-hosts: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -e 3 | 4 | HOSTNAME="vagrant" 5 | HOSTSFILE="./hosts_vagrant" 6 | 7 | CONFIG=$(vagrant ssh-config) 8 | HOST=$(echo "$CONFIG" | egrep -o "HostName .+" | cut -d" " -f2-) 9 | PORT=$(echo "$CONFIG" | egrep -o "Port .+" | cut -d" " -f2-) 10 | USER=$(echo "$CONFIG" | egrep -o "User .+" | cut -d" " -f2-) 11 | KEY=$(echo "$CONFIG" | egrep -o "IdentityFile .+" | cut -d" " -f2-) 12 | 13 | REPLACE_REGEX="^$HOSTNAME .*" 14 | LINE="$HOSTNAME \ 15 | ansible_ssh_host=$HOST \ 16 | ansible_ssh_port=$PORT \ 17 | ansible_ssh_user=$USER \ 18 | ansible_ssh_private_key_file=$KEY" 19 | 20 | touch "$HOSTSFILE" 21 | FOUND=$(egrep "$REPLACE_REGEX" $HOSTSFILE | wc -l) 22 | 23 | if [ $FOUND -eq 0 ]; then 24 | printf "[vagrant]\n$LINE" >> $HOSTSFILE 25 | else 26 | sed -i".backup" -e "s#^$HOSTNAME .*#$LINE#g" $HOSTSFILE 27 | rm -f "$HOSTSFILE.backup" 28 | fi 29 | -------------------------------------------------------------------------------- /bin/ansiblev: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ansible-vagrant "$@" 4 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from distutils.core import setup 4 | 5 | try: 6 | long_description = open('README.rst').read() 7 | except: 8 | long_description = None 9 | 10 | setup(name='ansible-vagrant', 11 | version='1.3.0', 12 | description='Simple helper to use ansible with vagrant', 13 | long_description=long_description, 14 | url='https://github.com/michaelcontento/ansible-vagrant', 15 | author='Michael Contento', 16 | author_email='michaelcontento@gmail.com', 17 | license='GPLv3', 18 | install_requires=['ansible'], 19 | keywords=['vagrant', 'ansible'], 20 | scripts=[ 21 | 'bin/ansible-vagrant-update-hosts', 22 | 'bin/ansible-playbook-vagrant', 23 | 'bin/ansible-playbookv', 24 | 'bin/ansible-vagrant', 25 | 'bin/ansiblev', 26 | ], 27 | classifiers=[ 28 | 'Environment :: Console', 29 | 'Environment :: Web Environment', 30 | 'Intended Audience :: Developers', 31 | 'Intended Audience :: System Administrators', 32 | 'Operating System :: OS Independent', 33 | 'Programming Language :: Python', 34 | 'Topic :: Software Development', 35 | 'Topic :: Software Development :: Build Tools', 36 | 'Topic :: System :: Clustering', 37 | 'Topic :: System :: Software Distribution', 38 | 'Topic :: System :: Systems Administration', 39 | 'Topic :: Utilities' 40 | ], 41 | ) 42 | --------------------------------------------------------------------------------