Technology Security Analyst
In Ansible, we face numerous circumstances where an undertaking is reliant upon an assistance, port, or framework's state. However, that state is relied upon to happen after some time because of an association reset, server reboot, execution time taken by a code, or time taken to begin assistance. So we want to sit tight for it to occur. Presently Ansible is so wealthy in its module treasure that it has a module named wait_for which empowers our playbook to hang tight for a state or errand culmination on remote objective machines. Utilizing this module we will trust that a condition will happen before going on with the next undertakings of our playbook.'
Ansible wait_for module can be utilized with or without a condition on remote objective hosts and it checks for that condition's state following a predefined time right away.
While utilizing module, one should keep in mind underneath focuses:
To work with the Ansible wait_for module, we should be aware of the satisfactory boundaries and their adequate choices. A portion of the boundaries have default values set, so this must likewise be noted.
Given beneath is a rundown of boundaries:
1. break: Default is 300 seconds. The greatest number of seconds to sit tight for. This shouldn't be utilized with different circumstances, since then it will fizzle.
2. state: Below are the adequate states:
3. rest: Default is 1 second. This is to rest between checks.
4. search_regex: To look at a string in document or attachment association.
5. port: Port number to check.
6. way: Path of a record which should be existing before proceeding.
7. msg: This is the message which will be printed instead of a normal error message in case of failures.
8. host: The IP or hostname to wait for.
9. exclude_hosts: The list of hosts, not to check when looking for active connections (TCP).
10. delay: Default is 0. This represents the number of seconds to wait for the next polling.
11. connect_timeout: Default is 5 seconds. This is to specify the time to wait for a connection to establish before closing and trying again.
12. active_connection_states: This is the list of connections (TCP) that can be counted as an active session. Default values are “TIME_WAIT”, “FIN_WAIT2, “FIN_WAIT1”, “ESTABLISHED”, “SYN_SENT”, “SYN_RECV”.
The unique return values by this module will enable us to know the actual execution happethat ned on remote target systems.
Given below are such unique return values:
1. elapsed: The seconds elapsed while waiting.
2. match_groupdict: This is the dictionary containing all the subgroup's names matched by the key given.
3. match_groups: Tuple containing all the subgroups of the match.
Given beneath are the models referenced:
Here we have one Ansible regulator hub named as ansible-regulator. As target hubs, we have two remote machines. The first machine is a Red Hat Enterprise Linux machine named host tone and the second machine is a Ubuntu machine named host-two. We will run our playbooks on the Ansible regulator machine and make changes on remote objective machines.
In this model, we have a playbook like beneath, utilizing which we will attempt to reboot the remote machines and the utilization wait_for module to sit tight for 30 seconds, and afterward, we look at the uptime on the same machines.
-name: Here we are rebooting the remote machines using Ansible reboot module reboot:
-name: Here we will wait for 30 seconds and then continue with the next wait_for:
-name: Here we are checking the uptime on the shell: uptime
-name: Here we are printing the output of debugging:
Now running this playbook like below, we have below output where we can see that servers were rebooted, and then we waited for 30 seconds, and then uptime was displayed.
ansible-playbook ansible_wait_for_reboot.YAML -v
In this model, we have a playbook underneath where we are checking for n httpd.conf record, if this document exists, alter this document. On the off chance that this document doesn't exist then, at that point, sit tight for it. For this, we have a playbook underneath.
-hosts: host-one tasks:
-name: Here we check for file httpd.conf
name: Here we are placing line in this file using lineinfile
path: /etc/httpd/conf/httpd.conf regexp: '^Listen '
insertafter: '^#Listen ' line: Listen 8443
Here, at first, we didn't have the httpd bundle introduced on having one, so the playbook was hanging tight for the httpd.conf document, as this record is given by the httpd bundle. Then, at that point, while it was sitting tight for httpd to be introduced and a document to be made, we went to have one and introduced the httpd bundle in the foundation. Presently in yield, you can see the slipped by time is 77 seconds which shows it sat tight for 77 seconds.
ansible-playbook ansible_wait_for_file.yaml -v
As we found in this article, this module is truly useful in commonsense or constant situations, where you truly need to sit tight for quite a while with the goal that a condition can finish on remote objective frameworks. Tasks like fixing, provisioning use it well indeed. In any case, as you probably are aware we will be exceptionally mindful with regards to its accessible choices before utilizing it. So learn it first and afterward use it.
Subscribe to get latest updates