# Conditional actions based on host type?

**URL:** <https://forum.ansible.com/t/conditional-actions-based-on-host-type/33181>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [June 15, 2020, 10:06am UTC](https://forum.ansible.com/t/conditional-actions-based-on-host-type/33181 "2020-06-15T10:06:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Farrington](https://avatars.discourse-cdn.com/v4/letter/m/ba9def/32.png) [@Mark\_Farrington](https://forum.ansible.com/u/Mark_Farrington)\
**Post date:** [June 15, 2020, 10:06am UTC](https://forum.ansible.com/t/conditional-actions-based-on-host-type/33181/1 "2020-06-15T10:06:03Z")

</div>

Hi,

New to ansible and looking for advice.

I have an action inside a task (I think that is correct terminology).

Something like this:

- name: Cloudwatch deploy agent configuration for webserver  
template:  
src: templates/web-inst.json  
dest: “/opt/aws/amazon-cloudwatch-agent/bin/config.json”  
mode: 0644  
tags:
- config

The task is used to install AWS cloudwatch on web servers and this action is to just apply the config. Now I want to extend this task to other types or servers (Lets say app server).  
So I want to add another config action like this:

- name: Cloudwatch deploy agent configuration for App Server  
template:  
src: templates/app-inst.json  
dest: “/opt/aws/amazon-cloudwatch-agent/bin/config.json”  
mode: 0644  
tags:
- config

So, now I have two code blocks for configuration (One for each type of server). How do I conditionally apply the correct config based on the host type?

I currently execute it using the following command.  
ansible-playbook -i …/inventory/testinstance/ -l webserver playbook.yml --tags cloudwatch

So, the idea is I would execute it with the appserver host instead like this:  
ansible-playbook -i …/inventory/testinstance/ -l appserver playbook.yml --tags cloudwatch

Think I can use the “when” statement to do this somehow?

Any help would be greatly received. Alternatively, is this approach just nonsense and there is a better way?

---

<div class="post-metadata">

**Author:** ![Ompragash](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ompragash/32/9070_2.png) [@Ompragash](https://forum.ansible.com/u/Ompragash)\
**Post date:** [June 15, 2020, 10:18am UTC](https://forum.ansible.com/t/conditional-actions-based-on-host-type/33181/2 "2020-06-15T10:18:24Z")

</div>

Hi Mark,

For your use-case, you can consider using Ansible magic variables [https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_variables.html#accessing-information-about-other-hosts-with-magic-variables](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#accessing-information-about-other-hosts-with-magic-variables)

example:

when: inventory\_hostname in groups [‘webserver’]

Regards,  
Ompragash

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [June 15, 2020, 10:39am UTC](https://forum.ansible.com/t/conditional-actions-based-on-host-type/33181/3 "2020-06-15T10:39:17Z")

</div>

Hi

Another option is to have the content of that json config file stored  
as a variable in group\_vars. You can then just use a single task, and  
limit the deployment to webservers or appservers. The config itself  
will be YAML, which is slightly easier to read.  
How feasible this is depends a bit on how complex that json data  
structure is. Do you have an example of that template?

---

<div class="post-metadata">

**Author:** ![Mark\_Farrington](https://avatars.discourse-cdn.com/v4/letter/m/ba9def/32.png) [@Mark\_Farrington](https://forum.ansible.com/u/Mark_Farrington)\
**Post date:** [June 15, 2020, 10:39am UTC](https://forum.ansible.com/t/conditional-actions-based-on-host-type/33181/4 "2020-06-15T10:39:43Z")

</div>

Thanks, That did what I needed.

---

<div class="post-metadata">

**Author:** ![Mark\_Farrington](https://avatars.discourse-cdn.com/v4/letter/m/ba9def/32.png) [@Mark\_Farrington](https://forum.ansible.com/u/Mark_Farrington)\
**Post date:** [June 15, 2020, 10:47am UTC](https://forum.ansible.com/t/conditional-actions-based-on-host-type/33181/5 "2020-06-15T10:47:38Z")

</div>

Many thanks for the alternative. I’m editing an existing project that is already set up this way and I think the config file is sufficiently different enough to justify the use of the template files so I’ll go with the magic variables suggestion.
