{"id":1239,"date":"2021-10-11T13:27:12","date_gmt":"2021-10-11T11:27:12","guid":{"rendered":"https:\/\/nine30.info\/?p=1239"},"modified":"2021-10-11T13:27:12","modified_gmt":"2021-10-11T11:27:12","slug":"vra-saltstack-config-pillars","status":"publish","type":"post","link":"https:\/\/nine30.nxt70.com\/index.php\/2021\/10\/11\/vra-saltstack-config-pillars\/","title":{"rendered":"vRA SaltStack Config \u2013 Pillars"},"content":{"rendered":"\n<p>Salt Pillars are key elements to have parametric Salt State files allowing them to be more flexible and reusable. Pillars are data structures defined on the Salt Master and distributed to minions. They allow confidential, targeted data to be securely sent only to the relevant Minions. Pillar data structure can be used for:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Minion configuration: Minion modules such as the execution modules, states, and returners can often be configured via data stored in Pillars.<\/li><li>Variables: variables which need to be assigned to specific Minions or groups of Minions can be defined in Pillars and then accessed inside sls formulas and template files.<\/li><li>Arbitrary data: Pillars can contain any basic data structure in dictionary format, so a key\/value store can be defined making it easy to iterate over a group of values in sls formulas.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Salt Pillar Top File<\/h2>\n\n\n\n<p>Salt Pillars use a Top file to match Salt Pillar data to Salt Minions. See my <a rel=\"noreferrer noopener\" href=\"https:\/\/nine30.info\/vra-saltstack-config-top-files\/\" target=\"_blank\">post about Top File<\/a> if you need a refresher about Top files. This Top file is very much like the Top file that is used to match Salt State files to Salt Minions. Like Top File, Salt Pillar is best learned by example, here is an very simple example.<\/p>\n\n\n\n<p>Pillar Top File named <code>top.sls<\/code> (the name has to be exactly this):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>base:\n  '*':\n    - defaults<\/code><\/pre>\n\n\n\n<p>Pillar data file <code>defaults.sls<\/code> in the same directory of the Top file. In the example below we have two key\/value pairs:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>shell: bash\neditor: vim<\/code><\/pre>\n\n\n\n<p>Pillar data refresh (the action to provide Pillar data to targeted Minions):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>salt '*' saltutil.refresh_pillar<\/code><\/pre>\n\n\n\n<p>When Salt Pillar data is refreshed, each Salt Minion is matched against the targets listed in the <code>top.sls<\/code> file. When a Salt Minion matches a target, it receives all of the Salt Pillar SLS files defined in the list underneath that target.<\/p>\n\n\n\n<p>In order to sees Minion&#8217;s Pillar use the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>salt '*' pillar.items<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Salt Pillar in Salt States<\/h2>\n\n\n\n<p>We can reference Pillar data in Salt States by means of templating, here is a State File that leverages key\/values defined in the defaults.sls Pillar data file introduced before:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vim_install:\n  pkg.installed:\n    - name: {{ pillar&#91;'editor'] }}<\/code><\/pre>\n\n\n\n<p>Actually Pillar data files are three-like data structures, so we can have a bit more complex data structures. Let&#8217;s assume to modify our defaults.sls file as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>system:\n  <meta charset=\"utf-8\">shell: bash\n  editor: vim\nports:\n  http: 8080<\/code><\/pre>\n\n\n\n<p>As a consequence our State file should change as follow in order to recognize that now they key <code>editor<\/code> is nested under <code>system<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><meta charset=\"utf-8\">vim_install:\n  pkg.installed:\n    - name: {{ pillar&#91;'system']&#91;'editor'] }}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Get Started with Pillar<\/h2>\n\n\n\n<p>There are few things we need to know and configure on the Salt Master before we can start playing with Pillar.<\/p>\n\n\n\n<p>We need to start setting up Pillar making sure the directory <code>\/srv\/pillar<\/code> exists on you Salt Master:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir \/srv\/pillar<\/code><\/pre>\n\n\n\n<p>The Pillar location can be configured using the&nbsp;<code>pillar_roots<\/code>&nbsp;option in the Master configuration file. It must not be in a subdirectory of the state tree or <code>file_roots<\/code>. If the pillar is under <code>file_roots<\/code>, any pillar targeting can be bypassed by minions. Here after there is the piece of configuration I have in my Master configuration file. Please, note that you can manage multiple environments, external Pillars (e.g. source your Pillars file from a git repository) and other nice things, but this goes beyond the scope of this post.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pillar_roots:\n  base:\n    - \/srv\/pillar<\/code><\/pre>\n\n\n\n<p>Do not forget to restart you Salt Master service in order to make sure the new configuration is taken into account by the Master:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>systemctl restart salt-master<\/code><\/pre>\n\n\n\n<p>Store your <code>top.sls<\/code> file and any Pillar SLS file referenced in the Top file in the <code>\/srv\/pillar<\/code> directory. Now you can use the <meta charset=\"utf-8\"><code>saltutil.refresh_pillar<\/code> and the <code>pillar.items<\/code> as introduced above to assign and check Pillar data against Minions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Lab Time!<\/h2>\n\n\n\n<p>In my lab I have some Minions, but for this activity I will restrict the scope to 3 minions having the string&nbsp;<code>secops<\/code>&nbsp;in their Minion IDs.<\/p>\n\n\n\n<p>In the \/srv\/pillar directory I have the following files: <\/p>\n\n\n\n<p>Top file, as usual named <code>top.sls<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>base:\n  'os:CentOS':\n    - match: grain\n    - centosdefaults\n  'os:Ubuntu':\n    - match: grain\n    - ubuntudefaults\n  'os:Windows':\n    - match: grain\n    - windowsdefaults<\/code><\/pre>\n\n\n\n<p>Pillar SLS for CentOS Minions named <code>centosdefaults.sls<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>path: \/opt\/iberia_lab\nuser: root\ngroup: root\nmode: 755<\/code><\/pre>\n\n\n\n<p><meta charset=\"utf-8\">Pillar SLS for Ubuntu Minions named <code>ubuntudefaults.sls<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>path: \/opt\/iberia_lab\nuser: seiberia\ngroup: seiberia\nmode: 755<\/code><\/pre>\n\n\n\n<p>Pillar SLS for Windows Minions named <code>windowsdefaults.sls<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>path: C:\\iberia_lab\nuser: Administrator\ngroup: na\nmode: na<\/code><\/pre>\n\n\n\n<p>Here is how my <code>\/srv\/pillar<\/code> directory looks like:<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" data-id=\"1264\" src=\"https:\/\/nine30.info\/wp-content\/uploads\/2021\/10\/1.pillar.directory-1024x543.png\" alt=\"\" class=\"wp-image-1264\"\/><\/figure>\n<\/figure>\n\n\n\n<p>Now I can refresh Pillar:<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-2 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" data-id=\"1262\" src=\"https:\/\/nine30.info\/wp-content\/uploads\/2021\/10\/2.refresh.pillar.new_-1024x543.png\" alt=\"\" class=\"wp-image-1262\"\/><\/figure>\n<\/figure>\n\n\n\n<p>I can check Pillar data assignment to Minions:<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-3 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" data-id=\"1261\" src=\"https:\/\/nine30.info\/wp-content\/uploads\/2021\/10\/3.pillar.items_.new_-1024x677.png\" alt=\"\" class=\"wp-image-1261\"\/><\/figure>\n<\/figure>\n\n\n\n<p>Now that I have Pillars data available in my Minions I can make good use of them. For this purpose I have a simple State file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># This State file ensure all systems have default directory according to company policy\n\n# Ensure default directory is there\ncreate standard_directory:\n file.directory:\n   - name: {{ pillar&#91;'path'] }}\n   - user: {{ pillar&#91;'user'] }}\n   - group: {{ pillar&#91;'group'] }}\n   - mode: {{ pillar&#91;'mode'] }}<\/code><\/pre>\n\n\n\n<p>Apply the State file and see it using different values available in different Minions:<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-4 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" data-id=\"1258\" src=\"https:\/\/nine30.info\/wp-content\/uploads\/2021\/10\/4.apply_.ubuntu.png\" alt=\"\" class=\"wp-image-1258\"\/><\/figure>\n<\/figure>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-5 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" data-id=\"1259\" src=\"https:\/\/nine30.info\/wp-content\/uploads\/2021\/10\/5.apply_.centos.png\" alt=\"\" class=\"wp-image-1259\"\/><\/figure>\n<\/figure>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-6 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" data-id=\"1260\" src=\"https:\/\/nine30.info\/wp-content\/uploads\/2021\/10\/6.apply_.windows-1024x543.png\" alt=\"\" class=\"wp-image-1260\"\/><\/figure>\n<\/figure>\n\n\n\n<p>With this post I am just scratching the surface of a powerful yet simple mechanism such as Pillar, however even with this basic notions you can start doing real life stuff and automate (A LOT!) in your environment.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Salt Pillars are key elements to have parametric Salt State files allowing them to be more flexible and reusable. Here is an introduction to Salt Pillar<\/p>\n","protected":false},"author":1,"featured_media":1275,"comment_status":"open","ping_status":"open","sticky":false,"template":"templates\/template-cover.php","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[23,29,48,99,122,126,131,143,144,145,167,172,185,186],"class_list":["post-1239","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tech","tag-automation","tag-bash","tag-data","tag-linux","tag-opensource","tag-oss","tag-pillar","tag-salt","tag-saltproject","tag-saltstack","tag-templating","tag-vim","tag-vrealize","tag-vrealizeautomation","entry"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/nine30.nxt70.com\/index.php\/wp-json\/wp\/v2\/posts\/1239","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nine30.nxt70.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nine30.nxt70.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nine30.nxt70.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nine30.nxt70.com\/index.php\/wp-json\/wp\/v2\/comments?post=1239"}],"version-history":[{"count":0,"href":"https:\/\/nine30.nxt70.com\/index.php\/wp-json\/wp\/v2\/posts\/1239\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nine30.nxt70.com\/index.php\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/nine30.nxt70.com\/index.php\/wp-json\/wp\/v2\/media?parent=1239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nine30.nxt70.com\/index.php\/wp-json\/wp\/v2\/categories?post=1239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nine30.nxt70.com\/index.php\/wp-json\/wp\/v2\/tags?post=1239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}