{"id":7323,"date":"2018-04-06T09:00:09","date_gmt":"2018-04-06T01:00:09","guid":{"rendered":"http:\/\/rmohan.com\/?p=7323"},"modified":"2018-04-06T09:00:09","modified_gmt":"2018-04-06T01:00:09","slug":"including-variables-in-ansible","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=7323","title":{"rendered":"Including variables in Ansible"},"content":{"rendered":"<p>Including variables in Ansible<\/p>\n<p>=====================================<br \/>\n[root@ansible1 ~]$ mkdir nginx<br \/>\n=====================================<br \/>\n[root@ansible1 ~]$ cd nginx<br \/>\n[root@ansible1 nginx]$ mkdir tasks vars<\/p>\n<p>=====================================<br \/>\n[root@ansible1 nginx]$ touch tasks\/environment.yml<br \/>\n=====================================<br \/>\n[root@ansible1 nginx]$ touch vars\/variables.yml<br \/>\n=====================================<br \/>\n[root@ansible1 ~]$ tree nginx<br \/>\nnginx<br \/>\n|\u2013 nginx.yml<br \/>\n|\u2013 tasks<br \/>\n| `\u2013 environment.yml<br \/>\n`\u2013 vars<br \/>\n`\u2013 variables.yml<br \/>\n=====================================<br \/>\n[root@ansible1 nginx]$ vim tasks\/environment.yml<br \/>\n\u2014<br \/>\n\u2013 name: install package<br \/>\nyum:<br \/>\nname: \u201c{{ package }}\u201d<br \/>\nstate: latest<br \/>\n\u2013 name: start service<br \/>\nservice:<br \/>\nname: \u201c{{ service }}\u201d<br \/>\nstate: \u201c{{ svc_state }}\u201d<br \/>\n=====================================<br \/>\n[root@ansible1 nginx]$ vim vars\/variables.yml<br \/>\n\u2014<br \/>\nfirewall_pkg: firewalld<br \/>\n=====================================<br \/>\n[root@ansible1 nginx]$ vim nginx.yml<br \/>\n\u2014<br \/>\n\u2013 name: anything<br \/>\nhosts: db<br \/>\nremote_user: root<br \/>\nbecome: true<br \/>\nbecome_method: sudo<br \/>\nbecome_user: root<br \/>\nvars:<br \/>\nrule: http<br \/>\ntasks:<br \/>\n\u2013 name: Include the variables from yaml file<br \/>\ninclude_vars: vars\/variables.yml<\/p>\n<p>\u2013 name: include env variables and set the variables<br \/>\ninclude: tasks\/environment.yml<br \/>\nvars:<br \/>\npackage: nginx<br \/>\nservice: nginx<br \/>\nsvc_state: started<\/p>\n<p>\u2013 name: install fiirewall pkg<br \/>\nyum:<br \/>\nname: \u201c{{ firewall_pkg }}\u201d<br \/>\nstate: latest<br \/>\n\u2013 name: start firewalld<br \/>\nservice:<br \/>\nname: firewalld<br \/>\nstate: started<br \/>\nenabled: true<br \/>\n=====================================<br \/>\n[root@ansible1 nginx]$ ansible-playbook \u2013syntax-check nginx.yml<\/p>\n<p>playbook: nginx.yml<br \/>\n=====================================<br \/>\n[root@ansible1 nginx]$ ansible-playbook nginx.yml<\/p>\n<p>PLAY [anything] ****************************************************************<\/p>\n<p>TASK [setup] *******************************************************************<br \/>\nok: [192.168.1.23]<\/p>\n<p>TASK [Include the variables from yaml file] ************************************<br \/>\nok: [192.168.1.23]<\/p>\n<p>TASK [install package] *********************************************************<br \/>\nok: [192.168.1.23]<\/p>\n<p>TASK [start service] ***********************************************************<br \/>\nok: [192.168.1.23]<\/p>\n<p>TASK [install fiirewall pkg] ***************************************************<br \/>\nok: [192.168.1.23]<\/p>\n<p>TASK [start firewalld] *********************************************************<br \/>\nok: [192.168.1.23]<\/p>\n<p>PLAY RECAP *********************************************************************<br \/>\n192.168.1.23 : ok=6 changed=0 unreachable=0 failed=0<br \/>\n=====================================<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Including variables in Ansible<\/p>\n<p>===================================== [root@ansible1 ~]$ mkdir nginx ===================================== [root@ansible1 ~]$ cd nginx [root@ansible1 nginx]$ mkdir tasks vars<\/p>\n<p>===================================== [root@ansible1 nginx]$ touch tasks\/environment.yml ===================================== [root@ansible1 nginx]$ touch vars\/variables.yml ===================================== [root@ansible1 ~]$ tree nginx nginx |\u2013 nginx.yml |\u2013 tasks | `\u2013 environment.yml `\u2013 vars `\u2013 variables.yml ===================================== [root@ansible1 nginx]$ vim tasks\/environment.yml \u2014 \u2013 name: install [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[91],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7323"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7323"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7323\/revisions"}],"predecessor-version":[{"id":7324,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7323\/revisions\/7324"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}