Defining a Data Source<\/H3>
\nLike with Node Sets you can have multiple Data Sources and they are identified by name:<\/p>\n\r\ndata_stores:\r\n pb_data:\r\n type: consul\r\n timeout: 360\r\n ttl: 20\r\n<\/pre>\nThis creates a Consul Data Source called pb_data<\/em>, you need to have a local Consul Agent already set up. I’ll cover the timeout<\/em> and ttl<\/em> a bit later.<\/p>\nPlaybook Locks<\/H3>
\nYou can create locks in Consul and by their nature they are distributed across the Consul network. This means you can ensure a playbook can only be executed once per Consul DC or by giving a custom lock name any group of related playbooks or even other systems that can make Consul locks.<\/p>\n\r\n---\r\nlocks:\r\n - pb_data\r\n - pb_data\/custom_lock\r\n<\/pre>\nThis will create 2 locks in the pb_data<\/em> Data Store – one called custom_lock<\/em> and another called choria\/locks\/playbook\/pb_name<\/em> where pb_name<\/em> is the name from the metadata.<\/p>\nIt will try to acquire a lock for up to timeout<\/em> seconds – 360 here, if it can’t the playbook run fails. The associated session has a TTL of 20 seconds and Choria will renew the sessions around 5 seconds before the TTL expires.<\/p>\nThe TTL will ensure that should the playbook die, crash, machine die or whatever, the lock will release after 20 seconds.<\/p>\n
Binding Variables<\/H3>
\nPlaybooks already have a way to bind CLI arguments to variables called Inputs. Data Sources extend inputs with extra capabilities.<\/p>\n
We now have two types of Input. A static input<\/em> is one where you give the data on the CLI and the data stays static for the life of the playbook. A dynamic input<\/em> is one bound against a Data Source and the value of it is fetched every time you reference the variable.<\/p>\n\r\ninputs:\r\n cluster:\r\n description: \"Cluster to deploy\"\r\n type: \"String\"\r\n required: true\r\n data: \"pb_data\/choria\/kv\/cluster\"\r\n default: \"alpha\"\r\n<\/pre>\nHere we have a input called cluster<\/em> bound to the choria\/kv\/cluster<\/em> key in Consul. This starts life as a static input<\/em> and if you give this value on the CLI it will never use the Data Source.<\/p>\nIf however you do not specify a CLI value it becomes dynamic<\/em> and will consult Consul. If there’s no such key in Consul the default is used, but the input remains dynamic and will continue to consult Consul on every access.<\/p>\nYou can force an input to be dynamic<\/em> which will mean it will not show up on the CLI and will only speak to a data source using the dynamic: true<\/em> property on the Input.<\/p>\nWriting and Deleting Data<\/H3>
\nOf course if you can read data you should be able to write and delete it, I’ve added tasks to let you do this:<\/p>\n\r\nlocks:\r\n - pb_data\r\n\r\ninputs:\r\n cluster:\r\n description: \"Cluster to deploy\"\r\n type: \"String\"\r\n required: true\r\n data: \"pb_data\/choria\/kv\/cluster\"\r\n default: \"alpha\"\r\n validation: \":shellsafe\"\r\n\r\nhooks:\r\n pre_book:\r\n - data:\r\n action: \"delete\"\r\n key: \"pb_data\/choria\/kv\/cluster\"\r\n\r\ntasks:\r\n - shell:\r\n description: Deploy to cluster {{{ inputs.cluster }}}\r\n command: \/path\/to\/script --cluster {{{ inputs.cluster }}}\r\n\r\n - data:\r\n action: \"write\"\r\n value: \"bravo\"\r\n key: \"pb_data\/choria\/kv\/cluster\"\r\n\r\n - shell:\r\n description: Deploy to cluster {{{ inputs.cluster }}}\r\n command: \/path\/to\/script --cluster {{{ inputs.cluster }}}\r\n<\/pre>\nHere I have a pre_book<\/em> task list that ensures there is no stale data, the lock ensures no other Playbook will mess around with the data while we run.<\/p>\nI then run a shell command that uses the cluster<\/em> input, with nothing there it uses the default and so deploys cluster alpha<\/em>, it then writes a new value and deploys cluster brova<\/em>.<\/p>\nThis is a bit verbose I hope to add the ability to have arbitrarily named tasks lists that you can branch to, then you can have 1 deploy<\/em> task list and use the main task list to set up variables for it and call it repeatedly.<\/p>\nConclusion<\/H2>
\nThat’s quite a mouthful, the possibilities of this is quite amazing. On one hand we have a really versatile data store in the Playbooks but more significantly we have expanded the integration possibilities by quite a bit, you can now have other systems manage the environment your playbooks run in.<\/p>\n
I will soon add task level locks and of course Node Set integration.<\/p>\n
For now only Consul and Memory is supported, I can add others if there is demand.<\/p>\n","protected":false},"excerpt":{"rendered":"
About a month ago I blogged about Choria Playbooks – a way to write series of actions like MCollective, Shell, Slack, Web Hooks and others – contained within a YAML script with inputs, node sets and more. Since then I added quite a few tweaks, features and docs, it’s well worth a visit to choria.io […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","footnotes":""},"categories":[7],"tags":[126,78,21],"_links":{"self":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts\/3570"}],"collection":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/comments?post=3570"}],"version-history":[{"count":7,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts\/3570\/revisions"}],"predecessor-version":[{"id":3577,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts\/3570\/revisions\/3577"}],"wp:attachment":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/media?parent=3570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/categories?post=3570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/tags?post=3570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}