Ansible 2.0.1 the same playbooks for aws and openstack iaas: how to differentiate?

Hi All :slight_smile:

I am using ansible 2.0.1 on both aws and openstack kilo. I would like to run the same playbook with appropriate conditions so that part of it would be run when iaas is aws and another part when iaas is openstack. I checked with the setup module that:

  • for aws vm:

$ ansible -m setup 127.0.0.1 | egrep -i ‘aws|amazon|openstack|virtualization’
“ansible_bios_version”: “4.2.amazon”,
“ansible_product_version”: “4.2.amazon”,
“ansible_virtualization_role”: “guest”,
“ansible_virtualization_type”: “xen”,

  • for openstack vm:

ansible -m setup 127.0.0.1 | egrep -i ‘aws|amazon|openstack|virtualization’
“ansible_product_name”: “OpenStack Nova”,
“ansible_system_vendor”: “OpenStack Foundation”,
“ansible_virtualization_role”: “host”,
“ansible_virtualization_type”: “kvm”,

so I can differentiate/divide tasks based on for example regex on “ansible_product_name”. Is there a better way to build playbooks for aws and openstack?

BR,
Rafal.

You can use those variables in conjunction with Ansible conditionals and blocks

Note that Ansible works best when you explicitly define what the entire host is supposed to look like, without using conditionals. This follows Ansible’s seemingly “declarative” philosophy.

Once you start getting into using conditionals, you’ll also start deviating towards “imperative” logic that doesn’t really fit well within Ansible. It’s a bit of a shame, because that imperative type of logic would really help Ansible become more composable.