Conditional actions based on host type?

20 views
Skip to first unread message

Mark Farrington

unread,
Jun 15, 2020, 6:06:03 AM6/15/20
to Ansible Project
Hi,

New to ansible and looking for advice.

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

Something like this:

 - nameCloudwatch deploy agent configuration for webserver
      template:
          srctemplates/web-inst.json
          dest"/opt/aws/amazon-cloudwatch-agent/bin/config.json"
          mode0644
      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:

 - nameCloudwatch deploy agent configuration for App Server
      template:
          srctemplates/app-inst.json
          dest"/opt/aws/amazon-cloudwatch-agent/bin/config.json"
          mode0644
      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?





Ompragash

unread,
Jun 15, 2020, 6:18:56 AM6/15/20
to ansible...@googlegroups.com
Hi Mark,


example:

when: inventory_hostname in groups ['webserver']

Regards,
Ompragash

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/2b0e6bcc-1197-4ade-bec6-b76a3c8a2da7o%40googlegroups.com.

Mark Farrington

unread,
Jun 15, 2020, 6:39:43 AM6/15/20
to Ansible Project
Thanks,   That did what I needed.


On Monday, June 15, 2020 at 11:18:56 AM UTC+1, Ompragash wrote:
Hi Mark,


example:

when: inventory_hostname in groups ['webserver']

Regards,
Ompragash

To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

Dick Visser

unread,
Jun 15, 2020, 6:39:51 AM6/15/20
to ansible...@googlegroups.com
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?
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Mark Farrington

unread,
Jun 15, 2020, 6:47:38 AM6/15/20
to Ansible Project
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.    
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages