Jay Taylor's notes
back to listing indexAnsible | Daniel Hall's Website
[web search]Daniel Hall's Website
Because the Internet doesn't have enough opinions already
Tag Archives: Ansible
Creating RR Records in Route53 with Ansible
So if you are like me and using Ansible to deploy all of your infrastructure, you probably run into more than your fair share of issues. Often Ansible modules take a list or a string, but output fancy structured data. One particular case of this is if you are using the route53 module and creating RR records in DNS. Here is what the route53 module accepts as a value:
- value The new value when creating a DNS record. Multiple comma- spaced values are allowed. When deleting a record all values for the record must be specified or Route53 will not delete it. [Default: None]
So here you can see that it takes a string (the source code indicates it also accepts lists) listing the ip addresses to point to. If you are deploying in a cloud however you probably need to pull the IP addresses from the inventory, which is stored as complex structured data. Extracting a list of IP addresses out of this is an extremely difficult task. To do this we will need to make heavy use of the features Ansible provides us through its Jinja2 variable expressions.
The first thing that we need is a list of all the variables for the machines in a particular group. Ansible doesn’t provide this, but it does provide two similar variables. First, it provides a variable names ‘group’ which gives the names of all the members of a particular group. Secondly it provides the ‘hostvars’ variable which gives you all of the variables in a dictionary with keys as the hostname and the variables as the values. As there is no builtin that does what we want we are going to need a custom filter plugin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
To install this plugin place it in a directory then add that directory to the end of your filter_plugins configuration option. For example if you placed the file in the plugins/filter directory your filter_plugins line might look like this:
filter_plugins = /usr/share/ansible_plugins/filter_plugins:plugins/filter
This plugin will take a dictionary and a list, lookup all the keys within the list and return their values in a new list in the same order as the keys were in. So we can use this to get all the variables for the machines we are interested in. For example to get the variables for the hosts in the webservers group we can now do this:
{{ hostvars|fetchlistfromdict(groups.webservers) }}
Now that we have the right variables we simply need to filter it down to just the attribute we need. Jinja2 provides the map filter for this purpose. Map takes a list and can either pick out a particular attribute, or run another filter on all the items in the list. So to get the IP addresses we would do this:
{{ hostvars|fetchlistfromdict(groups.webservers)|map(attribute='ansible_default_ipv4.address') }}
If you try to print this out in a debug line you might notice that you don’t get a list in response and Ansible instead gives you a string with the following:
<generator object do_map at 0x2d7f640>
To work around this issue you need to explicitly convert it to a list. This is fairly simple with the list filter. Adding that onto this example looks like this:
{{ hostvars|fetchlistfromdict(groups.webservers)|map(attribute='ansible_default_ipv4.address')|list }}
You now have a list of all the IP addresses ready to pass into a template, a role, or in this case into the route53 module. Finally we put this all together to create that round robin record we wanted at the start.
route53: command: create zone: example.com record: rr.example.com type: A value: "{{ hostvars|fetchlistfromdict(groups.webservers)|map(attribute='ansible_default_ipv4.address')|list }}"
Resources
Ansible Talk @ Infra Coders
Here are the notes that I used in my talk at Infrastructure Coders. Each section was also put on the screen as a ‘slide’. The configuration that I used in the demo is available at GitHub. A full video of the meetup is available on the Infrastructure Coders Youtube Channel, my talk starts at 25:05.
Ansible --------
0. There is nothing in the hat - Start a RHEL install - Cmd line: console=ttyS0 ks=http://admin01/ns3.cfg - If you want to follow the demos grab the ansible config from my github - You will need to substitute hostnames in the ansible hosts file - You should copy the firewall config from ns2 (remove port 647 if paranoid) 1. The problem - Ansible is the combination of several functions - There was a plan to build config management on func - However func is a pain to setup - Puppet and Chef have a steep learning curve - Ansible was also built to simplfy complexrelease procedures - You need to know ruby to extend Puppet/Chef 2. Ansible - Designed so you can figure out how to use it in 15 minutes - Designed to be easy to setup - Doesn't require much to be installed on the managed host - Designed to do config management/deployment/ad hoc - Other people do security better, just use SSH - You can extend ansible in any language that can output JSON
3. Simple Ansible Demo - Ansible hosts file - Ansible can be run directly on the command line - Run cat /etc/redhat-release - Get info using the setup module - It can prompt for auth, or use key based auth - On the new machine show it prompting - Run the rhelsetup script on the new machine - Install vim-enhanced 4. Playbooks - This is the method of scripting Ansible - Done in YAML - Executed in order *gee thanks puppet* - Designed to be as easy as possible
5. Example playbook - Playbook for the name servers - https://github.com/smarthall/tildaslash.com/blob/master/playbooks/zones.play - Can have multiple plays in a book - Can serialise if you dont want all to be down at once - Template config for the name servers - https://github.com/smarthall/tildaslash.com/blob/master/playbooks/zones/named.conf.j2 - Firewall install script - https://github.com/smarthall/tildaslash.com/blob/master/playbooks/firewall.play 6. My thoughts - Config management has been around a while, its going from art to science - Ansible covers more ground than puppet and chef do - Ansible doesn't compromise on simplicity to do that - I don't have to focus on the nodes, I can focus on services - There is something missing - Disk config is done in kickstarts - Network config can't be done by Ansible - Need to find a way to cover both with one
The playlist of all the videos is available at Youtube.
Tags
/dev/st0
Recent Tweets
- Reasons you need OPS people #53: DNS Negative caching. 3 days ago
- If you liked my first book, you can pre-order my second one now at packtpub.com/networking-and… 3 days ago
- Well its come to this. I may have to patch Linux. Which is only making this project more exciting! #alwaysbemaking 5 days ago
- And my next hardware project comes alive! http://t.co/jgwEfyDpX8 6 days ago
- This explains EVERYTHING! m.imgur.com/D76CBYU 1 week ago
Sections
- Computing (46)
- Applications (4)
- Arduino (1)
- Linux (23)
- System Administration (13)
- Ansible (1)
- System Administration (13)
- Programming (9)
- C/C++ (2)
- Java (2)
- Web Programming (3)
- Security (4)
- The Internet (4)
- HOWTO (5)
- Uncategorized (2)