In this tutorial, we will learn about creating an Ansible playbook to block and unblock IP and we will also learn about some of the important module which helps to create a playbook to block or unblock an IP in the blockips.conf file which is located inside following location
→ /etc/nginx/conf.d/blockips.conf
Ansible playbook to block and unblock IP
In Nginx we manage a file with name of blockips.conf where we add an IP to block the access and we can remove an IP if it is added by mistake and to do this process we add a lots of manual efforts and to avoid this we have created a ansible playbook to automate this process.
Output:
In this output we can see a list of an IP’s that are blocked and I want to unblock the 192.10.11.10 IP and to do this I will trigger the ansible playbook and explain them.
About Playbook Tasks:
In the below tasks we are using some Ansible modules which are performing some different operation
- lineinfile: Manages lines in text files.
- shell: It executes the shell command on a target
In the below playbook we are using the tags by which we can skips un-required tasks for our operation that we want to perform.
Here are the below commands that we trigger into our ansible server to remove and add IP to any host which is managed in our hosts file.
Command:
To Remove IP
ansible-playbook block-unblock.yml -e "host=192.168.64.10 ip=192.10.11.10" --tag remove
To Add IP
ansible-playbook block-unblock.yml -e "host=192.168.64.10 ip=192.10.11.10" --tag add
block-unblock.yml
#ansible-playbook-to-block-and-unblock-ip
---
- name: Playbook for block and ublock ip
hosts: "{{ host }}"
vars:
time: "{{ lookup('pipe', 'date +%Y%m%d-%H-%M') }}"
become: yes
tasks:
- name: "Add IP "
tags: add
lineinfile:
# path to add the ip to block
path: /etc/nginx/conf.d/blockips.conf
line: deny {{ ip }};
state: present
- name: verify syntax
shell: nginx -t
tags: add
register: syntax_output
- name: debuging syntax
tags: add
debug:
var: syntax_output
- name: "restart nginx"
tags: add
service: name=nginx state=restarted
when: syntax_output.stderr is search('syntax is ok')
#remove ip stage
- name: "remove IP"
tags: remove
lineinfile:
# path to remove the ip
path: /etc/nginx/conf.d/blockips.conf
# String to Search
regexp: deny {{ ip }};
# State is set to Absent to remove if the Searching Line is found
state: absent
- name: verify syntax
shell: nginx -t
tags: remove
register: syntax_output
- name: debuging syntax
tags: remove
debug:
var: syntax_output
- name: "restart nginx"
tags: remove
service: name=nginx state=restarted
when: syntax_output.stderr is search('syntax is ok')
After triggering the above playbook we get the following output where it shows that it passes all the stages and the IP that we want to remove is removed from the required host.
Output:
In the above as we discussed we will be removing an IP (192.10.11.10) from the blockips.conf file and by running the below command we made this possible and to view the output please find the below results.We can also setup this using Jenkins or any other CI/CD tools through which helps a user to not login to server again and again and to add or remove an IP manually.
Command:
ansible-playbook block-unblock.yml -e "host=192.168.64.10 ip=192.10.11.10" --tag remove
Now as we removed an IP from the blockips.conf now we if want to add back to blockips.conf we can do this by running the below command which will add an IP back to block list on a required host.
Command:
ansible-playbook block-unblock.yml -e "host=192.168.64.10 ip=192.10.11.10" --tag add
GitHub Link
We can also check the different project codes on pythontpoint Github page to do this you guys can look for us with the following GitHub Username.
GitHub User Name: PythonT-Point
So, in this tutorial, we have learned to create an Ansible playbook to block and unblock IP and we have also discussed the whole code used in this tutorial.
Do follow the following tutorials also:
Hey there would you mind sharing which blog platform you’re working with? I’m going to start my own blog in the near future but I’m having a hard time making a decision between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design seems different then most blogs and I’m looking for something completely unique. P.S My apologies for being off-topic but I had to ask!
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
Howdy! Would you mind if I share your blog with my facebook group? There’s a lot of people that I think would really enjoy your content. Please let me know. Thanks
I like this web blog its a master peace ! Glad I detected this on google .
Hi there! Quick question that’s completely off topic. Do you know how to make your site mobile friendly? My web site looks weird when viewing from my iphone4. I’m trying to find a theme or plugin that might be able to resolve this problem. If you have any recommendations, please share. Thanks!
I truly appreciate this post. I¦ve been looking all over for this! Thank goodness I found it on Bing. You have made my day! Thank you again
F*ckin’ amazing things here. I’m very satisfied to look your article. Thanks a lot and i’m looking ahead to touch you. Will you kindly drop me a mail?
I went over this web site and I believe you have a lot of great information, bookmarked (:.
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
I was suggested this web site by my cousin Im not sure whether this post is written by him as no one else know such detailed about my trouble You are incredible Thanks
You completed various good points there. I did a search on the subject matter and found a good number of people will have the same opinion with your blog.
I do trust all the ideas youve presented in your post They are really convincing and will definitely work Nonetheless the posts are too short for newbies May just you please lengthen them a bit from next time Thank you for the post.
Thanks for sharing. I read many of your blog posts, cool, your blog is very good.
Ny weekly I just like the helpful information you provide in your articles
La weekly Good post! We will be linking to this particularly great post on our site. Keep up the great writing
Real Estate I just like the helpful information you provide in your articles
Real Estate I very delighted to find this internet site on bing, just what I was searching for as well saved to fav
Your article helped me a lot, is there any more related content? Thanks! https://accounts.binance.com/uk-UA/register-person?ref=W0BCQMF1
Baddiehubs naturally like your web site however you need to take a look at the spelling on several of your posts. A number of them are rife with spelling problems and I find it very bothersome to tell the truth on the other hand I will surely come again again.
Nutra Gears I am truly thankful to the owner of this web site who has shared this fantastic piece of writing at at this place.
Nutra Gears I do not even understand how I ended up here, but I assumed this publish used to be great
Very interesting info !Perfect just what I was looking for! “The whole point of getting things done is knowing what to leave undone.” by Lady Reading.
Definitely, what a fantastic site and educative posts, I surely will bookmark your site.Best Regards!
I was looking at some of your content on this website and I believe this web site is very instructive! Keep posting.
My brother recommended I would possibly like this website. He was totally right. This put up truly made my day. You can not believe simply how much time I had spent for this info! Thank you!
ссылка на интересную статью, рекомендую ознакомиться, перейти на сайт, на русском языке ilskxpshlq … https://women.relevantsearchmedia.biz/wp-content/uploads/2024/kazino-top-listing-best.html
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me. https://accounts.binance.com/hu/register-person?ref=FIHEGIZ8
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
Hi! This is kind of off topic but I need some guidance from an established blog. Is it very hard to set up your own blog? I’m not very techincal but I can figure things out pretty fast. I’m thinking about setting up my own but I’m not sure where to begin. Do you have any points or suggestions? Thanks
Yeah bookmaking this wasn’t a speculative conclusion outstanding post! .
It’s exhausting to find knowledgeable individuals on this matter, however you sound like you recognize what you’re talking about! Thanks
dodb buzz naturally like your web site however you need to take a look at the spelling on several of your posts. A number of them are rife with spelling problems and I find it very bothersome to tell the truth on the other hand I will surely come again again.
Masalqseen This was beautiful Admin. Thank you for your reflections.
Hello my family member! I wish to say that this post is awesome, great written and include approximately all important infos. I’d like to see more posts like this .
Smartcric This is my first time pay a quick visit at here and i am really happy to read everthing at one place
Blue Techker very informative articles or reviews at this time.
I simply could not depart your website before suggesting that I actually enjoyed the standard information an individual supply for your visitors? Is going to be back frequently in order to inspect new posts
I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.