Last active
July 29, 2022 14:08
-
-
Save dmccuk/38e1e139a225754993e52448a47ce27e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let an Ansible role from scratch and do the following things, and then deploy the role. | |
| * Create the role | |
| * Setup the tasks using best practice | |
| * Add templates with ansible facts | |
| * Setup handlers for when things change | |
| * Make the whole thing idempotent and not task run unless it needs to | |
| * Show how variable precedence works for default, vars & group_vars | |
| All the code for this demo is available below. Don't forget to like and subscribe! | |
| ** NOTE ** | |
| These code samples below are the finished code. As part of the demo, I will be building these up step by step. ** | |
| ** | |
| 1) Create the role. | |
| ```` | |
| mkdir roles | |
| cd roles | |
| ansible-galaxy init webserver | |
| tree webserver | |
| webserver/ | |
| ├── defaults | |
| │ └── main.yml | |
| ├── files | |
| ├── handlers | |
| │ └── main.yml | |
| ├── meta | |
| │ └── main.yml | |
| ├── README.md | |
| ├── tasks | |
| │ └── main.yml | |
| ├── templates | |
| ├── tests | |
| │ ├── inventory | |
| │ └── test.yml | |
| └── vars | |
| └── main.yml | |
| ```` | |
| 2) Lets setup out tasks using best practice. Best practice is to move the tasks out of the main.yml, and into their own tasks. | |
| For example, lets use and install.yml to only install and start the service, then have a configure.yml to only contain tasks | |
| to configure the application. | |
| ```` | |
| vi webserver/tasks/main.yml | |
| --- | |
| - name: include role tasks | |
| include_tasks: "{{ webserver_tasks }}" | |
| loop: | |
| - install.yml | |
| - configure.yml | |
| loop_control: | |
| loop_var: webserver_tasks | |
| vi webserver/tasks/install.yml | |
| - name: install the latest version of Apache | |
| yum: | |
| name: httpd | |
| state: latest | |
| - name: start the service | |
| service: | |
| name: httpd | |
| state: started | |
| - name: add rule to FW | |
| firewalld: | |
| service: http | |
| permanent: true | |
| state: enabled | |
| notify: restart firewalld | |
| ```` | |
| To be able to run the role, it's good to have an ansible.cfg file, a hosts.ini file and a deployment file: | |
| ```` | |
| vi ansible.cfg | |
| [defaults] | |
| inventory=hosts.ini | |
| vi hosts.ini | |
| [webserver] | |
| 172.31.17.134 | |
| [webserver:vars] | |
| ansible_user=ec2-user | |
| ansible_ssh_private_key_file=~/.ssh/working.pem | |
| vi deploy.yml | |
| --- | |
| - name: deploy a webserver | |
| hosts: webserver | |
| become: true | |
| roles: | |
| - webserver | |
| ```` | |
| 3) Add the template to create the webpage | |
| ```` | |
| - name: create webpage | |
| template: | |
| src: templates/index.html.j2 | |
| dest: /var/www/html/index.html | |
| notify: restart httpd | |
| vi webserver/templates/index.html.j2 | |
| <!doctype html> | |
| <title>LondonIAC meetup - Site Maintenance</title> | |
| <style> | |
| body { text-align: center; padding: 150px; } | |
| h1 { font-size: 50px; } | |
| body { font: 20px Helvetica, sans-serif; color: #333; } | |
| article { display: block; text-align: left; width: 650px; margin: 0 auto; } | |
| a { color: #dc8100; text-decoration: none; } | |
| a:hover { color: #333; text-decoration: none; } | |
| </style> | |
| <article> | |
| <h1>We’ll be back soon!</h1> | |
| <div> | |
| <p>Sorry for the inconvenience but we’re performing some maintenance at the moment. If you need to you can always <a href="mailto:#">contact us</a>, otherwise we’ll be back online shortly!</p> | |
| <p> Message brought to you by: {{ ansible_hostname }} and variable: {{ variable }}</p> | |
| <img src="https://marcelorjava.files.wordpress.com/2014/04/dilbert.gif" alt="Dilbert"> | |
| <p>— The Team</p> | |
| </div> | |
| <p> | |
| </p> | |
| </article> | |
| ```` | |
| 4) Lets setup the handlers so we can restart services when something changes | |
| ```` | |
| vi webserver/handlers/main.yml | |
| --- | |
| - name: restart httpd | |
| service: | |
| name: firewalld | |
| state: restarted | |
| - name: restart httpd | |
| service: | |
| name: httpd | |
| state: restarted | |
| ```` | |
| 5) Lets add a variable to our code so we can follow how variable prececdence works. | |
| ```` | |
| vi webserver/defaults/main.yml | |
| --- | |
| variable: one | |
| ```` | |
| Update the index.html template and add a veriable so we can see the output change. | |
| Now run ansible to see it add the variable to the webpage. | |
| Now add a new variable to vars. | |
| ```` | |
| vi webserver/vars/main.yml | |
| --- | |
| variable: two | |
| ```` | |
| Re-run ansible. Does the value change? | |
| Now lets setup group_vars and create a host variable file. | |
| ```` | |
| mkdir group_vars | |
| vi group_vars/webserver.yml | |
| --- | |
| variable: three | |
| ```` | |
| Re-run ansible. What do you think will happen? | |
| As you can see form the output, playbook variables (in vars) beat the group_vars. So lets hash out the vars variable and re-run: | |
| ```` | |
| vi webserver/vars/main.yml | |
| --- | |
| #variable: two | |
| ```` | |
| Now when we re-run ansible, our host variable is picked up. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment