2
votes

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
4
Most Linux distributions include both nano and Vim. The two text editors have been living happily together for quite a long while so what problem exactly are you trying to fix?romainl
@romainl it's just a task I've been set as part of a test for a job application, I'm kinda struggling to understand Ansible and how it would translate something like 'sudo apt-get purge nano' for example.tipyn
Besides that you are not using the apt module, as it is suggested in the answer below, in your example tasks indentation isn't correct. tasks should be indented the same as hosts.Strahinja Kustudic

4 Answers

14
votes

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

0
votes

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.

0
votes

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
0
votes

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