{"id":1724,"date":"2020-03-21T18:47:13","date_gmt":"2020-03-21T15:47:13","guid":{"rendered":"https:\/\/artem.services\/?p=1718"},"modified":"2020-03-21T18:50:46","modified_gmt":"2020-03-21T15:50:46","slug":"1724","status":"publish","type":"post","link":"https:\/\/artem.services\/?p=1724&lang=en","title":{"rendered":"Ansible &#8212; Number of hosts per group"},"content":{"rendered":"<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1393\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2019\/08\/ansible-logo.png\" alt=\"\" width=\"1280\" height=\"720\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2019\/08\/ansible-logo.png 1280w, https:\/\/artem.services\/wp-content\/uploads\/2019\/08\/ansible-logo-300x169.png 300w, https:\/\/artem.services\/wp-content\/uploads\/2019\/08\/ansible-logo-768x432.png 768w, https:\/\/artem.services\/wp-content\/uploads\/2019\/08\/ansible-logo-1024x576.png 1024w, https:\/\/artem.services\/wp-content\/uploads\/2019\/08\/ansible-logo-954x537.png 954w\" sizes=\"(max-width: 1280px) 100vw, 1280px\" \/><\/p>\n<p>There is a &quot;<strong>hosts<\/strong>&quot; file in which in the &quot;<strong>db<\/strong>&quot; group there are<strong> N<\/strong> number of hosts, you need to automatically set a variable in the playbook equal to the number of hosts in a particular group<\/p>\n<h3>hosts<\/h3>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n[db]\r\ndb1 ansible_host=192.168.1.101\r\ndb2 ansible_host=192.168.1.102\r\ndb3 ansible_host=192.168.1.103\r\ndb4 ansible_host=192.168.1.104\r\n...\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3>example.yaml<\/h3>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n---\r\n- hosts: localhost\r\n  roles:\r\n    - db\/example\r\n  vars:\r\n    DB_COUNT: &quot;{{ groups['db'] | length }}&quot;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>There is a &quot;hosts&quot; file in which in the &quot;db&quot; group there are N number of hosts, you need to automatically set a variable in the playbook equal to the number of hosts in a particular group hosts &nbsp; example.yaml<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1131],"tags":[1133,1371],"_links":{"self":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1724"}],"collection":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1724"}],"version-history":[{"count":2,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1724\/revisions"}],"predecessor-version":[{"id":1726,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1724\/revisions\/1726"}],"wp:attachment":[{"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}