# playbook without hosts desribed

**URL:** <https://forum.ansible.com/t/playbook-without-hosts-desribed/30977>\
**Category:** Ansible Project\
**Created:** [August 7, 2019, 4:55pm UTC](https://forum.ansible.com/t/playbook-without-hosts-desribed/30977 "2019-08-07T16:55:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [August 7, 2019, 4:55pm UTC](https://forum.ansible.com/t/playbook-without-hosts-desribed/30977/1 "2019-08-07T16:55:02Z")

</div>

Hi,

Is there a way that we can use a playbook with no “hosts: XXX” defined.  
All the servers are available in the inventory file defined in the ansible.cfg , but the playbooks have to be run based on a single or multiple client on the ad-hoc request based.

Is there a way to remove  
`

hosts: all  
`

and have the same playbook working for security reasons ?  
In the meantime when a play is to be executed in all the clients they have to be mentioned from the command line(or jenkins)

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 7, 2019, 5:37pm UTC](https://forum.ansible.com/t/playbook-without-hosts-desribed/30977/2 "2019-08-07T17:37:04Z")

</div>

> Hi,
> 
> Is there a way that we can use a playbook with no "hosts: XXX" defined.

No, hosts is a required attribute for a play.

> All the servers are available in the inventory file defined in the  
> ansible.cfg , but the playbooks have to be run based on a single or  
> multiple client on the ad-hoc request based.

You can always set hosts: all and use the --limit to only run on some hosts.

> Is there a way to remove
> 
> &nbsp;&nbsp;hosts: all
> 
> &nbsp;&nbsp;and have the same playbook working for security reasons ?

I don't understand what you mean by "for security reasons".

> &nbsp;&nbsp;In the meantime when a play is to be executed in all the clients they have  
> to be mentioned from the command line(or jenkins)

You can use a variable for hosts an set it with extra vars on the command line

- hosts: '{{ myvar }}'

ansible-playbook pb.yml -e myvar=all

---

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [August 8, 2019, 1:53am UTC](https://forum.ansible.com/t/playbook-without-hosts-desribed/30977/3 "2019-08-08T01:53:39Z")

</div>

Hi kai,

Thanks for the inputs.

“for security reasons”. - I mean that we have disabled gather\_facts for better performance. And we want other users to play the playbook with access to the same inventory .  
I am looking for a way to avoid them running “ansible-playbook pb.yml -e myvar=all” and to always use “myvar=hostname.”

is there a way to stop a particular user to stop using " myvar=all" based on inventory or other modes?

---

<div class="post-metadata">

**Author:** ![mailinglists-ansible](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@mailinglists-ansible](https://forum.ansible.com/u/mailinglists-ansible)\
**Post date:** [August 8, 2019, 5:57am UTC](https://forum.ansible.com/t/playbook-without-hosts-desribed/30977/4 "2019-08-08T05:57:40Z")

</div>

Hi,

you could start with a task running against the localhost doing `jinja2`  
magic and see, if `myvar=!al` and then run the next task with

```auto
hosts: "{{ myvar}}"

```

Greets  
J

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 8, 2019, 7:12am UTC](https://forum.ansible.com/t/playbook-without-hosts-desribed/30977/5 "2019-08-08T07:12:37Z")

</div>

You could use this  
&nbsp;&nbsp;hosts: '{{ "" if myvar == "all" else myvar }}'

If myvar is all hosts becomes empty sting and that is not allowed and Ansible will fail.

But this is easily defeated but adding a comma at the end myvar=all,  
To fix that you would need to use the search/regex[1] test and craft a appropriate regexp for this.

[1] [https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_tests.html#testing-strings](https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#testing-strings)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [August 8, 2019, 5:33pm UTC](https://forum.ansible.com/t/playbook-without-hosts-desribed/30977/6 "2019-08-08T17:33:44Z")

</div>

Ansible is not designed to limit users in this way, you want something  
like tower/awx that can limit access to what users execute.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 8, 2019, 5:36pm UTC](https://forum.ansible.com/t/playbook-without-hosts-desribed/30977/7 "2019-08-08T17:36:07Z")

</div>

But it works so I don't see a problem.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [August 8, 2019, 5:36pm UTC](https://forum.ansible.com/t/playbook-without-hosts-desribed/30977/8 "2019-08-08T17:36:57Z")

</div>

its easy to bypass, if it is a security issue, they should really look  
at something that uses RBAC.
