tipyn
tipyn

Reputation: 49

Ansible playbooks

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

Upvotes: 2

Views: 5883

Answers (4)

Jonathan
Jonathan

Reputation: 7128

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

Upvotes: 0

Asier Gomez
Asier Gomez

Reputation: 6588

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

Upvotes: 0

sanshi_leilei
sanshi_leilei

Reputation: 89

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.

Upvotes: 0

code_ada
code_ada

Reputation: 884

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

Upvotes: 14

Related Questions