Ansible Template Module Tutorial - Complete Beginner's Guide

  Рет қаралды 32,539

Percy Grunwald from TopTechSkills

Percy Grunwald from TopTechSkills

Күн бұрын

Пікірлер: 77
@toptechskills
@toptechskills 5 ай бұрын
G'day everyone, if you're currently learning Ansible and want to see how all these modules fit together to build something useful, please consider checking out my course and letting me know what you think! It's currently in "early access" while I finish off the last chapter, and the first part of the course is free so you can try it out: learn.toptechskills.com/courses/productive-with-ansible. Thanks for all your support and happy coding!
@sf2998
@sf2998 3 жыл бұрын
man u have the best ansible tutorials. you should try to create videos to cover every topic of ansible
@toptechskills
@toptechskills 3 жыл бұрын
Thanks a lot for this comment, it means a lot to me 🙏
@yogienathan8057
@yogienathan8057 4 жыл бұрын
Super useful and comprehensible. Good work Percy. Glad i found this video.
@toptechskills
@toptechskills 4 жыл бұрын
Thanks for your kind comment yogie nathan, very happy that you found this video useful. Good luck with Ansible!
@arunkarnad4522
@arunkarnad4522 4 жыл бұрын
Small crisp and informative video
@LuisMiguelZapata
@LuisMiguelZapata 4 жыл бұрын
Amazing thank you. Excellent work... smooth and straight to the point. I hope to see more vídeos like this in your channel.
@toptechskills
@toptechskills 4 жыл бұрын
Thank you so much for your kind comment Luis 🙏
@sukeshnambiar3868
@sukeshnambiar3868 2 жыл бұрын
Thank you so much. Very helpful.
@toptechskills
@toptechskills 2 жыл бұрын
You're welcome, thank you for your comment
@flymoracer
@flymoracer 2 жыл бұрын
Great content, thank you. I have a use case that's slightly more complex, wondering if the template module can help. I'd like to manage the content within a specific section of a configuration file on my Linux servers. Is it possible to search for a specific location within an existing text file and inject a few lines?
@toptechskills
@toptechskills Жыл бұрын
The blockinfile module should be what you need: docs.ansible.com/ansible/latest/collections/ansible/builtin/blockinfile_module.html#ansible-collections-ansible-builtin-blockinfile-module
@karthickbaskar6886
@karthickbaskar6886 5 жыл бұрын
Superb explanation, easily understandable and very clear in teaching. Keep it up.
@toptechskills
@toptechskills 5 жыл бұрын
Thank you for all your comments, Karthick 🙏
@alabalistic
@alabalistic 3 жыл бұрын
Hi, great video quality. June 2021 I try to follow your steps, on Ubuntu 20.04, and I had fail. After changing ansible_user to ansible_user_id. I achieved the goal
@toptechskills
@toptechskills 3 жыл бұрын
Thank you for the update! I hope this is useful for others who try to follow the video.
@umangverma4557
@umangverma4557 5 жыл бұрын
Thanks, Thats a good start Intro to Jinja2
@toptechskills
@toptechskills 5 жыл бұрын
Thanks for your comment Umang, I'm happy it was a good intro for you.
@amichubby1
@amichubby1 2 жыл бұрын
Wonderful video. Thanks bro..
@toptechskills
@toptechskills 2 жыл бұрын
You're welcome, thanks a lot for your comment
@sivareddy9534
@sivareddy9534 2 жыл бұрын
Thanks for the video! I have one question on what is difference between blockinfile and template in Ansible ? I have parameters defined in Jenkins to store some user information from Jenkins and store in meta file - Is there any better way to capture data from user in Ansible other than block file?? Thanks
@toptechskills
@toptechskills 2 жыл бұрын
`blockinfile` "adds" or "updates" a block of text in a file, whereas `template` completely replaces a file with the templated file. Personally I think it's better to use `template` in all cases, since it's easier to reason about (you can know what the file contents will be by looking at the source code). > Is there any better way to capture data from user in Ansible other than block file?? I'm not sure what you mean by "capture" here, can you clarify?
@RA_I_AM
@RA_I_AM 2 жыл бұрын
Clear and to the point
@ramakrishna-pt8ne
@ramakrishna-pt8ne 5 жыл бұрын
Straight to the point ... Thank you!!!!
@Raj-si5yz
@Raj-si5yz 4 жыл бұрын
Thank you so much,could you give idea how to create Dev,stage and production enviorment?
@toptechskills
@toptechskills 4 жыл бұрын
Hi Raj, the best way of handling multiple environments is to use groups and then apply different variables to each env using Ansible's `group_vars`. Your roles and playbooks should be the same for all envs, only the values of the variables should change. Here's a good guide from the Ansible docs: docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html#how-to-differentiate-staging-vs-production Here's an excellent guide from DigitalOcean on a similar topic: www.digitalocean.com/community/tutorials/how-to-manage-multistage-environments-with-ansible Hope this helps for now
@vinnyn2249
@vinnyn2249 4 жыл бұрын
Thanks for the video! Amazing content! I've a question - not related to template module. I've a use case of using Ansible to call a REST Api render it in JSON and then extract data from one much needed object out of the data rendered. Just getting started with Ansible. I was thinking I could use the shell module to curl the api in JSON and extract that one object from JSON using JSON parsor. What do you think? Your comment would be appreciated.
@toptechskills
@toptechskills 4 жыл бұрын
Hi Vinny, thank you for your comment. I think the `uri` module would be best for your use case: docs.ansible.com/ansible/latest/modules/uri_module.html#uri-module. You will probably need to make use of the `return_content` option, which can parse the JSON response automatically. Here's a snippet to get you started: ``` - name: Make a JSON REST request uri: url: your.jira.example.com/rest/api/2/issue/ method: POST user: your_username password: your_pass body: "{{ lookup('file','issue.json') }}" force_basic_auth: yes status_code: 201 body_format: json return_content: yes register: uri_result - name: Print uri_result debug: var: uri_result ``` Hope that helps, good luck!
@Mrnw10
@Mrnw10 3 жыл бұрын
Awesome stuff Percy .. new subscriber incoming!
@toptechskills
@toptechskills 3 жыл бұрын
Thank you very much 🙏
@Zak0nline
@Zak0nline 4 жыл бұрын
very very good tutorial. thanks for sharing it with us. xD
@toptechskills
@toptechskills 4 жыл бұрын
Thank you Ben 🙏
@ketanthombare9392
@ketanthombare9392 11 ай бұрын
really really usefull tutorial
@tsivanathan
@tsivanathan 3 жыл бұрын
perfect and thanks a lot!
@toptechskills
@toptechskills 3 жыл бұрын
You're welcome, thank you for your comment 🙏
@yashasvidevang1533
@yashasvidevang1533 3 жыл бұрын
Could you please let me know about PsExec module I tried with some .exe file. But the playbook sticks in the middle. Will not proceed further at all. I waited for 30+ mins. Still the same
@toptechskills
@toptechskills 3 жыл бұрын
Sorry, I haven't worked with the PsExec module so I can't provide any assistance here. Maybe someone else can help in the comments.
@GGoblin
@GGoblin 4 жыл бұрын
If you have the time. I get one error stating: "fatal: [dnsserver]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'dnsserver' is undefined"}" my dnsserver is the variable of my machine. The weird part is that the rest of my playbook is running smoothly (the playbook itself doesn't contain vars either). Now i did this for replacing an nginx configuration file. I'm not including the code because it's almost identical to yours. Do you know where I should look for this problem. Never engaged this before. Thanks beforehand and great video :-)
@toptechskills
@toptechskills 4 жыл бұрын
Hi G Goblin, happy to help, do you have a code snippet or something I can look at? Where have you defined the variable `dnsserver`, and where is it used?
@GGoblin
@GGoblin 4 жыл бұрын
@@toptechskills Well 'dnsserver' is a host machine which the my 'ansibleController' is pushing configuration onto. This is all done with vagrant with multiple vm's. For now I'm only defining dnsserver (no dns-sofware yet). my hosts (defined in hosts.yml): #code snippet from hosts (/etc/ansible/hosts) [servers] dnsserver ansible_host=192.172.1.3 ansible_port=22 ## [webservers] ## alpha.example.org ## beta.example.org ## 192.168.1.100 ## 192.168.1.110 the section in the playbook that goes wrong (defined in /etc/ansible/hosts.yml): - name: kopier nginx config file template: src: nginx.conf.j2 dest: /etc/nignx/nginx.conf the complete error message: fatal: [dnsserver]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'dnsserver' is undefined"} Like I said the other plays in the playbook run smoothly and they are no variables declared. thanks in advance!
@GGoblin
@GGoblin 4 жыл бұрын
@@toptechskills I also see I get the same error when i try to copy files to the destination machine with the ansible copy keyword.
@GGoblin
@GGoblin 4 жыл бұрын
@@toptechskills If found the error :( apparantly i used an undefined variable in my template file itself so i guess I'll fix it from here. thanks for the reponse though!
@toptechskills
@toptechskills 4 жыл бұрын
Yeah, if your `dnsserver` is just the name of one of the hosts, it won't be defined as a variable anywhere, but you can use the name to access variables about it by using `hostvars`. If you want to access variables defined on `dnsserver` you can use `hostvars['dnsserver'][...]`. You can check what's available for that host like this: ``` - name: print host vars for dnsserver debug: var=hostvars['dnsserver'] ``` Hope that helps, good luck!
@mandarsalunkhe8949
@mandarsalunkhe8949 4 жыл бұрын
can you use regex under the loop keyword for multiple files? in your case - my_app* (something like this) ? or the loop keyword does not allow that?
@toptechskills
@toptechskills 4 жыл бұрын
Hi Mandar, you might be looking for something like the `fileglob` plugin: docs.ansible.com/ansible/latest/plugins/lookup/fileglob.html. Let me know if this works for your use case. Thanks for your comment.
@mandarsalunkhe8949
@mandarsalunkhe8949 4 жыл бұрын
Yes that's what I was looking for, thanks!
@JackReacher1
@JackReacher1 3 жыл бұрын
so basically using jinja templates have use only if you have different types of host otherwise it is of no use as one can just copy the conf files. does sourcing a conf file in the terminal containing password considered a bad security practice or is it alright in production?
@toptechskills
@toptechskills 3 жыл бұрын
That's right, if there is no variation between any of your hosts then there is no need for using variables/templates and using copy to install a static file is fine. Regarding the sourcing of the conf file, I would say it really depends on the context. It's normal for production hosts to contain files that have passwords inside them (e.g. config files for a web application to access a database using a password). What is this password for? Why are you sourcing it to a shell rather than storing it in a config file?
@JackReacher1
@JackReacher1 3 жыл бұрын
@@toptechskills I was thinking of doing this - 1. source db.cfg 2. mysql -u root -p $DBPASSWORD -h $DBHOST $DBNAME < dump.sql (sadly the second command gives me an error if I run it more than once) and obviously a warning. Is this a good idea? or Should I be importing the db.cfg file in the ansible playbook and use the mysql_db module to import the dump file?
@toptechskills
@toptechskills 3 жыл бұрын
Using the `mysql_db` module will allow you to set the variables from Ansible Vault from encrypted files on your control machine: docs.ansible.com/ansible/latest/user_guide/vault.html. If you want to use a file on the target machine, it's better to use an options file rather than sourcing shell variables: dev.mysql.com/doc/refman/8.0/en/option-files.html. You can template this file onto the target host with the `template` module and get variables from Ansible Vault, which I mentioned above. Hope that helps!
@cleitonsandestorres1710
@cleitonsandestorres1710 2 жыл бұрын
great job ;-) congrats
@toptechskills
@toptechskills 2 жыл бұрын
Thank you!
@Triviaquizwitharya
@Triviaquizwitharya 3 жыл бұрын
Good Explanation
@toptechskills
@toptechskills 3 жыл бұрын
Thank you! 🙏
@Triviaquizwitharya
@Triviaquizwitharya 3 жыл бұрын
@@toptechskills can you please make an elaborate video on ansible roles
@avasivannarayana2583
@avasivannarayana2583 5 жыл бұрын
in the .yml you just mentioned env: staging but in .j2 file there are three vars env, ansible_host and ansible_user where did you defined these all variables?
@toptechskills
@toptechskills 5 жыл бұрын
Hi Ava, thanks for your comment. `ansible_host` and `ansible_user` are "connection variables" that are available for connection types like SSH. If you're connecting to a host via SSH, these variables should be defined in all cases. Here's a link to the docs with more information about connection variables: docs.ansible.com/ansible/latest/reference_appendices/special_variables.html#connection-variables
@umairksa2011
@umairksa2011 5 жыл бұрын
Which tool you are using to write code?
@toptechskills
@toptechskills 5 жыл бұрын
Hi Őɱɨ Ƥȅɳǵȗɩɳ, I'm using VS Code: code.visualstudio.com
@AmolGautam
@AmolGautam 3 жыл бұрын
Thank you..
@toptechskills
@toptechskills 3 жыл бұрын
👍🙏
@ahsan-li7sh
@ahsan-li7sh 5 жыл бұрын
Great video
@toptechskills
@toptechskills 5 жыл бұрын
Thanks again for watching and commenting Ahsan! Glad you enjoyed it
@ahsan-li7sh
@ahsan-li7sh 5 жыл бұрын
@@toptechskills just few ideas l, it would be also nice if you have time to make videos about how to deploy website for example WordPress install, update and upgrade in docker container, deploying database using ansible, logging and monitor solutions with ansible videos. I know those are much work but I just wanted to share my idea
@ahsan-li7sh
@ahsan-li7sh 5 жыл бұрын
I mean some project based tutorial
@toptechskills
@toptechskills 5 жыл бұрын
@@ahsan-li7sh these are great ideas. I want to make some project based videos once I finish a few more module videos. My first idea was how to install Elasticsearch using Ansible, which is a pretty simple role. I really like the projects you suggested, I will try to make some of those ASAP. Thanks for the suggestions!
@ahsan-li7sh
@ahsan-li7sh 5 жыл бұрын
@@toptechskills looking forward to that
@RitamSur
@RitamSur 2 жыл бұрын
From where the value is coming for local_ip ?
@toptechskills
@toptechskills 2 жыл бұрын
The value comes from the "ansible_host", which is a "special connection variable" available for some connection plugins: docs.ansible.com/ansible/latest/reference_appendices/special_variables.html#connection-variables. That page lists some other special variables. Host-specific variables might also come from "facts", which get set if "gather_facts" is true for a play: docs.ansible.com/ansible/latest/user_guide/playbooks_vars_facts.html Does that help?
@thamkinathkounain3901
@thamkinathkounain3901 4 жыл бұрын
Hi..Could you create one on how to sudo to root or some other user .. Im getting Sorry, user test is not allowed to execute '/bin/sh -c echo BECOME-SUCCESS-*** /usr/bin/python /home/test/.ansible/tmp/ansible-tmp-1597731916.1-91350856264363/command.py' as root on server.
@toptechskills
@toptechskills 4 жыл бұрын
Hi Thamkinath Kounain, there are some more details about this here: docs.ansible.com/ansible/latest/user_guide/become.html#id1 Hope this is helpful.
@KHUSHI5969
@KHUSHI5969 2 жыл бұрын
i tired to use in dest: {{ ssh_config_file }} , though no errors, it does not copy file defaults/main.yml as ssh_config_file: /etc/ssh/ssh_config templates/ssh_config.j2 file exists
@toptechskills
@toptechskills 2 жыл бұрын
Make sure to put quotes around template strings. Usually there would be an error message about that. If quotes around the var like "{{ ... }}" doesn't solve the problem, please post the full playbook and I'll help further.
Ansible Advanced Template Loops Tutorial - Use Loop Vars in Your Templates
7:49
Percy Grunwald from TopTechSkills
Рет қаралды 27 М.
Ansible Command Module Tutorial - Complete Beginner's Guide
10:11
Percy Grunwald from TopTechSkills
Рет қаралды 13 М.
MAGIC TIME ​⁠@Whoispelagheya
00:28
MasomkaMagic
Рет қаралды 36 МЛН
Friends make memories together part 2  | Trà Đặng #short #bestfriend #bff #tiktok
00:18
Wait for the last one 🤣🤣 #shorts #minecraft
00:28
Cosmo Guy
Рет қаралды 21 МЛН
Ansible for Network Configuration Templates
21:02
David Mahler
Рет қаралды 59 М.
Ansible Copy Module Tutorial - Complete Beginner's Guide
10:42
Percy Grunwald from TopTechSkills
Рет қаралды 17 М.
Ansible Best Practices  Roles and Modules
1:04:24
Red Hat Ansible Automation
Рет қаралды 22 М.
Ansible Fetch Module Tutorial - Complete Beginner's Guide
9:17
Percy Grunwald from TopTechSkills
Рет қаралды 12 М.
Ansible Roles - Understanding Ansible Roles
28:57
networknutsdotnet
Рет қаралды 103 М.
Ansible File Module Tutorial - Complete Beginner's Guide
15:20
Percy Grunwald from TopTechSkills
Рет қаралды 28 М.
What is Ansible | Ansible Playbook explained | Ansible Tutorial for Beginners
16:41
Ansible Basics 07: Templates and Jinja2
10:23
Tom's IT Cafe
Рет қаралды 291
Ansible Shell Module Tutorial - Complete Beginner's Guide
7:50
Percy Grunwald from TopTechSkills
Рет қаралды 24 М.