0

In Ansible, i want to trigger selected tasks based on hosts.

My inventory:

[appserver]
ip1
ip2

[webserver]
ip3
ip4

And my main.yml file will be like this

- name : Playbook
  hosts: all
  user: username
  become: yes
  gather_facts: True

  tasks:

    - name: Task 1 
      replace:
        path: "somepath"
        regexp: "{{ oldvalue }}"
        replace: "{{ newvalue }}"
        backup: yes
    - name: Task 2
      replace:
        path: "somepath"
        regexp: "{{ oldvalue }}"
        replace: "{{ newvalue }}"
        backup: yes

How do I run this particular task1 on appserver group and task2 on webserver group. Is there any best way to achieve this without using ansible tags?

Deepan
  • 119
  • 1
  • 2
  • 8
  • 1
    Add a second play to your playbook. Move the second task to the second play, Target play 1 to `hosts: appserver` and play2 to `hosts: webserver`. – Zeitounator Feb 25 '20 at 15:57

1 Answers1

0
- hosts: appserver
  user: username
  become: yes
  gather_facts: True

  tasks:
    - name: Task 1 
      replace:
        path: "somepath"
        regexp: "{{ oldvalue }}"
        replace: "{{ newvalue }}"
        backup: yes

- hosts: webserver
  user: username
  become: yes
  gather_facts: True

  tasks:
    - name: Task 2
      replace:
        path: "somepath"
        regexp: "{{ oldvalue }}"
        replace: "{{ newvalue }}"
        backup: yes

After posting, I saw @Zeitounator's comment.

Jack
  • 5,801
  • 1
  • 15
  • 20