Unable to run Ansible 'expect' module - ansible

I am trying to run an Ansible script that invokes the 'expect' module (see end of the message).
When I run it, I get error:
The pexpect python module is required
Yet, the pip task for installing pexpect runs without error.
What am I doing wrong?
Thx.
Alain Désilets
=== Playbook content ===
---
- name: Run Anaconda shell
hosts: all
vars:
conda_home: "~/anaconda2-NEW"
conda_inst_sh_path: /path/to/Anaconda2-2018.12-MacOSX-x86_64.sh
tasks:
- name: install pexpect
pip:
name: pexpect
become: yes
become_user: root
- name: Run anaconda installation script
expect:
command: "sh {{conda_inst_sh_path}}"
responses:
(?i)password: "MySekretPa$$word"
become: yes
become_user: root

As the Ansible documentation of the expect module states:
The below requirements are needed on the host that executes this module.
python >= 2.6
pexpect >= 3.3
You need to install pexpect in a minimum version of 3.3 besides python >= 2.6 on the target system, that means that you have to install pexpect and python on every system, that is defined under hosts: all.

Related

Ansible ansible.builtin.expect | requirement: pexpect

Currently, I am trying to use the ansible.builtin.expect.
Here is my usecase:
- name: Set password for built-in user
expect: command: '/usr/share/elasticsearch/bin/elasticsearch-keystore add "bootstrap.password" -f'
responses: Enter value for bootstrap.password: 'test'
To use ansible.builtin.expect I have to install:
python >= 2.6
pexpect >= 3.3
I have Python 2.7.5 installed, but if i want to install pexpect, it only installs version 2.3.
To install pexpect i use:
- name: Install pexpect module
yum:
name: pexpect
state: latest
Does anyone know, how I am able to install pexpect version 3.3?
Why not installing directly the pip version matching your requirement:
- name: Install pexpect throuhg pip
become: true
pip:
name: "pexpect>=3.3"
state: present
something like this maybe:
- name: 'install pexpect'
become: true
become_user: 'root'
yum:
name: '{{ item }}'
state: present
enablerepo: 'standard'
with_items:
- 'pexpect'

How ansible find python modules on target and is it possible to use virtualenv to install modules dependencies?

I need to create some snake-oil certificates on some playbook and I'm using openssl_privatekey, openssl_csr and openssl_certificate modules for this. The problem is that this module depens on PyOpenSSL and that CentOS versions of PyOpenSSL are outdated.
To workaround this I install PyOpenSSL from pip, which works, but may overwrite an already existing PyOpenSSL module if that was installed with yum.
On my playbook I'm doing this:
- name: 'Install PyOpenSSL'
pip:
name: PyOpenSSL
state: present
version: '>= 0.15'
Now, is there a way to install this in a virtual environment, so that ansible is aware of it? If so I can simply remove the virtual environment in the play's end.
You can create a virtualenv with the pip module. You can probably make Ansible aware of that virtualenv by setting the ansible_python_interpreter fact to point at the python binary in the virtualenv, so something like:
---
- hosts: localhost
gather_facts: false
tasks:
- name: install modules
pip:
state: present
name: "{{ modules }}"
virtualenv: /tmp/venv
vars:
modules:
- pyopenssl
- set_fact:
ansible_python_interpreter: /tmp/venv/bin/python
Caveat: I haven't tested this thoroughly. It didn't break anything.

ansible throwing error when using pip command

When I am trying to run pip command through ansible I land up in error.
{"changed": false, "msg": "Unable to find any of pip to use. pip needs to be installed."}
When I debugged on my machine I found pip was installed in latest version. I realized my file uses sudo to run the pip.
So if I do which pip
I get the path of pip but if I do sudo which pip I get nothing.
I don't know how to change my file so instead of sudo it take
- name: "Allow newuser for new super user without SUDO password for using rsync:"
lineinfile:
path: /etc/sudoers
state: present
insertafter: '^%sudo'
line: "{{ user }} ALL=(ALL:ALL) NOPASSWD: /usr/bin/rsync"
- pip:
name: opencv-python
state: forcereinstall
executable: pip
I have no idea how to fix this issue
At the remote host find out which pip is used by the remote user. Use the executable and become the remote user. For example,
- name: Install opencv-python
become_user: admin
become: true
pip:
name: opencv-python
state: forcereinstall
executable: /home/admin/.local/bin/pip
See Becoming an Unprivileged User. Pipelining should solve the problems of becoming an unprivileged user.
shell> grep pipe ansible.cfg
pipelining = true
Notes
Whatever you install by pip this way will be available to this particular remote user only.
Preferably distro packages should be used. For example
shell> apt-cache search python-opencv
python-opencv - Python bindings for the computer vision library
python-opencv-apps - opencv_apps Robot OS package - Python 2 bindings
See The pip module isn't always idempotent #28952 and the Conclusions in particular.

pexpect version on vagrant box and on host is both 4.6.0, but when installing over ansible, 3.1 is the newest version

I was using ansible to install pexpect on both my vagrant box and also onto my host. When I installed pexpect onto both computers, the version is 4.6.0, but when using ansible to install using apt-get, the maximum version is only 3.1. The error message thrown is: "Insufficient version of pexpect installed (3.1), this module requires pexpect>=3.3. Error was __init__() got an unexpected keyword argument 'echo'"}How am I able to install pexpect in order to use the expect module for ansible?
The code for downloading pexpect is
- hosts: all
become: yes
become_user: root
gather_facts: no
#can use sudo/sudo_user instead of become, but thats depreceated in ansible 2.6 or later
tasks:
- name: download pip
apt: name=python3-pip state=latest
- name: update pexpect
command: pip3 install pexpect
command: pip3 install --upgrade pip3
command: pip3 install --upgrade pexpect
Run a task per command and try to use Ansible pip module.
- name: Install Pexpect
hosts: all
become: True
become_user: root
gather_facts: False
tasks:
- name: download pip
apt:
name: python3-pip
state: latest
- name: Install Pexpect
pip:
name: pexpect
state: latest
- name: Upgrade pip - Force reinstall to the latest version
pip:
name: pip
state: forcereinstall

Can't create gitlab project with ansible [duplicate]

I'm just getting started with ansible, and I'm trying to get it to work with the linode module from OSX Yosemite. I'm using brew-installed Python, and pip-installed ansible. So I have done the following:
$ brew install python
$ PYCURL_SSL_LIBRARY=nss pip install pycurl
$ CFLAGS=-Qunused-arguments CPPFLAGS=-Qunused-arguments pip install ansible
To test:
$ which python
/usr/local/bin/python
$ python --version
Python 2.7.9
$ which ansible
/usr/local/bin/ansible
$ ansible --version
ansible 1.8.2
configured module search path = None
When I run my playbook I get:
failed=True msg='pycurl required for this module'
So somehow ansible is not seeing the pycurl library.
Here is the playbook:
---
- hosts: 127.0.0.1
connection: local
gather_facts: no
tasks:
- name: "provision a server"
local_action:
module: linode
api_key: FOO
name: linode-test1
plan: 1
datacenter: 1
distribution: 99
password: 'superSecureRootPassword'
ssh_pub_key: ssh-rsa FOO
swap: 768
wait: yes
wait_timeout: 600
state: present
It's happening on ubuntu as well, finally figured out the solution. You need to explicity pass path to python executable
Change Ansible hosts file from
[local]
localhost
to
[local]
localhost ansible_python_interpreter=/usr/local/bin/python
or to your virtual environment python path.
I was able to fix this on OS X El Capitan 10.11.6 by editing hosts:
[local]
localhost ansible_python_interpreter=/usr/local/bin/python
You may then need additional Python dependencies:
pip install pycurl
pip install linode-python

Resources