Working with Ansible {Part-10}

Working with Ansible {Part-10}

Special Cases in Ansible. There are many special cases in the Ansible Playbook and we will try to study them.

  1. ignore_errors: The Ansible Playbook works in Top to down approach and in case some task fails then it exists the playbook and does not run the other tasks as well. So to overcome this we use ignore_errors. How do we use it let's see in the below YAML.

     ---
     - name: working with special cases
       hosts: Node-1
       tasks:
           - name: working with script
             script: ./myscript.sh
           - name: printing my data
             command: echo "This is how ignore_case works"
    

    Now, I have not used ignore_errors Let's execute and see if it can print the command. Ideally, it should not

    Now, by introducing ignore_errors in our playbook the execution won't stop it'll keep going.

     ---
     - name: working with special cases
       hosts: Node-1
       tasks:
           - name: working with script
             script: ./myscript.sh
             ignore_errors: true
           - name: printing my data
             command: echo "This is how ignore_case works"
    

    Let's execute our playbook now.

    Hence, we saw that ignore_case can help us in ignoring in Ansible-Playbook.

  2. delagate_to: By default, Ansible does the parallel execution. What does it mean that let's say I have 3 Nodes N1, N2, N3 and 3 Tasks T1, T2, T3 ansible will pick the T1 task and will try to execute on Node N1, N2, and N3 first and then will pick task T2 and execute on N1, N2 and N3 Node similarly with task T3.....

    Now we require to run Task T2 only on the N2 node. To achieve this we use delegate_to. We can delegate the task to a server or group of servers.

     ---
     - name: working with special cases
       hosts: all
       tasks:
           - name: gather server ipaddress & name
             shell: "uptime; uname -a"
    
           - name: gather ipaddress
             shell: "hostname -i"
             delegate_to: "Node-1"
    

    Now, see that The IP of Node-2 is 172.31.26.250 but as we have used the delegate_to Node-1 it is delegating the IP to Node-1 only for Node-2 as well (in the highlighted Area)

  3. serial: By default, Ansible works in Parallel mode. But if we use it in Parallel mode it can choke the whole network. So it is always recommended to run Ansible in Serial Mode. Mainly when we have 100 or 1000 servers to do patching or to roll out something. Let's see how we use the serial in Ansible Playbooks.

     ---
     - name: working with special cases
       serial: 1
       hosts: all
       tasks:
           - name: gather server ipaddress & name
             shell: "uptime; uname -a"
    
           - name: gather ipaddress
             shell: "hostname -i"
    

Now , in the Above Screenshot we can see that it is working on Node-2 first then it is coming to Node-1 because of Serial we used in our playbook. Kindly, Check the 007,008,009 YAML

Did you find this article valuable?

Support Chirag Kukreja by becoming a sponsor. Any amount is appreciated!