vagrant provisioning with ansible error - vagrant

I'm getting the following error on
==> default: Configuring cache buckets...
==> default: Running provisioner: ansible...
The executable 'ansible-playbook' Vagrant is trying to run was not
found in the PATH variable. This is an error. Please verify
this software is installed and on the path.
I'm using vagrant-cachier plugin for composer vendor caching and ansible is installed. What could be the problem?

As #ydaetskcoR said, you're missing Ansible on the host machine. Alternatively, you can run the playbooks locally, but the provisioner that ships with Vagrant doesn't support that, so you'll have to do it with a shell provisioner:
config.vm.synced_folder "ansible", "/opt/ansible"
config.vm.provision "ansible", type: "shell" do |s|
s.inline = <<SCRIPT
hash ansible-playbook &> /dev/null
if [ $? -eq 0 ]; then
echo Ansible already installed.
else
echo $(date +"%T"): Updating APT database.
apt-get update &> /dev/null
echo $(date +"%T"): Installing Python and pip.
apt-get -y install python-pip python-dev &> /dev/null
echo $(date +"%T"): Installing Ansible via pip.
pip install ansible &> /dev/null
fi
mkdir -p /etc/ansible
hostname > /etc/ansible/hosts
echo $(date +"%T"): Executing Ansible playbook.
ansible-playbook /opt/ansible/playbook.yml --connection=local
SCRIPT
end

Related

Ansible : Must install the sshpass program

Ansible command:
ansible all -m module-name -o -e "ansible_user=username ansible_password=password"
Giving following error :
host-ip | FAILED! => {"msg": "to use the 'ssh' connection type with passwords, you must install the sshpass program"}
Install sshpass:
apt-get update
apt-get- install sshpass
if not This error can solved by exporting environment variable.
export ANSIBLE_HOST_KEY_CHECKING=False
If not try to create a file ansible.cfg in your current folder with the following contents:
[defaults]
host_key_checking = false

debugging a vagrant setup bash script

I have a Vagrantfile with a setup script as below. I've run it several times with vagrant up and I know the first few commands work, but later ones I know did not succeed.
Vagrantfile:
# -*- mode: ruby -*-
VAGRANTFILE_API_VERSION = "2"
$setup = <<END
# some bash code
# ...
# something that somehow never runs
sudo chown -R vagrant:vagrant /some/path
END
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "puppetlabs/centos-7.0-64-nocm"
config.vm.define "client", primary: true do |client|
client.vm.hostname = "client.example.com"
client.vm.network :private_network, ip: "192.168.250.10"
client.vm.provision "shell", inline: $setup
end
end
Is there any way to see what errors happen when scripts are run on vagrant up?
I tried vagrant up --debug &> vagrant.log but the output was literally thousands of lines and I couldn't find anything useful (or didn't know what to look for, which is more likely).
echo should do the trick and display messages from your shell file.
echo "running update ..."
sudo apt-get update
echo "running install lib ..."
sudo apt-get install --yes git-all libreadline-dev build-essential curl git m4 python-setuptools ruby texinfo libbz2-dev libcurl4-openssl-dev libexpat-dev libncurses-dev zlib1g-dev
echo "install rvm ..."
curl -L https://get.rvm.io | bash -s stable --ruby=2.0.0
all echo messages will be displayed in the output when running the shell provisioning
If you need to redirect stderr to stdout you can do something like grep * 2>&1 -
ref: http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html
While Frédéric Henri's answer is definitely recommended and helpful, vagrant also comes with some built in debugging help which you can read more about here, but basically you can vagrant up with a debug flag for more information:
vagrant up --debug

ansible sudo: sorry, you must have a tty to run sudo

I need to run playbooks on Vagrant boxes and on aws when I setup environment with cloud formation.
In Vagrant file I use ansible-local and everything works fine
name: Setup Unified Catalog Webserver
hosts: 127.0.0.1
connection: local
become: yes
become_user: root
roles: generic
However when I create instance in AWS the ansible playbook fails with error:
sudo: sorry, you must have a tty to run sudo
This happen because it is run as root and it doesnt have tty. But I dont know how to fix it without making change in /etc/sudoers to allow !requiretty
Is there any flags I can setup in ansible.cfg or in my Cloud Formation template?
"#!/bin/bash\n", "\n", "
echo 'Installing Git'\n","
yum --nogpgcheck -y install git ansible htop nano wget\n",
"wget https://s3.eu-central-1.amazonaws.com/XXX -O /root/.ssh/id_rsa\n",
"chmod 600 /root/.ssh/id_rsa\n",
"ssh-keyscan 172.31.7.235 >> /root/.ssh/known_hosts\n",
"git clone git#172.31.7.235:something/repo.git /root/repo\n",
"ansible-playbook /root/env/ansible/test.yml\n
I was able to fix this by setting the transport = paramiko configuration in ansible.cfg.
I have found the following solutions for myself:
1. Change requiretty in /etc/sudoers with sed run playbooks and change it back.
"#!/bin/bash\n", "\n", "
echo 'Installing Git'\n","
yum --nogpgcheck -y install git ansible htop nano wget\n",
"wget https://s3.eu-central-1.amazonaws.com/xx/ansible -O /root/.ssh/id_rsa\n",
"chmod 600 /root/.ssh/id_rsa\n",
"ssh-keyscan 172.31.9.231 >> /root/.ssh/known_hosts\n",
"git clone git#172.31.5.254:somerepo/dev.git /root/dev\n",
"sed -i 's/Defaults requiretty/Defaults !requiretty/g' /etc/sudoers\n",
"\n",
"ansible-playbook /root/dev/env/ansible/uk.yml\n",
"\n",
"sed -i 's/Defaults !requiretty/Defaults requiretty/g' /etc/sudoers\n"
OR
2. In ansible playbook specify variable:
- name: Setup
hosts: 127.0.0.1
connection: local
sudo: {{ require_sudo }}
roles:
- generic
Run in AWS Cloud Formation template would be
"ansible-playbook -e require_sudo=False /root/dev/env/ansible/uk.yml\n"
And for Vagrant in ansible.cfg it can be specified
require_sudo=True
Also in CF template may identify who is running and the pass variable
ansible-playbook -e$(id -u |egrep '^0$' > /dev/null && require_sudo=False || require_sudo=True; echo "require_sudo=$require_sudo") /apps/ansible/uk.yml
If you need to specific connection: paramiko within just one playbook versus a global configuration in ansible.cfg, you can add connection: paramiko following in the playbook, example:
- name: Run checks after deployments
hosts: all
# https://github.com/paramiko/paramiko/issues/1369
connection: paramiko
gather_facts: True

Automatically Installing and running Ansible Local via Vagrant

I am using Vagrant and trying to enable Ansible to work with it. Because I am working in a virtualenv with Python 3.5.0, I have to use Ansible-Local – the "main" Ansible will not run on my host because I am not running Python 2.x.
Regardless, everything mostly works, except that vagrant up does not find Ansible. Here is the output from the call:
==> default: Running provisioner: ansible_local...
default: Installing Ansible...
The Ansible software could not be found! Please verify
that Ansible is correctly installed on your guest system.
If you haven't installed Ansible yet, please install Ansible
on your Vagrant basebox, or enable the automated setup with the
`install` option of this provisioner. Please check
https://docs.vagrantup.com/v2/provisioning/ansible_local.html
for more information.
However, if I use vagrant ssh to access the VM, Ansible is clearly installed:
vagrant#vagrant-ubuntu-trusty-64:~$ ansible --version
ansible 2.0.0.2
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides
I'm at a loss of what to do. I can run ansible-playbook server.yml, while SSH'd into the server, and it works correctly. However, the whole point of me using Ansible is that I could automatically run it via Vagrant. Here's the relevant portion of my Vagrantfile:
config.vm.provision "ansible_local" do |ansible|
ansible.install = true
ansible.version = "latest"
ansible.sudo = true
ansible.playbook = "server.yml"
end
Any suggestions are greatly appreciated. Thank you!
By bringing the box up in debug mode:
VAGRANT_LOG=debug vagrant up
Shows:
DEBUG ssh: Re-using SSH connection.
INFO ssh: Execute: ansible-galaxy --help && ansible-playbook --help (sudo=false)
DEBUG ssh: stderr: ERROR! Missing required action
DEBUG ssh: stdout: Usage: ansible-galaxy [delete|import|info|init|install|list|login|remove|search|setup] [--help] [options] ...
Options:
-h, --help show this help message and exit
-v, --verbose verbose mode (-vvv for more, -vvvv to enable connection
debugging)
--version show program's version number and exit
DEBUG ssh: Exit status: 5
ansible-galaxy --help is the problem as it expected an action. Because that failed, vagrant thinks that there was a problem with the install.
It looks like this has been fixed on the master branch and will be in the next version.
What you could try is:
# -*- mode: ruby -*-
# vi: set ft=ruby :
$install_ansible = <<SCRIPT
apt-get -y install software-properties-common
apt-add-repository ppa:ansible/ansible
apt-get -y update
apt-get -y install ansible
SCRIPT
Vagrant.configure(2) do |config|
config.vm.box = 'ubuntu/trusty64'
config.vm.provision 'shell', inline: $install_ansible
# Patch for https://github.com/mitchellh/vagrant/issues/6793
config.vm.provision "shell" do |s|
s.inline = '[[ ! -f $1 ]] || grep -F -q "$2" $1 || sed -i "/__main__/a \\ $2" $1'
s.args = ['/usr/bin/ansible-galaxy', "if sys.argv == ['/usr/bin/ansible-galaxy', '--help']: sys.argv.insert(1, 'info')"]
end
config.vm.provision :ansible_local do |ansible|
ansible.sudo = true
ansible.playbook = 'server.yml'
end
end
note: Credit to MasonM for the
following snippet:
# Patch for https://github.com/mitchellh/vagrant/issues/6793
config.vm.provision "shell" do |s|
s.inline = '[[ ! -f $1 ]] || grep -F -q "$2" $1 || sed -i "/__main__/a \\ $2" $1'
s.args = ['/usr/bin/ansible-galaxy', "if sys.argv == ['/usr/bin/ansible-galaxy', '--help']: sys.argv.insert(1, 'info')"]
end
Or another option while waiting for the fix to be released is building vagrant from source.

Setting up Shell Script for Vagrant Setup

Trying to write a shell script to setup my server environment on Ubuntu through Vagrant and am running into a problem where the script ends unexpectedly. I added the path to the shell script in Vagrant's provision config option.
Vagrant:
# Specify our provision script
config.vm.provision "shell", path: "scripts/bootstrap.sh"
My script:
#!/bin/bash
# Install dependencies for Ruby
sudo apt-get update
sudo apt-get install -y git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common
# Setting up rbenv
echo 'Setting up rbenv'
git clone git://github.com/sstephenson/rbenv.git .rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL
After running, I expect the repository to be cloned into the .rbenv folder and to have rbenv added to $PATH in ~/.bashrc along with the rbenv init function evaluated and put into ~/.bashrc. However, when the script is executed on Vagrant's provision step I end up with the script just cloning the git repository and then terminating without executing anything else in my script.
Output:
==> default: Processing triggers for libc-bin (2.19-0ubuntu6) ...
==> default: Setting up rbenv
==> default: Cloning into '.rbenv'...
And then the script terminates and ~/.bashrc is left unchanged. I was wondering how I can change my shell script so that it will perform the action I want (which is adding rbenv to ~/.bashrc). Any ideas?
As mklement0 said, the script does not run as the vagrant user: it runs as root.
If you want to run the script as the vagrant user you need privileged: false.
config.vm.provision :shell, privileged: false, path: "scripts/bootstrap.sh"
As mklement0 said: use set -xv to debug your provisioning scripts.
If you want to run as another user, don't forget that su user won't work: How do I use su to execute the rest of the bash script as that user?
If you want execute some script like vagrant user, try this.
su vagrant -l -c "echo Hello world"

Resources