How to pass hostname to playbook in ansible playbook

  1. I have a requirement where I need to pass a hostname so that the ansible playbook can take the hostname and do the necessary actions like stopping and starting the service of a application.

How can I pass hostname that comes externally to the ansible playbook?

  • hosts: all
    gather_facts: true
    tasks:
  • name: Set services on job servers in {{ work_env }} to {{ work_op }}
    service:
    name: “{{ item }}”
    state: “{{ work_op }}”
    when:
  • inventory_hostname in groups[work_env]
  • inventory_hostname in groups[“job”]
    loop:
  • rdcstop
  • ratstop
    loop_control:
    label: Set {{ item }} service on {{ inventory_hostname }} to {{ work_op }}.

Here in the above work_env,work_op all are defined in my template. The “job” is defined in my inventory file, this tells what host will fall under the “jobs” category. So, if I remove the inventory file, how will ansible will be able to read the hostname that comes from external calls?