# How to pass hostname to playbook in ansible playbook

**URL:** <https://forum.ansible.com/t/how-to-pass-hostname-to-playbook-in-ansible-playbook/33028>\
**Category:** Ansible Developer\
**Created:** [May 27, 2020, 5:55am UTC](https://forum.ansible.com/t/how-to-pass-hostname-to-playbook-in-ansible-playbook/33028 "2020-05-27T05:55:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Swordfish\_Binary](https://avatars.discourse-cdn.com/v4/letter/s/8dc957/32.png) [@Swordfish\_Binary](https://forum.ansible.com/u/Swordfish_Binary)\
**Post date:** [May 27, 2020, 5:55am UTC](https://forum.ansible.com/t/how-to-pass-hostname-to-playbook-in-ansible-playbook/33028/1 "2020-05-27T05:55:59Z")

</div>

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?
