When creating roles for your playbooks, what are some of the tips and tricks you are applying yourself to make your roles more robust or resilient?
How do you write your unit tests?
Unit testing is a great idea for roles. If you write and use roles, its important to incorporate testing. There's a lot out there about how best to "test" a role. I hope others will share what they do.
At a minimum, I use the "tests" directory of a role as a smoke test. So, my "tests" directory looks a lot like a playbook directory. I have an ansible.cfg file, an inventory file, a requirements.yml file, a roles directory (if the role has dependencies), a site.yml file, a group_vars and host_vars directory (if needed). This means that I can clone a role, modify the inventory file, and run the site.yml playbook. I try to make sure that my roles work on their own.
tbh, my main trick is to stay away from roles ... until you are very very comfortable with Ansible and it serves a clear goal to use a role/roles for your given situation. I see to many students on day 2 start asking about roles when they are not even ready to automate a simple httpd install.
And when you start thinking about some type of automated testing, work with TDD or Test Driven Development. Describe what it is you want to achieve, describe how you would prove you achieved said goal and then start writing your playbooks.
Personally, i first write on a piece of paper whatever i would have done in bash. Then create ad-hoc oneliners, compile them into plays and playbooks and then rewrite it into a role whenever i think it will serve a purpose when the playbooks are available as a role. By then the code is pretty stable.
Unit testing is a great idea for roles. If you write and use roles, its important to incorporate testing. There's a lot out there about how best to "test" a role. I hope others will share what they do.
At a minimum, I use the "tests" directory of a role as a smoke test. So, my "tests" directory looks a lot like a playbook directory. I have an ansible.cfg file, an inventory file, a requirements.yml file, a roles directory (if the role has dependencies), a site.yml file, a group_vars and host_vars directory (if needed). This means that I can clone a role, modify the inventory file, and run the site.yml playbook. I try to make sure that my roles work on their own.
Thanks Dan. Those are helpful tips.
I also been finding myself enjoying using the setup module beforehand to see which facts and variables the host gives me and the JSON structure, that helps me to better parse the output and get what I need.
Hi Razique! :)
Here are my top tips & tricks ;)
Amazing tips and feedback @littlebigfab. Point 10 is a great one. I also use it when I write multi-lines text.
Point 7 is a great reminder that I should get into the habit of doing it as well :)
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.