4

I am writing an ansible playbook, and I would first like to install pew, and transition into that pew environment in order to install some other python libraries.

So, my playbook is going to look something like this...

tasks:

# task 1
- name: install pew if necessary
  command: pip install pew

# task 2
- name: create new pew environment if necessary
  command: pew new devpi-server

# task 3
- name: transition to pew environment
  command: pew workon devpi-server

# task 4
- name: install devpi-server
  command: pip install devpi-server

# task 5
- name: run devpi server
  command: devpi-server ## various args

In the spirit of keeping my playbook idempotent, I would like to do all of these tasks only if necesary.

So, I would only want to install pew if it isn't already installed, only want to create a new pew environment if it doesn't already exist, only workon the pew environment if we aren't already... etc etc...

Does anyone have good advice as to how to accomplish this? I am somewhat familiar with ansible conditionals, but only when it is something like, "is a file downloaded or not"

I haven't had to determine yet if programs are installed, virtual environments are loaded, etc..

Zack
  • 13,454
  • 24
  • 75
  • 113

2 Answers2

5

You can use the Pip module in Ansible to ensure that certain packages are installed.

For your conditionals I would refer to: How to make Ansible execute a shell script if a package is not installed and http://docs.ansible.com/ansible/playbooks_conditionals.html#register-variables - these should get you on the right track.

So your playbook would look a little like:

- pip: name=pew

- name: Check if pew env exists
  command: pew command
  register: pew_env_check

- name: Execute script if pew environment doesn't exist
  command: somescript
  when: pew_env_check.stdout.find('no packages found') != -1
Kieran
  • 286
  • 5
  • 12
  • I noticed the pip module as well. What would be your advice if I don't have pip installed on the remote machine. Curl get-pip.py and then run it? That's my current strategy. – Zack Sep 22 '16 at 20:09
  • If you have `easy_install` you could install it via that: http://docs.ansible.com/ansible/easy_install_module.html – Kieran Sep 22 '16 at 20:11
  • I don't have that either, and [this post](http://stackoverflow.com/questions/26501086/getting-msg-failed-to-find-required-executable-easy-install-when-trying-to-br) seems to indicate I shouldn't use it. – Zack Sep 22 '16 at 20:12
  • Ah - fair enough. Then I would go with your approach and add a check to see if pip is installed, if not, run your conditional and install it via curl. – Kieran Sep 22 '16 at 20:13
  • Sweet. I think this is enough to get me started. I'll circle back and give you the accept once I've confirmed it all works. Thanks! – Zack Sep 22 '16 at 20:16
  • Still running into a few issues getting the entire script to run, but this is a solid enough of an answer to help others. – Zack Sep 23 '16 at 19:34
0

For Ansible tasks - Here are some examples:

- name: Install bottle python package
  ansible.builtin.pip:
    name: bottle

- name: Install bottle python package on version 0.11
  ansible.builtin.pip:
    name: bottle==0.11

- name: Install bottle python package with version specifiers
  ansible.builtin.pip:
    name: bottle>0.10,<0.20,!=0.11

- name: Install multi python packages with version specifiers
  ansible.builtin.pip:
    name:
      - django>1.11.0,<1.12.0
      - bottle>0.10,<0.20,!=0.11

Reference: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/pip_module.html

Yakir GIladi Edry
  • 2,511
  • 2
  • 17
  • 16