Why doesn't gem install fpm via ansible? - amazon-ec2

I have created an EC2 instance on Amazon Cloud and I am installing some stuffs via ansible too. But when it installs fpm using gem:
- name: install fpm
gem: name=fpm state=latest
sudo: yes
it says:
changed: [XX.XX.XXX.XXX] => {"changed": true, "name": "fpm", "state": "latest", "version": "1.3.3"}
No errors. But when I enter the instance and try to run a script it says:
fpm is mandatory, please run gem install fpm
If I do sudo gem install fpm in console of the EC2, the script runs as espected.
So what am I doing wrong? Doesn't Ansible install the fpm?

I have fixed the problem by doing
- name: install fpm
command: bash -lc "gem install fpm"
instead of
- name: install fpm
gem: name=fpm state=latest
sudo: yes
Now It does not ask for fpm anymore, it is installed. But why gem does not work?

Related

ansible returns with "Failed to import the required Python library (Docker SDK for Python: docker (Python >= 2.7) or docker-py (Python 2.6))

I am running myserver in ubuntu:
+ sudo cat /etc/os-release
NAME="Ubuntu"
VERSION="16.04.6 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.6 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial
I use ansible and when I run it I get the following error:
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Failed to import the required Python library (Docker SDK for Python: docker (Python >= 2.7) or docker-py (Python 2.6)) on dd63315fad06's Python /usr/bin/python. Please read module documentation and install in the appropriate location, for example via `pip install docker` or `pip install docker-py` (Python 2.6). The error was: No module named docker"}
when I run
python -c "import sys; print(sys.path)"
I see:
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/local/lib/python2.7/dist-packages/pip-19.2.2-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/fasteners-0.15-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/monotonic-1.5-py2.7.egg', '/usr/lib/python2.7/dist-packages']
and python versions are as follows:
+ python --version
Python 2.7.12
+ python3 --version
Python 3.5.2
Then as I see everything is fine and I am not sure why I get
"Failed to import the required Python library (Docker SDK for Python: docker (Python >= 2.7) or docker-py (Python 2.6)) on dd63315fad06's Python /usr/bin/python. Please read module documentation and install in the appropriate location, for example via `pip install docker` or `pip install docker-py` (Python 2.6). The error was: No module named docker"
in ansible?
It appears that you don't have the docker module installed.
You will need to install it via your system package manager (apt install python-docker, for example), or using pip (pip install docker).
If you have multiple Python versions, make sure that you've installed the docker module into the version that Ansible is using.
Here, in May 2021 for Ubuntu 20.04 you need to run apt install python3-docker because no python 2.x is shipped by default anymore
I have faced the same issue for the Ansible docker-compose module. I was able to fix it by selecting python3 for those tasks.
Before (Not working)
- name: Create docker service services
docker_compose:
project_src: /root/
become: true
After (Working)
We can get the python location by $which python3
- name: Create fleuntd services
docker_compose:
project_src: /root/
become: true
vars:
ansible_python_interpreter: /bin/python3
I started getting this same error in April 2021, with the release of Version 5.0 of the Docker SDK For Python. The error message was almost the exact same as the original question, with the only difference being that at the end of the error message was one of the statements:
The error was: No module named parse
or
The error was: No module named selectors
This was ultimately due to the older version of pip that Ansible was using, incorrectly installing a Python3 library on a Python2.7 setup. The fix was to pin the version of the docker Python library to something earlier than Version 5.0, and the 'websocket-client' library to something earlier than Version 1.0:
- name: Install Docker SDK for Python
pip:
name: "docker<5"
become: yes
- name: Setup more docker dependencies
pip:
name: "websocket-client<1"
become: yes
Alternatively, this set of commands would have also worked, given that Python2 was still in use:
pip install docker<5
pip install websocket-client<1
Once these older versions of the Docker SDK for Python and Websocket Client were installed, Ansible was able to again successfully manage Docker on my behalf.
In my case (Ubuntu 20 with installed docker) these commands was required
apt update
apt install python3 python3-pip
pip3 install docker docker-compose
Detailed requirements:
https://docs.ansible.com/ansible/latest/collections/community/general/docker_compose_module.html

Ansible - pip not found

I am getting this error:
TASK [pip] *********************************************************************
failed: [default] (item=urllib3) =>
{"changed": false, "item": "urllib3",
"msg": "Unable to find any of pip2, pip to use. pip needs to be installed."}
Upon a suggestion I run following command:
ansible default -a "which pip"
I get an error:
default | FAILED | rc=1 >>
non-zero return code
So I guess that means no pip installed. I tried installing pip using:
ansible default -a "easy_install pip"
I get the following error:
default | FAILED | rc=2 >>
[Errno 2] No such file or directory
Any ideas?
UPDATE
In play_local.yaml, I have the following task:
- name: Prepare system
hosts: default
become: yes
gather_facts: false
pre_tasks:
- raw: sudo apt-get -y install python python-setuptools python-pip build-essential libssl-dev libffi-dev python-dev easyinstall pip
- file: path=/etc/sudoers.d/ssh-auth-sock state=touch mode=0440
#- lineinfile: line='Defaults env_keep += "SSH_AUTH_SOCK"' path=/etc/sudoers.d/ssh-auth-sock
- replace:
path: /etc/apt/sources.list
regexp: 'br.'
replace: ''
Shouldn't this task install pip?
Seems like pip is not installed, you can use the following task to install it:
- name: Install pip
apt:
name: python-pip
update_cache: yes
state: present
May be pip is hashed. Meaning pip is installed at path x (may be /usr/local/bin/pip), however, cached at path y (may be /usr/bin/pip). You can confirm that from - ansible default -m shell -a ‘type pip’. To resolve this you’ll need to run - ansible default -m shell -a ‘hash -r’.
BTW, you can also use command module instead of shell.
I've just met the same problem on a brand new CentOS 7. Solved through installing setuptools with yum first and then pip with easy_install as below :
ansible default -b -m yum -a "name=python-setuptools state=present"
ansible default -b -m easy_install -a "name=pip state=present"
For Debian Based systems (RUN THIS ON CLIENT SYSTEMS):
first install package python-is-python3 then add alias for pip echo alias pip=pip3 >> ~/.bashrc
I know my solution would be dumb but It works.
# pip-fix.yml
- name: pip fix
hosts: all
become: true
tasks:
- name: install python-is-python3
apt: name=python-is-python3 update_cache=yes state=present
- name: creating alias
shell: echo alias pip=pip3 >> ~/.bashrc
- name: test and upgrade pip
pip: name=pip state=latest
tags:
- packages
run using ansible-playbook pip-fix.yml
Ansible Not finding / Installing Pip
While not using the author's original details, i think this might help them or others.
I was getting the following error for installing pip:
via Ansible:
FAILED! => {"changed": false, "cmd": "/bin/easy_install --upgrade pip", "failed": true, "msg": "Couldn't find index page for 'pip' (maybe misspelled?)
directly on the host:
Searching for pip
Reading https://pypi.python.org/simple/pip/
Couldn't find index page for 'pip' (maybe misspelled?)
Scanning index of all packages (this may take a while)
Reading https://pypi.python.org/simple/
No local packages or download links found for pip
error: Could not find suitable distribution for
Requirement.parse('pip')
I attempted to use the above hash -r answer:
(NOTE: help hash... hash -r == forget all remembered locations)
- name: forget easy_install path
shell: hash -r
become: true
however this was not a solve for me.
I found via another post: 'pip install' fails for every package ("Could not find a version that satisfies the requirement") that this was the final fix:
curl https://bootstrap.pypa.io/get-pip.py | python
or
- name: manually install pip
shell: curl https://bootstrap.pypa.io/pip/2.7/get-pip.py | python
become: true
NOTE: i changed the pip version. Also, there is a better way to do a curl in ansible, this is simply an example
I then followed with the easy_install pip latest. to ensure it was up to date.
below fix worked for me
#ln -s /usr/local/bin/pip /usr/bin/pip

Ansible: Unable to run docker compose in an ansible playbook

I appear to be unable to run docker compose tasks in an ansible playbook. I get stuck in a loop.
The first error I get when running sudo ansible-playbook playbook.yml is the following
fatal: [10.0.3.5]: FAILED! => {"changed": false, "msg": "Unable to load docker-compose. Try `pip install docker-compose`. Error: No module named compose"}
so I remote to that machine and did sudo pip install docker-compose and try running the playbook again. This time I get...
fatal: [10.0.3.5]: FAILED! => {"changed": false, "msg": "Cannot have both the docker-py and docker python modules installed together as they use the same namespace and cause a corrupt installation. Please uninstall both packages, and re-install only the docker-py or docker python module"}
so I try uninstalling docker python...
sudo uninstall docker python
Then I get the following when attempting to run the playbook again
fatal: [10.0.3.5]: FAILED! => {"changed": false, "msg": "Failed to import docker-py - No module named docker. Try `pip install docker-py`"}
However this is already install on the machine, as when I run sudo pip install docker-py I see the following...
Requirement already satisfied (use --upgrade to upgrade): docker-py in /usr/local/lib/python2.7/dist-packages
Cleaning up...
Does anyone know how to escape this loop and successfully get an ansible playbook that uses docker-compose to run?
The machine os is linux 14.04
Thanks,
What worked for me was to first uninstall everything docker related in the virtualenv for Ansible.
pip uninstall docker docker-py docker-compose
And then install the docker-compose module, which will install the docker module as well as a dependency.
pip install docker-compose
The Ansible docker module will try to import docker, which will also succeed with the docker module, and as such not provide an error with the misleading instruction to install docker-py.
I had the just had the same error message while trying to run sudo ansible playbook-with-docker.yaml
{"changed": false, "msg": "Unable to load docker-compose. Try `pip install docker-compose`. Error: No module named compose"}
I took me about 2 hours to figure out that in Linux pip install is not the same as sudo pip install (quite obvious, once you know what's happening ).
So in case someone has the same issue - make sure you're you are running everything consistently either as sudo or not sudo, but don't mix stuff :)
...and use sudo pip list | grep docker to verify.
As already stated in other answers, docker-compose python module is missing.
You can install it manually as previous answers indicate or you can use a "pure" Ansible solution that is to install via a task.
For that, use ansible.builtin.pip to install docker-compose module (you can add more modules to install if needed in the same task).
- hosts: all
gather_facts: no
tasks:
- name: Install docker-compose python package
ansible.builtin.pip:
name: docker-compose
Reference: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/pip_module.html
To add more context to Thermostat's answer
I was using pip3 and not pip with the following:
Ubuntu 20.04
Ansible 2.10.7
Python 3.8.10
pip 20.0.2 (pip3)
Here's how I fixed mine:
So first I ran the command to remove all existing copies of docker, docker-py and docker-compose python libraries:
pip3 uninstall docker docker-py docker-compose
And then ran the command below to install the python docker-compose library alongside the python docker library
pip3 install docker-compose
That's all.

ERROR: composer is not a legal parameter in an Ansible task or handler

Hi I've been trying to make ansible run composer install to install all the content in my composer.json inside my laravel file. But I'm getting this error ERROR: composer is not a legal parameter in an Ansible task or handler I'm not sure what's causing this. Below are the content of my playbook.
---
- name: Install PHP5+
apt: name={{ item }} update_cache=yes state=latest
with_items:
- git
- mcrypt
- php5-cli
- php5-curl
- php5-fpm
- php5-intl
- php5-json
- php5-mcrypt
- php5-sqlite
- sqlite3
notify:
- Reload Nginx
- name: install composer
shell: curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
args:
creates: /usr/local/bin/composer
- composer:
command: install
working_dir: /path/to/project
Now if I do vagrant provision I'm getting the Composer is not a legal parameter. Basically I just wanted to run composer and tell composer to install all dependency inside my composer json
http://docs.ansible.com/ansible/composer_module.html
I will usually just use a role from Ansible Galaxy, such as geerlingguy.composer for such requirements.
According to the Ansible Docs, the composer module is available from Ansible 1.6, but requires composer to be pre-installed on the executable path.
Installing Composer is usually going to be a multi-step process, if you use the installer (from the geerlingguy.composer tasks/main.yml).
Check if Composer is installed.
Download Composer installer (with Ansible get_url).
Run Composer installer (via PHP executable).
Move Composer into globally-accessible location.
Update Composer to latest version (if configured).
Ensure composer directory exists.
Add GitHub OAuth token for Composer (if configured).
Or, you can download the latest version, https://getcomposer.org/composer.phar or a tagged version make it executable and move it into an appropriate directory.

Ansible "Permission denied" when trying to install/check a gem

I recently decided to switch my Ansible deployment to install Ruby via rbenv rather than from apt-get via ruby1.9.1. Now I'm getting an error when trying to install the gem via Ansible.
TASK: [nginx | s3cp gem] ******************************************************
failed: [staging.myapp.com] => {"cmd": ["/usr/local/bin", "query", "-n", "^s3cp$"], "failed": true, "item": "", "rc": 13}
msg: [Errno 13] Permission denied
FATAL: all hosts have already failed -- aborting
Ansible playbook entry for this command:
- name: s3cp gem
gem: name=s3cp state=present executable=/usr/local/bin
I have sudo set to "yes" in a higher-level call to this playbook part. So I am not sure why it's tripping up. I also am able to login with the same user used for Ansible and navigate to that directory and also install this gem.
It was working fine when I was using apt-get to install ruby1.9.1. Any ideas?
This is deployed to an Ubuntu 13.04 server, by the way.
MORE INFORMATION:
Apparently it's not just tripping up on s3cp. I skipped that one and went on to another command to install bundler. This command also would not work (failed in the same way). I am wondering if there's a default ruby that's conflicting with the rbenv ruby (though, which ruby when ssh'ed in is yielding the expected rbenv directory).
MORE-MORE INFO:
I tried to install ruby via rvm instead. I had the same error. :(
What happens when you run ansible with -vvvv? It should provide full verbose output of the tasks, hopefully including any errors that it encounters. With a bit of luck it will show you what the problem is.
Another thing to check is what user you're running the tasks as. How do you have the following parameters set at the top of your play (or do you not specify any of these)?
- hosts: myhosts
user: someuser
sudo: True
sudo_user: another_user
As far as I know, the gem ansible module is not rbenv-aware. This means that when you call the gem module, it will try to install a gem system wide. This, of course, will fail if you're not acting as root on your node.
To install a gem with rbenv, you must use rbenv's gem shim. The only way to do this is to be able to trigger rbenv init by sending the command thru bash :
- name: Install Bundler
command: bash -lc "gem install bundler"
This has been already adressed here :
Install Bundler gem using Ansible

Resources