Ansible Facts Gathering

Ticker

6/recent/ticker-posts

Ansible Facts Gathering

Question: The Nautilus DevOps team is trying to setup a simple Apache web server on all app servers in Stratos DC using Ansible. They also want to create a sample html page for now with some app specific data on it. Below you can find more details about the task.

You will find a valid inventory file /home/thor/playbooks/inventory on jump host (which we are using as an Ansible controller).

Create a playbook index.yml under /home/thor/playbooks directory on jump host. Using blockinfile Ansible module create a file facts.txt under /root directory on all app servers and add the following given block in it. You will need to enable fact gathering for this task.

Ansible managed node IP is <default ipv4 address>

(You can obtain default ipv4 address from Ansible's gathered facts by using correct the Ansible variable taking into account Jinja2 syntax)

Install httpd server on all apps. After that make a copy of facts.txt file as index.html under /var/www/html. Make sure to start httpd service after that.

Note: Do not create a separate role for this task, just add all changes in index.yml playbook.


Please Note :-  Perform the below commands based on your question  server,  user name & other details might differ . So please read task carefully before executing. All the Best 👍

Solution:  

1Go through the folder mentioned in task and create inventory & playbook files

     cd /home/thor/playbooks/


2Create a playbook file as per the task  ( refer Video below for clarity )


---

-

  hosts: stapp01, stapp02, stapp03

  gather_facts: true

  become: yes

  become_method: sudo

  tasks:

    - name: create a  file using blockinfile

      blockinfile:

       create: yes

       path: /root/facts.txt

       block: |

         Ansible managed node IP is {{ ansible_default_ipv4.address }}

    - name: Install apache packages

      package:

       name: httpd

    - name: file copy

      shell: cp /root/facts.txt /var/www/html/index.html

    - name: ensure httpd is running

      systemd:

       name: httpd

       state: restarted


 


3. Post file saved , run below command to execute the playbook
   ansible-playbook  -i inventory index.yml



4. validate the task by curl to all app server and check
      curl http://stapp01


5. Click on Finish & Confirm to complete the task successful

Happy Learning!!!!


Apart from this if you need more clarity,  I have made a  tutorial video on this , please go through and share your comments. Like and share the knowledge




Post a Comment

2 Comments

  1. i think task 'file copy' could have ansible.builtin.copy without shell

    ReplyDelete
    Replies
    1. Thanks for your comment. Indeed you can use as per your convenient

      Delete

Latest Posts

KodeKloud Kubernetes Security CKS  Lab Challenge 4 |  Audit-policy | Install & configure falco utility | Inspect the API server audit logs and identify the user