When I run this simple Ansible playbook:
- name: EC2 Test Example
hosts: localhost
connection: local
gather_facts: False
tasks:
- name: EC2 Instance
ec2:
# Amazon EC2 key pair name
key_name: my-key-pair
# Amazon EC2 Security Group
group: my-security-group
instance_type: t2.micro
# Latest from https://wiki.debian.org/Cloud/AmazonEC2Image/Jessie
image: ami-221ea342
wait: yes
register: ec2
I run with venv/bin/ansible-playbook -i localhost, playbook.yml:
PLAY [EC2 Test Example] ********************************************************
TASK [EC2 Instance] ************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "boto required for this module"}
to retry, use: --limit #/Users/admin/temp/ansec2/playbook.retry
PLAY RECAP *********************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=1
So obviously, I have boto installed in the venv that I'm using as well as my default system Python:
➜ ansec2 venv/bin/pip list
Package Version
--------------- --------
ansible 2.2.1.0
boto 2.45.0
boto3 1.4.4
botocore 1.5.4
...
I've read a few similar posts and I don't see a working solution.
The root cause of your problem is the -i localhost, hack. You don't need to use it anymore in Ansible.
You can just run:
ansible-playbook playbook.yml
And with connection: local in the play Ansible will use the Python executable set by venv.
When you use the -i localhost, hack, Ansible calls its default /usr/bin/python.
In this case you still can add the ansible_python_interpreter parameter to tell Ansible to use this specific environment:
ansible-playbook -i localhost, playbook.yml --extra-vars "ansible_python_interpreter=/Users/admin/temp/ansec2/venv/bin/python"
But I think you should avoid it and use the first method.
In my case the message was because I was missing boto, although I had boto3. So I did pip install boto and that fixed it.
If you already have boto installed for the python interpreter you want to use (as the OP does), then you can tell Ansible to use that python interpreter like so:
ansible-playbook --extra-vars "ansible_python_interpreter=/path/to/desired/python" playbook.yml
If you want to use python3, this can be
ansible-playbook --extra-vars "ansible_python_interpreter=$(command -v python3)" playbook.yml
If instead you do not have boto installed yet, you must install it with pip first before running your playbook. If you want to install boto for your python3 interpreter, you can do so with this command:
python3 -m pip install boto
If you want to install boto for a different python interpreter, use
/path/to/desired/python -m pip install boto
Related
I'm trying to create a droplet on DigitalOcean using Ansible. I have written the following script for that
- name: launch DO droplet
hosts: localhost
gather_facts: False
tasks:
- name: spin up DO droplet
local_action:
module: digital_ocean
state=present
command=droplet
name=test1
api_token=***
ssh_key_ids=DigitalOcean_MAC
size_id=2gb
region_id=ams2
image_id=ubuntu-18-04-x64
wait_timeout=500
register: my_droplet
- name: print info about my_droplet
local_action:
module: debug
msg="ID is {{ my_droplet.droplet.id }} IP is {{ my_droplet.droplet.ip_address }}"
- name: Add new droplet to host group
local_action: add_host hostname={{ my_droplet.droplet.ip_address }} groupname=launched
- name: Wait for SSH to come up
local_action: wait_for host={{ my_droplet.droplet.ip_address }} port=22 delay=60 timeout=320 state=started
I'm executing it via
ansible-playbook create_droplet.yml -c local -i localhosts
But I'm always getting the following error
> PLAY [launch DO droplet]
> ****************************************************************************************************************
>
> TASK [spin up DO droplet]
> *************************************************************************************************************** fatal: [localhost -> localhost]: FAILED! => {"changed": false, "msg":
> "dopy >= 0.3.2 required for this module"} to retry, use: --limit
> #/Users/wim/Dropbox/Programming/Ansible/DigitalOcean/create_droplet.retry
>
> > PLAY RECAP
> > ****************************************************************************************************************************** localhost : ok=0 changed=0 unreachable=0
> > failed=1
I'm installing that dopy thing using:
sudo pip install 'dopy>=0.3.5,<=0.3.5'
But also that does not help.
Any suggestions what's wrong...or is there another way to create a droplet on DigitalOcean.
sudo pip install 'dopy>=0.3.5,<=0.3.5'
Won't help without knowing what ansible_python_interpreter you are using, since -c local requires the local that ansible is using to contain the module, and not whatever random python you ran pip against. I have a strong suspicion ansible will default to /usr/bin/python which might be fine, but I'm guessing in your circumstance it's not otherwise you wouldn't be asking this question.
It is very likely you can achieve success via ansible-playbook -e ansible_python_interpreter=$(which python) ... since the pip from your $PATH is probably the same from the python on your path. You can, of course, be certain via python -c "import dopy" in that same terminal and ensure nothing explodes.
I'm trying to retrieve some information from a cisco switch via snmp_facts module (yes pysnmp is installed on my ansible host). I keep getting this error:
TASK [snmp_facts] ********************************************************************************
fatal: [10.1.1.1]: FAILED! => changed=false
msg: Missing required pysnmp module (check docs)
This is the command I am running:
ansible 192.168.1.11 -m snmp_facts -a 'community=blah host={{ inventory_hostname }} version=v2c' -k
From playbooks I wrote earlier, I used delegate_to: localhost but haven't been successful, it doesn't look like a valid option
pysnmp is installed on my ansible host
If that's true, you'll need to have ansible run that module using the python that contains pysnmp, not the one that is running ansible (as they can, and very often are, different)
It's close to what #larsks said:
ansible -c local -i localhost, \
-e ansible_python_interpreter=/the/path/to/the/pysnmp/python ...
I am using below ansible yml file to install python, pip, etc.
roles/python/main.yml:
---
- name: python
apt:
pkg: python
- name: python-pip
apt:
pkg: python-pip
- name: mongopy
pip:
pkg: mongopy
- name: mtools
pip:
pkg: mtools
when I run ansible-playbook on this script, I get below
PLAY [ec2] ***********************************************************************************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************************************************************************
ok: [xxxxx.ap-southeast-2.compute.amazonaws.com]
PLAY RECAP ***********************************************************************************************************************************************************************************************
xxxxxap-southeast-2.compute.amazonaws.com : ok=1 changed=0 unreachable=0 failed=0
there is no error on them but I checked these apps are not installed on the remote host. What wrong with my yml file? Is there any place I can check what the error is?
below is my playbook:
python.yml:
---
- hosts: ec2
remote_user: ubuntu
roles:
- python
below is the command I run:
ansible-playbook -i hosts python.yml
There are no tasks in your python role. Please have a look at the role structure.
If roles/x/tasks/main.yml exists, tasks listed therein will be added to the play
Tasks file (main.yml) should be placed in the tasks subdirectory of the role, not in the main role's directory.
And this has nothing to do with how you described the problem (installing Python or Pip). Even if you replaced the tasks with a single debug task which displays Hello world by default, it would not run.
when trying to run ansible on cloud9,
some of my task have:
sudo_user: emr-user
HOSTS file:
[development]
localhost ansible_connection=local ansible_ssh_user=ubuntu
Running with:
ansible-playbook -i hosts site.yml --limit=development
keeps failing on this task with:
failed: [localhost] => {"failed": true, "parsed": false}
[sudo via ansible, key=zacflhyhixxhiajrlmtitjxgpxqimnmn] password:
I believe it is related to the fact the cloud9 runs on password-less ubuntu root
I was able to bypass it using sudo su and then running:
ansible-playbook -i hosts site.yml --limit=development
but it doesn't feel right. any other ideas?
I am testing with ansible, what I'm trying to do is install apache2 on another ubuntu server, I already have the group "test" defined with 1 ip. but what happens is that ansible throws me some errors when executing it, I've searched a lot of sites and a lot of people have had this issue, but on different situations and I amd starting to get frustrated with it. Can somebody help me?
Ansible Playbook:
---
- hosts: test
sudo: yes
tasks:
- name: Check if Im sudo
command: echo $USER
- name: install packages
apt: name:apache2 update_cache=yes state=latest
notify: start apache2
handlers:
- name: start apache2
service: name=apache2 state=started
STDOUT
root#ip-172-31-35-33:/etc/ansible/example# ansible-playbook example.yml
PLAY [test] *******************************************************************
GATHERING FACTS ***************************************************************
ok: [172.31.36.176]
TASK: [Check if Im sudo] ******************************************************
changed: [172.31.36.176]
TASK: [install packages] ******************************************************
failed: [172.31.36.176] => {"failed": true}
msg: this module requires key=value arguments (['name:apache2', 'update_cache=yes', 'state=latest'])
FATAL: all hosts have already failed -- aborting
PLAY RECAP ********************************************************************
to retry, use: --limit #/root/example.retry
172.31.36.176 : ok=2 changed=1 unreachable=0 failed=1
BTW, the host is reachable, I can ssh into it, even with ansible, this is the proof
root#ip-172-31-35-33:/etc/ansible/example# ansible -m shell -a "ifconfig | grep 'inet addr'" test
172.31.36.176 | success | rc=0 >>
inet addr:172.31.36.176 Bcast:172.31.47.255 Mask:255.255.240.0
inet addr:127.0.0.1 Mask:255.0.0.0
another thing is that I'm able to install apache2 by hand on the other server, BUT IT IS NOT INSTALLED BECAUSE I WANT TO INSTALL IT USING ANSIBLE
Thanks
Within an individual task, Ansible requires you to make the choice between standard YAML syntax and their own parsed version with equals signs. In this task, you are mixing the two:
- name: install packages
apt: name:apache2 update_cache=yes state=latest
notify: start apache2
This could be either written:
- name: install packages
apt:
name: apache2
update_cache: yes
state: latest
notify: start apache2
Or:
- name: install packages
apt: name=apache2 update_cache=yes state=latest
notify: start apache2
YAML also allows for using bracket and comma syntax to allow you to specify your key-value information on the same line:
- name: install packages
apt: {name: apache2, update_cache: yes, state: latest}
notify: start apache2
Any of these are valid.
You're using a colon where an equal is needed. You need to change the name:apache2 to name=apache2.