Ansible Features I Missed
Well, ok some of these aren't particularly new, but I've only just become aware of them. Even after years of Ansible experience, there's always something to learn!
Module Defaults
It's been around since at least 2.7 and the docs are here.
If you frequently call the same module with the same arguments, it can be useful to define default arguments for that particular module using the module_defaults attribute.
If I've got a simple playbook, where I'm calling the same thing over and passing the same information a lot, this is really useful to cut down on duplication and error possibilities.
Where will I be using this mostly though? For spinning up all things public cloud.
The Module defaults group lets you define a whole load of commonly called variables, for AWS, Azure, VMware, k8s and others.
The docs example of setting the AWS region, is a good one. Each AWS module is going to mandate a region as an option, so set it only once so it can be used throughout those modules:
# example_play.yml - hosts: localhost module_defaults: group/aws: region: us-west-2 tasks: - name: Get info aws_s3_bucket_info: # now the region is shared between both info modules - name: Get info ec2_ami_info: filters: name: 'RHEL*7.5*'
That is useful and will cut down on YAML lines!
Anchors & Aliases
Jury's out for me on this one to be to honest!
But I think the java options example in the docs is a useful one.
... vars: app1: jvm: &jvm_opts opts: '-Xms1G -Xmx2G' port: 1000 path: /usr/lib/app1 app2: jvm: <<: *jvm_opts
path: /usr/lib/app2
This looks complicated on the face of it, but it's easy enough to understand -
&jvm_opts is the anchor, or "remember these values"
We can then re-use them for another app definition, using the *jvm_opts alias.
The outcome is that app1 and app2 share the same opts and port definitions, and the path value is merged using <<
For the more complex set of variable structures, this little trick could be useful!
Sr Cloud Engineer and product owner at Aviva
4 年Nice one Phil, shame that module defaults can't be used universally across a load of modules eg. Files or copy! Would save a lot of lines of YAML in some of my playbooks!
Principal Software Engineer at Ansible by Red Hat
4 年I used YAML anchors and aliases to avoid repeating configuration for years and I do recommend them. The issue with module_defaults is that it makes impossible to make use of JSON Schema to validate playbooks/tasks, as you can no longer know which required fields are optional or not (those defined in in defaults will be missing from the call). Alternative is to mark even mandatory arguments as optional in order to avoid false-positive failures for users that make use of module_defaults. As it can easily be seen that is no joy for https://github.com/ansible-community/schemas On the other hand, YAML native features are not affected by this. Still they cannot be used outside a single file and I do expect that ansible module_defaults to effects to be set for the entire playbook, with included tasks and roles.
Head of Automation & Infrastructure - Financial Services
4 年Sweet, wasn’t aware of the defaults!