Ansible playbooks - linux

I haven't used Ansible before, does anyone know how to write a simple playbook that uninstalls nano and installs vim on a Linux server? I would imagine you would need to include an option to configure which text editor preference you want after doing the above.
Cheers
edit
This is what I've got so far...
---
# Playbook to uninstall nano and install vim
- hosts: all
remote_user: luca
sudo: yes
tasks:
- name: uninstall nano
action: command: sudo apt-get purge nano
- name: Install vim
action: command: sudo apt-get install vim

tmp.yml
---
- hosts: all
tasks:
- name: nano remove
apt: name=nano state=absent
- name: vim install
apt: name=vim state=present
ansible-playbook tmp.yml
http://docs.ansible.com/apt_module.html

If you are working on fedora/centos/rhel:
---
- hosts: all
tasks:
- name: nano remove
yum: name=nano state=absent
- name: vim install
yum: name=vim state=latest
Refer to doc of yum module . Set proper args for "state": install (present or installed, latest), or remove (absent or removed) a package.

If you want to remove and install with command, you must write without 'action', like this:
tasks:
- name: uninstall nano
command: sudo apt-get purge nano
- name: Install vim
command: sudo apt-get install vim
But it is not recommended, it is better doing with 'absent'.
And I am not sure if can put sudo, so you can connect directly with your root user.
- hosts: all
remote_user: root
sudo: True

I personally find it cleaner with a loop for the installs. For the uninstall just change the "state"
hosts: desktop-linux
tasks:
- name: Install Desktop packages
apt: name={{item}} state=installed
with_items:
- meld
- synergy
- retext
- pidgin
- steam
- ubuntu-restricted-extras
- nautilus-admin
- unity-tweak-tool
- vlc

Related

How to install MongoDB with Ansible?

I'm a beginner with Ansible and I'm now trying to install MongoDB on an Ubuntu 14.04 host. According to the MongoDB installation instructions the manual process is as follows:
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
sudo apt-get update
sudo apt-get install -y mongodb-org
I guess I can run this using the Ansible shell module, but since line 2 of the 4 lines about would constantly add new lines to the mongodb-org-3.6.list file I guess that is not the correct way.
Does anybody know what the logical way of doing this with Ansible is? All tips are welcome!
In that case you don't even have to mess with lineinfile. Use the apt_repository module (documentation) ; in this example we'll install MongoDB 3.4:
[...]
tasks:
- name: Add Mongo packages repo
apt_key: id=2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5 keyserver=keyserver.ubuntu.com
- name: add repo itself
apt_repository: repo='deb http://repo.mongodb.org/apt/ubuntu {{ansible_distribution_release}}/mongodb-org/3.4 multiverse' state=present
- name: install packages
apt: pkg=mongodb-org state=present
[...]
create your mongo-ansible.yml file and use this:
# Install mongodb
---
- name: Add mongo ppa key
sudo: yes
apt_key: >
keyserver=hkp://keyserver.ubuntu.com:80
id=7F0CEB10
state=present
- name: Add mongo sources list
sudo: yes
lineinfile: >
line="deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse"
dest=/etc/apt/sources.list.d/mongodb.list
state=present
create=yes
- name: Install mongo
sudo: yes
apt: name=mongodb-org state=latest update_cache=yes
In ansible 2.0 sudo deprecated and block added. So for modern syntax and latest mongodb:
---
# https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/
- block:
- name: Import MongoDB public GPG Key
apt_key:
keyserver: keyserver.ubuntu.com
id: 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
- name: Add MongoDB repository into sources list
apt_repository:
repo: deb http://repo.mongodb.org/apt/ubuntu {{ansible_distribution_release}}/mongodb-org/3.4 multiverse
state: present
- name: Install MongoDB package
apt:
name: mongodb-org
update_cache: yes
become: yes
There is no need to do this by using shell commands. apt_key module can add keys, apt module can be used to install and lineinfile module can ensure a particular line in file.
- name: get apt key
apt_key: keyserver=hkp://keyserver.ubuntu.com:80 id=EA312927
- name: add to mongodb-org-3.2.list
lineinfile: dest=/etc/apt/sources.list.d/mongodb-org-3.2.list line="deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse"
- name: install mongo db
apt: name=mongodb-org update_cache=yes
If you need to create mongodb-org-3.2.list first then you can use file module for that.
- name: create mongodb-org-3.2.list
file: path=/etc/apt/sources.list.d/mongodb-org-3.2.list state=present

Using easy_install3 with ansible

It seems easy_install3 is not currently available as an ansible module. So instead of doing this:
---
- name: Install apt dependencies
apt: name={{item}} state=installed
with_items:
- python3-setuptools
- name: install pip3
easy_install3: name=pip
I'm using this:
---
- name: Install apt dependencies
apt: name={{item}} state=installed
with_items:
- python3-setuptools
- name: install pip3
shell: easy_install3 pip
Is there a better alternative?
As mentioned in the comments, you can just use apt to install python3-pip rather than going via easy_install like the dark old days:
---
- name: Install apt dependencies
apt: name=python3-pip state=installed
If you did need to force a specific executable for easy_install then you could use the executable option:
- name: install pip3
easy_install: name=pip
executable: easy_install-3.3
However, it might also help to know that pip has been bundled with Python since 3.4 so you might not need to do anything at all.
I think executable option can be used to mention the version of easy_install. However(as suggested in the comments), it is recommended to use the pip module which you can first install using easy_install.
See: http://docs.ansible.com/ansible/easy_install_module.html for more information.

ansible install node.js version 6

For installing the node 6.x version these are the commands:
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get install -y nodejs
now how exactly do I do that in ansible?
any ideas here?
this is what I had till now, but it installs the old version
---
- name: Ensure Ubuntu Distro is Supported
get_url:
url='https://deb.nodesource.com/node/dists/"{{ ansible_distribution_release }}"/Release'
dest=/dev/null
register: distrosupported
- name: Remove Old Chris Lea PPA
apt_repository:
repo='ppa:chris-lea/node.js'
state=absent
when: distrosupported|success
ignore_errors: yes
- name: Remove Old Chris Lea Sources
sudo: yes
file:
path='/etc/apt/sources.list.d/chris-lea-node_js-"{{ ansible_distribution_release }}".list'
state=absent
when: distrosupported|success
ignore_errors: yes
- name: Add Nodesource Keys
sudo: yes
apt_key:
url=https://deb.nodesource.com/gpgkey/nodesource.gpg.key
state=present
- name: Add Nodesource Apt Sources List Deb
sudo: yes
apt_repository:
repo='deb https://deb.nodesource.com/node "{{ ansible_distribution_release }}" main'
state=present
when: distrosupported|success
- name: Add Nodesource Apt Sources List Deb Src
sudo: yes
apt_repository:
repo='deb-src https://deb.nodesource.com/node "{{ ansible_distribution_release }}" main'
state=present
when: distrosupported|success
- name: Install NodeJS
sudo: yes
apt: pkg=nodejs state=latest update_cache=true
when: distrosupported|success
- debug: msg="{{npm_pkgs}}"
- name: install global npm packages
sudo: yes
npm: name={{item}} global=yes state=latest
with_items: "{{npm_pkgs}}"
I was using this playbook for installing node 6.1.0 via nvm(node version manager):
Note: You may need to change the hosts and connection in the play.
---
- hosts: localhost
connection: local
vars:
node_version: 6.1.0
tasks:
- name: Download the nvm(node version manager) install script
get_url: url=https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh dest=/tmp/install.sh
- name: Install dependencies
apt: pkg={{ item }} update_cache=yes cache_valid_time=3600
with_items:
- git
- curl
- build-essential
- libssl-dev
become: yes
become_user: root
- name: Execute the nvm install script
shell: bash install.sh chdir=/tmp executable=/bin/bash
- name: Register the NVM_DIR
shell: echo $NVM_DIR
register: nvm_dir
- name: Install the specified node version using the nvm command and set it as default
shell: . {{ nvm_dir.stdout }}/nvm.sh && nvm install {{ node_version }} && nvm run {{node_version}} --version && nvm alias default {{node_version}}
creates=~/.nvm/versions/node/v{{ node_version }}
For more information on nvm, see: https://github.com/creationix/nvm
Based on the code in the original question, and the helpful comment from #ydaetskcoR, I was able to install NodeJS 6.x with the following on Ubuntu 16.04:
- name: Add Nodesource Keys
become: yes
apt_key:
url: https://deb.nodesource.com/gpgkey/nodesource.gpg.key
state: present
# Note: "xenial" is ubuntu-speak for 16.04
- name: Add Nodesource Apt Sources
become: yes
apt_repository:
repo: '{{ item }}'
state: present
with_items:
- 'deb https://deb.nodesource.com/node_6.x xenial main'
- 'deb-src https://deb.nodesource.com/node_6.x xenial main'
- name: Install NodeJS and NPM
become: yes
apt:
name: '{{ item }}'
state: latest
update_cache: yes
with_items:
- nodejs
- nodejs-legacy
- npm
I've refactored a little for brevity, but the most important thing is the addition of _6.x to the repository URLs.
This worked for me using Ansible 2.3.2.0

pure-uploadscript of pure-ftpd is not working on ubuntu

I am using the apt-get install the pure-ftp on ubuntu server 14.04.4
sudo apt-get install pure-ftpd
sudo pure-uploadscript -B -r /home/john/hello.sh
the hell.sh file, and it's able to run.
#!/bin/sh
echo "hello"
Then, I use FileZilla to upload the file. I can upload the file, but the script is not called. please help;
official doc
If you install the pure-ftpd server by apt-get, it may not give you the feature that you want to use. I checked the /var/run folder, some file are missing there. I complied the code with --with-uploadscript, it's working now.
I had to also compile from source, fortunately the install is not too heavy. It may be worth uploading the compiled files from your system to your mirror and just downloading and running make install. On the other hand, this works as well:
- name: install pure-ftpd from source
block:
- name: create required pure-ftpd dirs
become: yes
file:
path: /etc/pure-ftpd
state: directory
owner: root
mode: 0755
- name: install deps for building pureftpd
apt: pkg={{ item }} state=present
with_items:
- libssl-dev
- libpam0g-dev
- name: download and unpack pure-ftpd source
unarchive:
src: http://download.pureftpd.org/pub/pure-ftpd/releases/pure-ftpd-1.0.49.tar.gz
dest: /usr/local/src/
remote_src: yes
keep_newer: yes
register: source_unpack
- name: configuring pure-ftpd source with custom modules
command: "./configure --prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin --libexecdir=/usr/libexec
--datadir=/usr/share --sysconfdir=/etc --sharedstatedir=/usr/com --localstatedir=/var --libdir=/usr/lib64
--includedir=/usr/include --infodir=/usr/share/info --mandir=/usr/share/man --with-virtualchroot --with-everything
--with-uploadscript --with-tls --with-pam"
args:
chdir: /usr/local/src/pure-ftpd-1.0.49
when: source_unpack|changed
register: pure_ftpd_configure
- name: make and install pure-ftpd
become: yes
shell: make && make install
args:
chdir: /usr/local/src/pure-ftpd-1.0.49
when: pure_ftpd_configure|changed
when: stat_result.stat.exists == False
tags:
- ftp

How to Install python3.4.3 with Ansible

I want to install python3.x by use pyenv with ansible.
- name: install pyenv
git: >
repo=https://github.com/pyenv/pyenv.git
dest=/home/www/.pyenv
accept_hostkey=yes
become: yes
become_user: www
- name: enable pyenv
shell: |
echo 'export PYENV_ROOT="/home/www/.pyenv"' >> /home/www/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> /home/www/.bashrc
echo 'eval "$(pyenv init -)"' >> /home/www/.bashrc
- name: install python
shell: pyenv install 3.4.3
How to install python3.x with ansible?
So here is what worked for me well to get any version of python installed with ansible and make it an alternative installation. I first ran configure and make, later compressed the result since this takes a while, then re-distributed the file using a mirror so I can run make altinstall on its own. Here is the recipe:
---
# Check the alt python3 version
- name: check alt python version
shell: /usr/local/bin/python3.6 --version
register: python3_version
ignore_errors: yes # If not installed
tags:
- python-alt
# Stuff I did manually to compile everything first by hand
# Python3 alt-install - steps to create binary:
# wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz
# tar xf Python-3.6.4.tgz
# mv Python-3.6.4 Python-3.6.4-binary && cd Python-3.6.4-binary
# ./configure --prefix=/usr/local --enable-optimizations
# cd .. && tar -zcvf Python-3.6.4-binary.tar.gz Python-3.6.4-binary (upload to mirror servers)
# make && sudo make altinstall UNINST=1
- name: download and unpack alternative python3
unarchive:
src: http://www.yourmirror.com/centos/python/Python-3.6.4-binary.tar.gz dest=/tmp/Python-3.6.4-binary.tar.gz
dest: /tmp
remote_src: yes
keep_newer: yes
when: python3_version['stderr'] != 'Python 3.6.4'
tags:
- python-alt
# Its possible to install (instead of altinstall) python3 here
- name: make install alt python3
make:
chdir: /tmp/Python-3.6.4-binary
target: altinstall
params:
UNINST: 1 # Replace
when: python3_version['stderr'] != 'Python 3.6.4'
become: yes
tags:
- python-alt
- name: download get-pip.py
get_url:
url: https://bootstrap.pypa.io/get-pip.py
dest: /tmp/get-pip.py
mode: 0664
tags:
- python-alt
- name: install pip for python3
shell: /usr/local/bin/python3.6 /tmp/get-pip.py
become: yes
tags:
- python-alt
# We need virtualenv installed under py3 for the virtualenv command to work
- pip:
name: virtualenv
executable: /usr/local/bin/pip3.6
become: True
tags:
- python-alt
If you want to compile everything on your server you could do the following before the altinstall step and also download the source code package instead of the pre-compiled tar. I don't recommend doing it this way because it does take up resources and you don't want to be doing it in prod. Using Python2.7.14 as an example:
---
# Build the default target
- debug:
var: python2_version
tags:
- python_alt
- make:
chdir: /tmp/Python-2.7.14-binary
when: python2_version['stderr'] != 'Python 2.7.14'
tags:
- python_alt
- name: configure target command
command: ./configure --prefix=/usr/local --enable-optimizations chdir=/tmp/Python-2.7.14-binary
when: python2_version['stderr'] != alt_python_version
tags:
- python_alt
Rather than using the shell module to set environment variables on the remote host, Ansible has the environment keyword, which can set per task or even per playbook.
Assuming the www user already exists I managed to get this working with some more specific path setting:
- name: enable pyenv and install python
shell: /home/www/.pyenv/bin/pyenv init - && /home/www/.pyenv/bin/pyenv install 3.4.3 chdir=/home/www
environment:
pyenv_root: /home/www/.pyenv
path: "{{ pyenv_root }}/bin:$PATH"
become: yes
become_user: www
You will need to run the playbook with:
ansible-playbook --ask-become-pass <playbook-name>
and supply the password for the www user on request.
If that doesn't work, you might have to post the whole playbook here for us to look at :)

Resources