Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run a particular task on specific host in ansible

my inventory file's contents -

[webservers]
x.x.x.x ansible_ssh_user=ubuntu

[dbservers]
x.x.x.x ansible_ssh_user=ubuntu

in my tasks file which is in common role i.e. it will run on both hosts but I want to run a following task on host webservers not in dbservers which is defined in inventory file

- name: Install required packages
  apt: name={{ item }} state=present
  with_items:
    - '{{ programs }}'
  become: yes
  tags: programs

is when module helpful or there is any other way? How could I do this ?

like image 745
Prem Sompura Avatar asked Aug 10 '15 06:08

Prem Sompura


People also ask

How do I run a specific task in Ansible playbook?

The easiest way to run only one task in Ansible Playbook is using the tags statement parameter of the “ansible-playbook” command. The default behavior is to execute all the tags in your Playbook with --tags all .

Which option would target a playbook to run only on certain hosts?

Ansible command limit option Using the --limit parameter of the ansible-playbook command is the easiest option to limit the execution of the code to only one host. The advantage is that you don't need to edit the Ansible Playbook code before executing to only one host.

How can we delegate tasks in Ansible?

Ansible delegate_to property or the keyword specified in the ansible-playbook is used to provide the control to run the task locally or to the other different hosts rather than running on the remote hosts specified on the inventory server list, and this can be the few tasks or running the entire play-book locally and ...

How do you run task only once in Ansible?

For such requirements where we need one tasks to run only once on a batch of hosts and we will be running that from Ansible controller node, we have feature parameter named run_once. When we have this parameter mentioned in a task, that task will run only once on first host it finds despite the host batch.

How do I run Ansible commands against specific hosts?

When you execute Ansible through an ad hoc command or by running a playbook, you must choose which managed nodes or groups you want to execute against. Patterns let you run commands and playbooks against specific hosts and/or groups in your inventory.

How to view all tags in an Ansible playbook?

Ansible playbook will run only the task with the specified tag, it will skip the rest of the tasks in the playbook. Use the ‘–list-tags’ flag to view all the tags.

What is task1 in Ansible test server?

Task1: On the remote server named mwiapp01 defined under hostgroup named testserver this task would start a quick and simple web server using Netcat command and listen to port 8081. We have used async and poll of ansible to execute the job and run it background to know more about this refer to our previous article

What is an ansible pattern?

An Ansible pattern can refer to a single host, an IP address, an inventory group, a set of groups, or all hosts in your inventory. Patterns are highly flexible - you can exclude or require subsets of hosts, use wildcards or regular expressions, and more. Ansible executes on all inventory hosts included in the pattern.


3 Answers

Thank you, this helps me too.

hosts file:

[production]
host1.dns.name

[internal]
host2.dns.name

requirements.yml file:

- name: install the sphinx-search rpm from a remote repo on x86_64 - internal host
  when: inventory_hostname in groups['internal']
  yum:
    name: http://sphinxsearch.com/files/sphinx-2.2.11-1.rhel7.x86_64.rpm
    state: present

- name: install the sphinx-search rpm from a remote repo on i386 - Production
  when: inventory_hostname in groups['production']
  yum:
    name: http://sphinxsearch.com/files/sphinx-2.2.11-2.rhel6.i386.rpm
    state: present
like image 30
Albert Avatar answered Oct 11 '22 20:10

Albert


If you want to run your role on all hosts but only a single task limited to the webservers group, then - like you already suggested - when is your friend.

You could define a condition like:

when: inventory_hostname in groups['webservers']
like image 92
udondan Avatar answered Oct 11 '22 21:10

udondan


An alternative to consider in some scenarios is -

delegate_to: hostname

There is also this example form the ansible docs, to loop over a group. https://docs.ansible.com/ansible/latest/user_guide/playbooks_delegation.html -

- hosts: app_servers

  tasks:
    - name: gather facts from db servers
      setup:
      delegate_to: "{{item}}"
      delegate_facts: True
      loop: "{{groups['dbservers']}}"
like image 11
openCivilisation Avatar answered Oct 11 '22 20:10

openCivilisation