Ansible – Ansible Flow- Install Package in Ansible (Part-4)

Posted by

using ADHOC command 
	Do one task in ONE MACHINE(localhost)													
	Do one task in ONE REMOTE MACHINE																
	Do one task in MULTIPLE REMOTE MACHINES using Inventory					  
	Do one task in MULTIPLE REMOTE MACHINES using group in Inventory	 
		
using Playbook(Program)
	Do Multiple tasks(Playbook) in ONE MACHINE(localhost) 
	Do Multiple tasks(Playbook) in ONE REMOTE MACHINE		
	Do Multiple tasks(Playbook) in MULTIPLE REMOTE MACHINE using Inventory 
	Do Multiple tasks(Playbook) in MULTIPLE REMOTE MACHINE using group in Inventory 
	Do Multiple tasks(Playbook) in MULTIPLE REMOTE MACHINE using group of group in Inventory 
	Do Multiple tasks(Playbook) in MULTIPLE REMOTE MACHINE using environment directory Inventory 
		
using Role(Program)
	Do One Role in in ONE MACHINE(localhost)
	Do Multiple Roles in ONE REMOTE MACHINE
	Do Multiple Roles in MULTIPLE REMOTE MACHINE using Inventory
	Do Multiple Roles in MULTIPLE REMOTE MACHINE using group in Inventory
	Do Multiple Roles in MULTIPLE REMOTE MACHINE using group of group in Inventory
	Do Multiple Roles in MULTIPLE REMOTE MACHINE using environment directory Inventory

Install Package in Local host using using adhoc command

Using ad-hoc Ansible commands

  1. Install Apache in Ubuntu
    • Use the apt module to install the apache2 package.
  2. Copy index.html in Ubuntu
    • Use the copy module to copy index.html to the web server’s document root.
  3. Start the Web Service
    • Use the service module to start the apache2 service.

Install Apache using apt module:

official – https://docs.ansible.com/ansible/latest/collections/ansible/builtin/apt_module.html

ansible localhost -m apt -a "name=apache2 state=present"

Copy index.html using copy module:

official – https://docs.ansible.com/ansible/latest/collections/ansible/builtin/copy_module.html

ansible localhost -m copy -a "src=/path/to/local/index.html dest=/var/www/html/index.html"

Start Apache service using service module:

official – https://docs.ansible.com/ansible/latest/collections/ansible/builtin/service_module.html

ansible localhost -m service -a "name=apache2 state=started"

Install Package in remote machine using adhoc command

Inventory

Inventory is a list of IP addresses or hostnames of the Ansible Remote Servers (ARS) that Ansible will manage.

Correct IP List:

  • The correct format separates IP addresses with commas, without spaces
18.208.198.47,172.22.240.232

uthentication and Authorization

  • Authentication: How to log in to the remote servers.
  • Authorization: What actions you are allowed to perform on the remote servers.

Ansible Commands

Ad-hoc Commands with IP Addresses

  1. Install Apache using apt module:
ansible -i "18.208.198.47,172.22.240.232," -m apt -a "name=apache2 state=present" -u jami -k -b

Copy index.html using copy module:

ansible -i "18.208.198.47,172.22.240.232," -m copy -a "src=index.html dest=/var/www/html/index.html" -u jami -k -b

Start Apache service using service module:

ansible -i "18.208.198.47,172.22.240.232," -m service -a "name=apache2 state=started" -u ubuntu -k -b

To install in all server at single run, please add ‘all’ instead of ‘-i’

ansible all -i 4.227.160.192,20.42.101.46, -m apt -a "name=apache2 state=present" -u jami -k -b
ansible all -i 4.227.160.192,20.42.101.46, -m copy -a "src=/home/jami/index.html dest=/var/www/html/index.html" -u jami -k -b
ansible all -i 4.227.160.192,20.42.101.46, -m service -a "name=apache2 state=started" -u jami -k -b

Explanation of the Command Options

  • -i: Specifies the inventory file or inline list of hosts.
  • -m: Specifies the module to use (e.g., apt, copy, service).
  • -a: Provides arguments to the module.
  • -u: Specifies the user to log in as (e.g., ubuntu).
  • -k: Prompts for the SSH password.
  • -b: Uses become to execute commands with elevated privileges (sudo).

Getting below error on first run

4.227.160.192 | FAILED! => {
    "msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.  Please add this host's fingerprint to your known_hosts file to manage this host."
}
20.42.101.46 | FAILED! => {
    "msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.  Please add this host's fingerprint to your known_hosts file to manage this host."

Two things missing

Commands to Run on Each ARS

Update the package list:

    Install sshpass

    enable sshpass in both ansible remote server

    apt-get update
    apt install sshpass
    

    Modifying the Ansible Configuration File

    The image suggests modifying the Ansible configuration file located at /etc/ansible/ansible.cfg. Here are the steps to modify this file to disable host key checking, which can help avoid SSH-related issues.

    Open the Ansible configuration file:

    sudo nano /etc/ansible/ansible.cfg
    

    Find the [defaults] section and add or modify the following line:

    [defaults]
    host_key_checking = False
    
    1. Save and close the file.
      • Press Ctrl + X to exit.
      • Press Y to confirm changes.
      • Press Enter to save.

    modify ansible config file

    /etc/ansible/ansible.cfg (using this config file we can overwrite the behavior of executable)

    root@Jami2:/home/jami# vim /etc/ansible/ansible.cfg
    

    copy configuration file and search in google from ansible.cfg

    https://github.com/ansible/ansible/blob/stable-2.9/examples/ansible.cfg

    add section and host_key_checking = False

    [defaults]
    host_key_checking = False

    Now run all the command

    ansible all -i 4.227.160.192,20.42.101.46, -m apt -a "name=apache2 state=present" -u jami -k -b
    ansible all -i 4.227.160.192,20.42.101.46, -m copy -a "src=/home/jami/index.html dest=/var/www/html/index.html" -u jami -k -b
    ansible all -i 4.227.160.192,20.42.101.46, -m service -a "name=apache2 state=started" -u jami -k -b

    Go to google and hit the ip address, if getting below page it means https not enable in VM

    alternate check from VM using

    curl http://localhost

    its working

    enable http inbound security rule from network setting to access from internet

    Example: Allowing Port 80 (HTTP)

    Here is an example of allowing port 80 for HTTP traffic:

    • Source: Any
    • Source port ranges: *
    • Destination: Any
    • Destination port ranges: 80
    • Protocol: TCP
    • Action: Allow
    • Priority: 100 (or an appropriate value based on your existing rules)
    • Name: Allow-HTTP

    Install Package in Multiple remote machine using Inventory

    Inventory File

    The inventory file lists the IP addresses of the remote servers (Ansible Remote Servers – ARS).

    Inventory File (inventory):

    18.208.198.47
    172.22.240.232
    

    Ansible Commands

    The commands provided in the image use the inventory file to run tasks on the remote servers.

    Install Apache using apt module:

    ansible all -i inventory -m apt -a "name=apache2 state=present" -u ubuntu -k -b
    
    • all: Targets all hosts listed in the inventory.
    • -i inventory: Specifies the inventory file.
    • -m apt: Uses the apt module.
    • -a “name=apache2 state=present”: Arguments for the apt module, ensuring Apache (apache2) is installed.
    • -u ubuntu: Specifies the remote user (ubuntu).
    • -k: Prompts for the SSH password.
    • -b: Uses become to run the command with elevated privileges (sudo).

    Copy index.html using copy module:

    ansible all -i inventory -m copy -a "src=index.html dest=/var/www/html/index.html" -u ubuntu -k -b
    
    • -m copy: Uses the copy module.
    • -a “src=index.html dest=/var/www/html/index.html”: Arguments for the copy module, copying index.html to /var/www/html/index.html.

    Start Apache service using service module:

    ansible all -i inventory -m service -a "name=apache2 state=started" -u ubuntu -k -b
    
    • -m service: Uses the service module.
    • -a “name=apache2 state=started”: Arguments for the service module, ensuring the Apache service (apache2) is started.

    Putting It All Together

    Create Inventory File: Create a file named inventory with the following content:

      18.208.198.47
      172.22.240.232
      
      1. Run Ansible Commands:

      Install Apache:

      ansible all -i inventory -m apt -a "name=apache2 state=present" -u ubuntu -k -b
      

      Copy index.html to Web Server:

      ansible all -i inventory -m copy -a "src=index.html dest=/var/www/html/index.html" -u ubuntu -k -b
      

      Start Apache Service:

      ansible all -i inventory -m service -a "name=apache2 state=started" -u ubuntu -k -b
      
      ansible all -i inventory -m apt -a "name=apache2 state=present" -u jami -k -b
      ansible all -i inventory -m copy -a "src=/home/jami/index.html dest=/var/www/html/index.html" -u jami -k -b
      ansible all -i inventory -m service -a "name=apache2 state=started" -u jami -k -b

      Install Package in MULTIPLE REMOTE MACHINES using group in Inventory

      Inventory File with Groups

      The inventory file defines groups of hosts. Here, there are two groups: web and db.

      Inventory File (inventory):

      [web]
      18.208.198.47
      172.22.240.232
      
      [db]
      18.208.198.4
      172.22.240.2d
      

      Ansible Commands Using Groups

      The commands provided in the image target the web group to install Apache, copy a file, and start the Apache service.

      Install Apache using apt module:

        ansible web -i inventory -m apt -a "name=apache2 state=present" -u ubuntu -k -b
        

        Copy index.html using copy module:

        ansible web -i inventory -m copy -a "src=index.html dest=/var/www/html/index.html" -u ubuntu -k -b
        

        Start Apache service using service module:

        ansible web -i inventory -m service -a "name=apache2 state=started" -u ubuntu -k -b
        

        Explanation of the Commands

        • web: Specifies the group of hosts to target (web group).
        • -i inventory: Specifies the inventory file.
        • -m apt: Uses the apt module.
        • -a "name=apache2 state=present": Arguments for the apt module, ensuring Apache (apache2) is installed.
        • -m copy: Uses the copy module.
        • -a "src=index.html dest=/var/www/html/index.html": Arguments for the copy module, copying index.html to /var/www/html/index.html.
        • -m service: Uses the service module.
        • -a "name=apache2 state=started": Arguments for the service module, ensuring the Apache service (apache2) is started.
        • -u ubuntu: Specifies the remote user (ubuntu).
        • -k: Prompts for the SSH password.
        • -b: Uses become to run the command with elevated privileges (sudo).
        guest
        0 Comments
        Inline Feedbacks
        View all comments
        0
        Would love your thoughts, please comment.x
        ()
        x