Javascript required
Skip to content Skip to sidebar Skip to footer

The Api Server Https galaxy ansible com api Is Not Responding Please Try Again Later

Ansible - Roles


Roles provide a framework for fully independent, or interdependent collections of variables, tasks, files, templates, and modules.

In Ansible, the part is the primary mechanism for breaking a playbook into multiple files. This simplifies writing complex playbooks, and it makes them easier to reuse. The breaking of playbook allows yous to logically break the playbook into reusable components.

Each office is basically limited to a detail functionality or desired output, with all the necessary steps to provide that result either within that role itself or in other roles listed as dependencies.

Roles are not playbooks. Roles are small functionality which can exist independently used but have to be used within playbooks. There is no way to straight execute a role. Roles have no explicit setting for which host the role will utilise to.

Peak-level playbooks are the bridge holding the hosts from your inventory file to roles that should be practical to those hosts.

Creating a New Office

The directory structure for roles is essential to create a new role.

Part Structure

Roles have a structured layout on the file organisation. The default structure can be changed simply for at present allow us stick to defaults.

Each role is a directory tree in itself. The role proper name is the directory name within the /roles directory.

$ ansible-milky way -h        

Usage

ansible-galaxy [delete|import|info|init|install|listing|login|remove|search|setup] [--help] [options] ...        

Options

  • -h, --assistance − Bear witness this help message and exit.

  • -v, --verbose − Verbose style (-vvv for more, -vvvv to enable connection debugging)

  • --version − Show plan's version number and go out.

Creating a Role Directory

The higher up command has created the office directories.

$ ansible-galaxy init vivekrole  ERROR! The API server (https://galaxy.ansible.com/api/) is not responding, delight try again afterwards.   $ ansible-galaxy init --forcefulness --offline vivekrole  - vivekrole was created successfully   $ tree vivekrole/  vivekrole/  ├── defaults  │   └── main.yml  ├── files ├── handlers  │   └── main.yml  ├── meta  │   └── main.yml  ├── README.dr. ├── tasks  │   └── primary.yml  ├── templates ├── tests │   ├── inventory  │   └── test.yml  └── vars      └── main.yml    viii directories, viii files        

Not all the directories will exist used in the example and we will testify the utilise of some of them in the example.

Utilizing Roles in Playbook

This is the lawmaking of the playbook we have written for demo purpose. This lawmaking is of the playbook vivek_orchestrate.yml. We have divers the hosts: tomcat-node and called the ii roles – install-tomcat and start-tomcat.

The problem statement is that we accept a war which we demand to deploy on a machine via Ansible.

---  - hosts: tomcat-node  roles:     - {role: install-tomcat}     - {role: offset-tomcat}        

Contents of our directory structure from where we are running the playbook.

Directory

$ ls  ansible.cfg  hosts  roles  vivek_orchestrate.retry vivek_orchestrate.yml        

Roles

There is a tasks directory under each directory and information technology contains a main.yml. The main.yml contents of install-tomcat are −

---  #Install vivek artifacts  -      block:        - name: Install Tomcat artifacts          activeness: >              yum proper name = "demo-tomcat-1" state = present           annals: Output                always:        - debug:           msg:              - "Install Tomcat artifacts chore ended with bulletin: {{Output}}"              - "Installed Tomcat artifacts - {{Output.changed}}"        

The contents of main.yml of the start tomcat are −

#Start Tomcat           -      block:        - proper noun: Start Tomcat        control: <path of tomcat>/bin/startup.sh"        register: output        become: truthful         always:        - debug:           msg:              - "Beginning Tomcat job ended with message: {{output}}"              - "Tomcat started - {{output.changed}}"        

The reward of breaking the playbook into roles is that anyone who wants to use the Install tomcat feature can call the Install Tomcat office.

Breaking a Playbook into a Role

If not for the roles, the content of the main.yml of the respective function can be copied in the playbook yml file. But to accept modularity, roles were created.

Any logical entity which tin can exist reused equally a reusable function, that entity tin be moved to function. The case for this is shown in a higher place

Ran the command to run the playbook.

-vvv option for verbose output – verbose output  $ cd vivek-playbook/        

This is the command to run the playbook

$ sudo ansible-playbook -i hosts vivek_orchestrate.yml –vvv  ----------------------------------------------------------------- -----------------------------------------------------------------------        

Output

The generated output is as seen on the screen −

Using /users/demo/vivek-playbook/ansible.cfg as config file.

PLAYBOOK: vivek_orchestrate.yml ********************************************************* ***********************************************************  ane plays in vivek_orchestrate.yml   PLAY [tomcat-node] ********************************************************************** ******** *************************************************    Chore [Gathering Facts] ************************************************* ****************************** *********************************************  Tuesday 21 November 2017  xiii:02:05 +0530 (0:00:00.056) 0:00:00.056 ******  Using module file /usr/lib/python2.7/sitepackages/ansible/modules/system/setup.py  <localhost> ESTABLISH LOCAL Connection FOR USER: root  <localhost> EXEC /bin/sh -c 'repeat ~ && slumber 0'  <localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo     /root/.ansible/tmp/ansible-tmp-1511249525.88-259535494116870 `" &&     repeat ansible-tmp-1511249525.88-259535494116870="`     echo /root/.ansible/tmp/ansibletmp-1511249525.88-259535494116870 `" ) && slumber 0'  <localhost> PUT /tmp/tmpPEPrkd TO     /root/.ansible/tmp/ansible-tmp-1511249525.88259535494116870/setup.py  <localhost> EXEC /bin/sh -c 'chmod u+x     /root/.ansible/tmp/ansible-tmp1511249525.88-259535494116870/     /root/.ansible/tmp/ansible-tmp-1511249525.88259535494116870/setup.py && sleep 0'  <localhost> EXEC /bin/sh -c '/usr/bin/python     /root/.ansible/tmp/ansible-tmp1511249525.88-259535494116870/setup.py; rm -rf     "/root/.ansible/tmp/ansible-tmp1511249525.88-259535494116870/" > /dev/null ii>&one && sleep 0'  ok: [server1]  META: ran handlers    TASK [install-tomcat : Install Tomcat artifacts] *********************************** ***************************************************************  task path: /users/demo/vivek-playbook/roles/install-tomcat/tasks/main.yml:5  Tuesday 21 November 2017  thirteen:02:07 +0530 (0:00:01.515)       0:00:01.572 ******  Using module file /usr/lib/python2.7/sitepackages/ansible/modules/packaging/os/yum.py  <localhost> ESTABLISH LOCAL Connectedness FOR USER: root  <localhost> EXEC /bin/sh -c 'echo ~ && sleep 0'  <localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` repeat     /root/.ansible/tmp/ansible-tmp-1511249527.34-40247177825302 `" && echo     ansibletmp-1511249527.34-40247177825302="` repeat     /root/.ansible/tmp/ansible-tmp1511249527.34-40247177825302 `" ) && sleep 0'  <localhost> PUT /tmp/tmpu83chg TO     /root/.ansible/tmp/ansible-tmp-1511249527.3440247177825302/yum.py  <localhost> EXEC /bin/sh -c 'chmod u+x     /root/.ansible/tmp/ansible-tmp1511249527.34-40247177825302/     /root/.ansible/tmp/ansible-tmp-1511249527.3440247177825302/yum.py && sleep 0'  <localhost> EXEC /bin/sh -c '/usr/bin/python     /root/.ansible/tmp/ansible-tmp1511249527.34-40247177825302/yum.py; rm -rf     "/root/.ansible/tmp/ansible-tmp1511249527.34-40247177825302/" > /dev/null two>    &1 && sleep 0'  changed: [server1] => {     "inverse": true,     "invocation": {        "module_args": {           "conf_file": null,           "disable_gpg_check": simulated,           "disablerepo": naught,           "enablerepo": null,           "exclude": nothing,           "install_repoquery": true,           "installroot": "/",           "list": null,           "name": ["demo-tomcat-1"],           "skip_broken": fake,           "state": "present",           "update_cache": false,           "validate_certs": true        }     },     "msg": "",     "rc": 0,     "results": [        "Loaded plugins: product-id,        search-disabled-repos,        subscriptionmanager\nThis system is not registered to Red Chapeau Subscription Management.        You tin use subscription-director to annals.\nResolving Dependencies\n-->        Running transaction check\n--->        Parcel demo-tomcat-1.noarch 0:SNAPSHOT-1 will be installed\n--> Finished Dependency        Resolution\n\nDependencies Resolved\northward       \north================================================================================\n        Package Arch Version Repository                Size\north==================================================================\nInstalling:\n        demo-tomcat-one noarch SNAPSHOT-1 demo-repo1 7.1 K\n\nTransaction        Summary\n==================================================================\nInstall  1        Packet\n\nTotal download size: vii.1 Chiliad\nInstalled size: 7.9 K\nDownloading           packages:\nRunning transaction        check\nRunning transaction test\nTransaction exam succeeded\nRunning transaction\north  Installing :        demotomcat-i-SNAPSHOT-1.noarch 1/ane \n  Verifying  :        demo-tomcat-1-SNAPSHOT-1.noarch 1/1 \n\nInstalled:\northward         demo-tomcat-1.noarch 0:SNAPSHOT-one \n\nComplete!\n"     ]  }    TASK [install-tomcat : debug] ********************************************************** ***************************************************************************  task path: /users/demo/vivek-playbook/roles/install-tomcat/tasks/main.yml:xi  Tuesday 21 November 2017  13:02:thirteen +0530 (0:00:06.757) 0:00:08.329 ******  ok: [server1] => {     "changed": false,     "msg": [        "Install Tomcat artifacts job ended with message: {          u'msg': u'', u'changed': True, u'results':           [u'Loaded plugins: product-id,           search-disabledrepos,           subscription-manager\\nThis system is not registered to Ruby Hat Subscription Management.           Y'all can use subscription-manager to register.\\nResolving Dependencies\\northward-->           Running transaction bank check\\due north--->           Package demo-tomcat-ane.noarch 0:SNAPSHOT-i volition exist installed\\due north-->           Finished Dependency Resolution\\n          \\nDependencies           Resolved\\n\\north==================================================================\\n           Package Arch Version Repository                   Size\\n========================================================================           =====\\nInstalling:\\northward demo-tomcat-ane noarch SNAPSHOT-1 demo-repo1 7.1 K\\north\\nTransaction           Summary\\north=========================================================\\nInstall  1           Package\\north\\nTotal download size: 7.1 M\\nInstalled size: 7.9 One thousand\\nDownloading              packages:\\nRunning           transaction cheque\\nRunning transaction test\\nTransaction test succeeded\\nRunning              transaction\\n            Installing : demo-tomcat-1-SNAPSHOT-i.noarch ane/1 \\n  Verifying  :           demo-tomcat-1-SNAPSHOT-1.noarch          1/ane \\n\\nInstalled:\\n  demo-tomcat-1.noarch 0:SNAPSHOT-one  \\n\\nComplete!\\n'], u'rc': 0       }",        "Installed Tomcat artifacts - True"     ]  }    Task [install-tomcat : Make clean DEMO environs] **************************************** ************************************************************  task path: /users/demo/vivek-playbook/roles/install-tomcat/tasks/main.yml:xix  Tuesday 21 November 2017  13:02:13 +0530 (0:00:00.057) 0:00:08.387 ******  [Alarm]: when statements should non include jinja2 templating delimiters such equally {{ }} or     {% %}. Constitute: {{installationOutput.inverse}}    Using module file /usr/lib/python2.vii/sitepackages/ansible/modules/files/file.py  <localhost> Institute LOCAL Connexion FOR USER: root  <localhost> EXEC /bin/sh -c 'echo ~ && slumber 0'  <localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo     /root/.ansible/tmp/ansible-tmp-1511249534.thirteen-128345805983963 `" && repeat     ansible-tmp-1511249534.thirteen-128345805983963="` echo     /root/.ansible/tmp/ansibletmp-1511249534.thirteen-128345805983963 `" ) && sleep 0'  <localhost> PUT /tmp/tmp0aXel7 TO     /root/.ansible/tmp/ansible-tmp-1511249534.13128345805983963/file.py  <localhost> EXEC /bin/sh -c 'chmod u+x     /root/.ansible/tmp/ansible-tmp1511249534.13-128345805983963/     /root/.ansible/tmp/ansible-tmp-1511249534.13128345805983963/file.py && slumber 0'  <localhost> EXEC /bin/sh -c '/usr/bin/python     /root/.ansible/tmp/ansible-tmp1511249534.13-128345805983963/file.py; rm -rf     "/root/.ansible/tmp/ansible-tmp1511249534.13-128345805983963/" > /dev/null 2>&i     && sleep 0'  changed: [server1] => {     "inverse": true,        "diff": {           "afterward": {              "path": "/users/demo/DEMO",              "land": "absent-minded"        },        "before": {           "path": "/users/demo/DEMO",           "state": "directory"        }     },     "invocation": {        "module_args": {           "attributes": zippo,           "fill-in": nada,           "content": null,           "delimiter": null,           "diff_peek": null,           "directory_mode": nothing,           "follow": fake,           "force": false,           "group": null,           "style": zero,           "original_basename": null,           "owner": naught,           "path": "/users/demo/DEMO",           "recurse": false,           "regexp": zippo,           "remote_src": null,           "selevel": null,           "serole": null,           "setype": nil,           "seuser": nothing,           "src": null,           "land": "absent",           "unsafe_writes": null,           "validate": null        }     },     "path": "/users/demo/DEMO",     "state": "absent"  }    Chore [install-tomcat : debug] ******************************************************** *************************************************************  task path: /users/demo/vivek-playbook/roles/install-tomcat/tasks/master.yml:29  Tuesday 21 November 2017  13:02:fourteen +0530 (0:00:00.257)       0:00:08.645 ******  ok: [server1] => {    "changed": faux,     "msg": [        "Make clean DEMO environment task ended with message:{u'diff': {u'after': {u'path':           u'/users/demo/DEMO', u'state': u'absent'},        u'earlier': {u'path': u'/users/demo/DEMO', u'state': u'directory'}}, u'state': u'absent',           u'changed': True, u'path': u'/users/demo/DEMO'}",        "bank check value  :True"     ]  }    TASK [install-tomcat : Copy Tomcat to user home] ************************************* ********************************************************  chore path: /users/demo/vivek-playbook/roles/install-tomcat/tasks/main.yml:37  Tuesday 21 Nov 2017  13:02:xiv +0530 (0:00:00.055)       0:00:08.701 ******  [WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or     {% %}. Found: {{installationOutput.changed}}    Using module file /usr/lib/python2.7/sitepackages/ansible/modules/commands/command.py  <localhost> Plant LOCAL CONNECTION FOR USER: root  <localhost> EXEC /bin/sh -c 'echo ~ && slumber 0'  <localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo     /root/.ansible/tmp/ansible-tmp-1511249534.43-41077200718443 `" && repeat     ansibletmp-1511249534.43-41077200718443="` echo     /root/.ansible/tmp/ansible-tmp1511249534.43-41077200718443 `" ) && sleep 0'  <localhost> PUT /tmp/tmp25deWs TO     /root/.ansible/tmp/ansible-tmp-1511249534.4341077200718443/control.py  <localhost> EXEC /bin/sh -c 'chmod u+ten     /root/.ansible/tmp/ansible-tmp1511249534.43-41077200718443/     /root/.ansible/tmp/ansible-tmp-1511249534.4341077200718443/command.py && sleep 0'  <localhost> EXEC /bin/sh -c '/usr/bin/python     /root/.ansible/tmp/ansible-tmp1511249534.43-41077200718443/command.py; rm -rf     "/root/.ansible/tmp/ansibletmp-1511249534.43-41077200718443/" > /dev/null 2>&1     && sleep 0'  changed: [server1] => {     "changed": true,     "cmd": [        "cp",        "-r",        "/opt/ansible/tomcat/demo",        "/users/demo/DEMO/"     ],    "delta": "0:00:00.017923",     "finish": "2017-11-21 thirteen:02:xiv.547633",     "invocation": {        "module_args": {           "_raw_params": "cp -r /opt/ansible/tomcat/demo /users/demo/DEMO/",           "_uses_shell": false,           "chdir": nil,           "creates": nothing,           "executable": null,           "removes": null,           "warn": truthful        }     },     "rc": 0,     "kickoff": "2017-eleven-21 13:02:14.529710",     "stderr": "",     "stderr_lines": [],     "stdout": "",     "stdout_lines": []  }    TASK [install-tomcat : debug] ******************************************************** **********************************************************  job path: /users/demo/vivek-playbook/roles/install-tomcat/tasks/main.yml:47  Tuesday 21 Nov 2017  xiii:02:xiv +0530 (0:00:00.260)       0:00:08.961 ******  ok: [server1] => {     "changed": false,     "msg": "Re-create Tomcat to user home task ended with bulletin {       'stderr_lines': [], u'inverse': True, u'end': u'2017-xi-21 13:02:fourteen.547633', u'stdout':        u'', u'cmd': [u'cp', u'-r', u'/opt/ansible/tomcat/demo', u'/users/demo/DEMO/'], u'rc': 0,        u'first': u'2017-eleven-21 13:02:14.529710', u'stderr': u'', u'delta': u'0:00:00.017923',        'stdout_lines': []}"  }    TASK [beginning-tomcat : First Tomcat] ************************************************** **********************************************************  task path: /users/demo/vivek-playbook/roles/showtime-tomcat/tasks/main.yml:5  Tuesday 21 November 2017  13:02:14 +0530 (0:00:00.044)       0:00:09.006 ******  Using module file /usr/lib/python2.seven/sitepackages/ansible/modules/commands/command.py  <localhost> ESTABLISH LOCAL Connexion FOR USER: root  <localhost> EXEC /bin/sh -c 'repeat ~ && sleep 0'  <localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo     /root/.ansible/tmp/ansible-tmp-1511249534.63-46501211251197 `" && echo     ansibletmp-1511249534.63-46501211251197="` repeat     /root/.ansible/tmp/ansible-tmp1511249534.63-46501211251197 `" ) && sleep 0'  <localhost> PUT /tmp/tmp9f06MQ TO     /root/.ansible/tmp/ansible-tmp-1511249534.6346501211251197/command.py  <localhost> EXEC /bin/sh -c 'chmod u+x     /root/.ansible/tmp/ansible-tmp1511249534.63-46501211251197/     /root/.ansible/tmp/ansible-tmp-1511249534.6346501211251197/command.py && slumber 0'  <localhost> EXEC /bin/sh -c '/usr/bin/python     /root/.ansible/tmp/ansible-tmp1511249534.63-46501211251197/command.py; rm -rf     "/root/.ansible/tmp/ansibletmp-1511249534.63-46501211251197/" > /dev/null ii>&1     && sleep 0'  changed: [server1] => {     "changed": truthful,     "cmd": [ "/users/demo/DEMO/bin/startup.sh" ],     "delta": "0:00:00.020024",     "end": "2017-11-21 thirteen:02:14.741649",     "invocation": {        "module_args": {           "_raw_params": "/users/demo/DEMO/bin/startup.sh",           "_uses_shell": false,           "chdir": null,           "creates": naught,           "executable": zilch,           "removes": naught,           "warn": true        }     },     "rc": 0,     "get-go": "2017-11-21 13:02:fourteen.721625",     "stderr": "",     "stderr_lines": [],     "stdout": "Tomcat started.",     "stdout_lines": [ "Tomcat started." ]  }    TASK [showtime-tomcat : debug] ************************************************* **********************************************************************  task path: /users/demo/vivek-playbook/roles/start-tomcat/tasks/main.yml:x  Tuesday 21 November 2017  13:02:14 +0530 (0:00:00.150)       0:00:09.156 ******  ok: [server1] => {     "changed": false,     "msg": [        "Offset Tomcat task concluded with message: {'          stderr_lines': [], u'changed': True, u'end': u'2017-11-21 13:02:xiv.741649', u'stdout':           u'Tomcat started.', u'cmd': [u'/users/demo/DEMO/bin/startup.sh'], u'rc': 0, u'get-go':           u'2017-11-21 13:02:14.721625', u'stderr': u'', u'delta': u'0:00:00.020024',           'stdout_lines': [u'Tomcat started.']}",        "Tomcat started - Truthful"     ]  }  META: ran handlers  META: ran handlers    PLAY RECAP *******************************************************************************  *********************************************************  server1  : ok = 9    changed = 4    unreachable = 0    failed = 0    Tuesday 21 November 2017  13:02:14 +0530 (0:00:00.042)       0:00:09.198 ******  ===============================================================================  install-tomcat : Install Tomcat artifacts ------------------------------- 6.76s  /users/demo/vivek-playbook/roles/install-tomcat/tasks/principal.yml:v --------------  Gathering Facts --------------------------------------------------------- 1.52s   ------------------------------------------------------------------------------  install-tomcat : Copy Tomcat to user dwelling house ------------------------------- 0.26s  /users/demo/vivek-playbook/roles/install-tomcat/tasks/main.yml:37 -------------   install-tomcat : Clean DEMO surroundings --------------------------------- 0.26s  /users/demo/vivek-playbook/roles/install-tomcat/tasks/main.yml:xix -------------   commencement-tomcat : Start Tomcat --------------------------------------------- 0.15s  /users/demo/vivek-playbook/roles/start-tomcat/tasks/main.yml:5 ----------------  install-tomcat : debug -------------------------------------------------- 0.06s  /users/demo/vivek-playbook/roles/install-tomcat/tasks/main.yml:11 -------------   install-tomcat : debug -------------------------------------------------- 0.06s  /users/demo/vivek-playbook/roles/install-tomcat/tasks/master.yml:29 -------------   install-tomcat : debug -------------------------------------------------- 0.04s  /users/demo/vivek-playbook/roles/install-tomcat/tasks/master.yml:47 -------------   beginning-tomcat : debug ---------------------------------------------------- 0.04s  /users/demo/vivek-playbook/roles/first-tomcat/tasks/chief.yml:10 ---------------        

Hit the post-obit URL and you lot will be directed to a page as shown below − http://ten.76.0.134:11677/HelloWorld/HelloWorld

Hello World

The deployed war merely has a servlet which displays "Hullo Earth". The detailed output shows the time taken past each and every task considering of the entry added in ansible.cfg file −

[defaults]  callback_whitelist = profile_tasks        

Useful Video Courses


DevOps Project: DevOps CI/CD Pipeline with Jenkins Ansible Docker Kubernetes on AWS

Video

Master Ansible ( Fastest Way Ever )

Video

Automation with Ansible Playbooks

Video

Ansible for Absolute Beginners - Hands-on - DevOps

Video

Ansible for an Absolute Beginner - Automation - DevOps

Video

Ansible - Getting Started

Video

billsonlearallings.blogspot.com

Source: https://www.tutorialspoint.com/ansible/ansible_roles.htm