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'
Related
I want to run similar commands on Linux mint and Ubuntu but they have small differences.
I found a solution but it makes me rewrite each task twice. Is there a better way to do that?
- name: install the latest version of Ansible
block:
- name: Add Ansible PPA (Ubuntu)
ansible.builtin.apt_repository:
repo: ppa:ansible/ansible
become: true
when: ansible_facts['distribution'] == 'Ubuntu'
- name: Add Ansible PPA (Linux Mint)
ansible.builtin.apt_repository:
repo: ppa:ansible/ansible
codename: focal
become: true
when: ansible_facts['distribution'] == 'Linux Mint' and ansible_distribution_release == 'una'
- name: install Ansible and dependency
apt:
name:
- software-properties-common
- ansible
state: latest
update_cache: yes
become: true
One way to do this is to use the omit filter. See the documentation. This can be used to make the codename parameter optional when running the apt_repository module.
Example:
- name: set distro codename when OS is Linux Mint
ansible.builtin.set_fact:
distro_codename: focal
when: ansible_facts['distribution'] == 'Linux Mint' and ansible_distribution_release == 'una'
- name: Add Ansible PPA
ansible.builtin.apt_repository:
repo: "ppa:ansible/ansible"
codename: "{{ distro_codename | default(omit) }}"
This will ensure that the codename is used while creating APT repository if it is set (for Linux Mint in this case). Otherwise it will be omitted.
you could do something like this:
- name: install the latest version of Ansible
block:
- name: Add Ansible PPA (Linux Mint or Ubuntu)
ansible.builtin.apt_repository:
repo: ppa:ansible/ansible
codename: "{{ '' if ansible_facts['distribution'] == 'Ubuntu' else 'focal' }}"
become: true
- name: install Ansible and dependency
apt:
name:
- software-properties-common
- ansible
state: latest
update_cache: yes
become: true
when codename is empty, it takes automatically the name of distribution
Ansible version: 2.9
Python version being used: 3.6+
OS: ubuntu 18
Trying to remove docker-py using pip.
Code snippets I have tried...
post_tasks:
- pip:
name: docker-py
state: absent
- name: remove docker-py sdk
pip:
name: docker-py
state: absent
Document referred: https://docs.ansible.com/ansible/2.9/modules/docker_container_module.html
package never gets uninstalled.
Direct pip command from shell works
pip uninstall docker-py
- name: remove docker-py sdk
become: no
pip:
name: docker-py
state: absent
I had become: yes for the whole ansible-playbook, i did a override for this task, so may be we should avoid become: yes for pip packages.
I am new to ansible and writing a playbook to install locustio with python3 on a Ubuntu 18.04. I don't know how to install locustio with pip3 in playbook. If use pip package, then it gives error to use older version of locust. ( use a pinned old locust version (pip/pip3 install locustio==0.13.5) . I would like to know how to install locust with pip3 or locust 0.13.5 version with pip?
- hosts: all
tasks:
- name: Create folder to keep the files
file:
path: /opt/locust
state: directory
mode: '0755'
become: yes
- name: Python installation
apt:
name: ['python3']
state: present
become: yes
- name: Pip installation
apt:
name: ['python3-pip']
state: present
become: yes
- name: pip install
apt:
name: ['python-pip']
state: present
become: yes
- name: Locust Installation
pip:
name: ['locustio']
state: present
become: yes
I haven't used Ansible, but looking at the docs there seems to be a executable parameter that you could set to pip3.
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.
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