how to run a particular task on specific host in ansible

痴心易碎 提交于 2019-12-03 23:31:03

问题


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 ?


回答1:


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']



回答2:


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


来源:https://stackoverflow.com/questions/31912748/how-to-run-a-particular-task-on-specific-host-in-ansible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!