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
Related
I'm looking for a way to install a given version of node via ansible and nvm, the installation of nvm is working as expected because if I connect with the root user, I can execute the command nvm install 8.11.3 but this same command doesn't work with Ansible, I don't understand why.
---
- name: Install nvm
git: repo=https://github.com/creationix/nvm.git dest=~/.nvm version=v0.33.11
tags: nvm
- name: Source nvm in ~/.{{ item }}
lineinfile: >
dest=~/.{{ item }}
line="source ~/.nvm/nvm.sh"
create=yes
tags: nvm
with_items:
- bashrc
- profile
- name: Install node and set version
become: yes
become_user: root
shell: nvm install 8.11.3
...
error log
TASK [node : Install node and set version] *************************************************************************************
fatal: [51.15.128.164]: FAILED! => {"changed": true, "cmd": "nvm install 8.11.3", "delta": "0:00:00.005883", "end": "2018-12-03 15:05:10.394433", "msg": "non-zero return code", "rc": 127, "start": "2018-12-03 15:05:10.388550", "stderr": "/bin/sh: 1: nvm: not found", "stderr_lines": ["/bin/sh: 1: nvm: not found"], "stdout": "", "stdout_lines": []}
to retry, use: --limit .../.../ansible/stater-debian/playbook.retry
It's okay, here's the configuration that works
- name: Install node and set version
become: yes
become_user: root
shell: "source /root/.nvm/nvm.sh && nvm install 8.11.3"
args:
executable: /bin/bash
I think the clue in the output you need is:
"/bin/sh: 1: nvm: not found"
To run a command without including the full path to that command (i.e. nvm rather than /the/dir/nvm/is/installed/in/nvm), then the directory that contains the command, must be in the $PATH environment variable for the shell that runs the command.
In this case it looks like that is not present for the shell that Ansible spawns, versus the shell your interactive commands run in. Change:
- name: Install node and set version
become: yes
become_user: root
shell: nvm install 8.11.3
to
- name: Install node and set version
become: yes
become_user: root
shell: /full/path/to/nvm install 8.11.3
If you don't know what to put in place of '/full/path/to', try either:
which nvm
or
find / -name nvm
I will just post under here, because there are hundreds of these posts.
- name: Install node
become: true
become_user: root
shell: "source /root/.nvm/nvm.sh && nvm install {{ personal_node_version }} && nvm alias default {{ personal_node_version }}"
args:
executable: /bin/bash
worked for me.
This worked for me on Ubuntu 20.04 using nvm version 0.39.1:
- name: Install NVM
shell: >
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
args:
creates: "/root/.nvm/nvm.sh"
- name: Install Node Versions
shell: ". /root/.bashrc && nvm install {{item}}"
with_items:
- 'v10.24.1'
- 'v16.17.0'
- '--lts'
- 'node'
Based on all the posts found on stack and tweaked a little for my own needs - I found this solution worked perfectly for both installing NVM (the easy part) and creating a loop that allows you to insert 1 or many versions of Node as needed
# test if nvm has been installed by the user desired
- stat:
path: /home/yournonrootuser/.nvm
register: nvm_path
- name: Setup NodeVersionManager and install node version
become: yes
# Execute config files such as .profile (Ansible uses non-interactive login shells)
become_flags: -i
become_user: yournonrootuser
block:
- name: Install nvm
shell: >
curl -o- https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
args:
executable: /bin/bash
chdir: "$HOME"
creates: "$HOME/.nvm/nvm.sh"
- name: Setup .profile of yournonrootuser
lineinfile:
path: ~/.profile
# This will make sure Node is on the users PATH
line: source ~/.nvm/nvm.sh
create: yes
become_flags: -i
when: nvm_path.stat.exists == false
# if we got here we already know node version manager is installed
- name: installing node versions using loop
command: sudo -iu yournonrootuser nvm install {{item}}
args:
executable: /bin/bash
chdir: "$HOME"
creates: "$HOME/.nvm/versions/node/v{{item}}"
loop:
- 14.18.3
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
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
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 :)
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