Score:0

Ansible playbook on ubuntu 20

cn flag

How to write Ansible Playbook to install different applications on different hosts on Ubuntu 20.04 Conditions

  1. When running a playbook it should ask for the host name (we must give the specific host name)

For all the packages we have to give numbers like 1,2,3, .....

After giving the host name it should ask for the package number, at that time we should give that number, the specific package should be installed on the specific host

Score:0
hu flag

I would organize my Ansible repo to have separate role for each software and then use ansible_hostname fact of gather_facts module as condition to install software for the specific host. I would go with a folder structure like this:

└── ansible_repo
    ├── roles
    │   ├── vim
    │   │   └── tasks
    │   │       └── main.yml
    │   ├── tmux
    │   │   └── tasks
    │   │       └── main.yml
    │   └── vlc
    │       └── tasks
    │           └── main.yml
    ├── inventory
    └── playbook.yml

The contents of the playbook.yml will go along something like this:

# playbook.yml
- hosts: all
  become: true
# run roles
- hosts: all
  roles:
    - vim
    - tmux
    - vlc

The main.yml inside each task will follow this template (there vim is used):

---
# tasks file for vim

- name: Install vim
  apt:
    name:
      - vim
    when gather_facts.ansible_hostname = "host1" or "host3"

You can store the host name in inventory file as well.

While this one would work, but it would not be the best practice, I think. A better way would be to group your hosts based on what packages the need and then use the specific roles for that group.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.