Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Linux Distributions»Debian»How to Install Ansible on Debian 9 (Stretch)

    How to Install Ansible on Debian 9 (Stretch)

    RahulBy RahulAugust 25, 20183 Mins ReadUpdated:August 29, 2018

    Ansible is an automation tool for managing multiple remote hosts from the single machine. It provides you with an easy to configure for a large number of remote hosts. For example, you are performing the same tasks over multiple machines, Ansible provides you the option to automate these tasks.

    This tutorial will help you to install Ansible on Debian 9 (Stretch) systems.

    Step 1 – Configure SSH Access

    You can configure key based ssh for the remote Linux Ansible hosts. So password will not be required for SSH. Ansible also allows you to use a password for ssh, but key-based ssh is more secure.

    Generate a ssh key pair on your Ansible server:

    ssh-keygen
    

    Copy the public key to all your remote hosts you need to connect via this ssh key.

    ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]_host
    

    Step 2 – Install Ansible on Debian 9

    Ansible provides its official PPA for the installation on Debian systems.

    Add the following line to /etc/apt/sources.list file:

    deb http://ppa.launchpad.net/ansible/ansible/ubuntu trusty main
    

    Then run the following commands to add signing key and install Ansible.

    sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 93C4A3FD7BB9C367
    sudo apt update
    sudo apt install ansible
    

    Step 3 – Configure Ansible Hosts & Groups

    Your server is ready with Ansible for remote host management and automation. You can have a number of hosts you need and manage them with single Ansible server.

    Here you need to define your remote systems in Ansible hosts file (/etc/ansible/hosts). You can also make groups of hosts with similar types. Here you need to properly organize your hosts into groups. Groups are used for performing one task on all remote hosts defined under it.

    Edit Ansible hosts configuration file. For exmaple:

    sudo nano /etc/ansible/hosts
    

    Add your hosts and organize them with groups. A host can be added under multiple groups.

    [webservers]
    web-host1
    web-host2
    
    [dbservers]
    db-host1
    

    The below image will help you to understand group and hosts under a group.

    Single Host Vars Configuration

    You need to define settings for your hosts. The host-specific file must be with the same name as host (eg: web-host1) under the host_vars directory.

    sudo mkdir /etc/ansible/host_vars
    sudo vi /etc/ansible/host_vars/web-host1
    

    Add the SSH settings to this file for the web-host1.

    ansible_ssh_host: 192.168.1.15
    ansible_ssh_port: 22
    ansible_ssh_user: root
    

    In case you don’t have used Step 1 for the ssh connection for this host. You can also add one of the below methods to web-hosts1 configuration file for the authentication.

    ansible_ssh_pass: secret_password
    ansible_ssh_private_key_file: /home/rahul/.ssh/aws.pem
    

    Group Vars Configuration

    You can configure common variable settings of a Group under group configurations. The group file name must be same as the group name (eg: webservers) under group_vars directory.

    sudo mkdir /etc/ansible/group_vars
    sudo vi /etc/ansible/group_vars/webservers
    

    Add the common variables to this file used by all the hosts added under this group.

    ansible_ssh_port: 22
    ansible_ssh_user: root
    

    Step 4 – Test Ansible Setup

    Your Ansible setup is ready for testing. To test all hosts connectivity using ping module like:

    ansible -m ping all
    

    To test connectivity for specific host or group of hosts

    ansible -m ping web-host1        ## Specific host 
    ansible -m ping webservers       ## Specific group 
    

    You can also run a command using shell module. For example, test the free memory on web-host1. You can also perform the same task for a group. Just use group name instead of hostname.

    ansible -m shell -a 'free -m' web-host1
    

    Ansible
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Install Ansible on Ubuntu 16.04 (Xenial)
    Next Article How to Install Ansible on Debian 8 (Jessie)

    Related Posts

    How to Create a Sudo User in Debian

    Updated:June 26, 20212 Mins Read

    How To Install XRDP (Remote Desktop) on Debian 10

    4 Mins Read

    Initial Server Setup with Debian 10/9/8

    Updated:June 25, 20214 Mins Read

    How To Install and Configure VNC Server on Debian 10

    Updated:June 26, 20215 Mins Read

    How to Install TeamViewer on Debian 10

    3 Mins Read

    How to Install .NET Core on Debian 10

    2 Mins Read

    4 Comments

    1. martin on October 22, 2020 1:09 pm

      sudo apt-key adv –keyserver keyserver.ubuntu.com –recv-keys 93C4A3FD7BB9C367 fail with

      Executing: /tmp/apt-key-gpghome.eMCYAsZUlC/gpg.1.sh –keyserver keyserver.ubuntu.com –recv-keys 93C4A3FD7BB9C367
      gpg: keyserver receive failed: Server indicated a failure

      Reply
      • Jannek on January 8, 2021 11:42 am

        “sudo apt-key adv –keyserver keyserver.ubuntu.com –recv-keys 93C4A3FD7BB9C367” is not the right command, try “sudo apt-key adv –keyserver keyserver.ubuntu.com –recv-keys 93C4A3FD7BB9C367”

        Reply
    2. Dan on June 4, 2019 10:20 am

      if you get error on freshly installed Debian 9:
      gpg: keyserver receive failed: No dirmngr
      then install dirmngr:
      apt install dirmngr

      Reply
    3. Sboon on February 14, 2019 7:30 pm

      Great post as always.

      Very helpful, thanks!

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Install Ionic Framework on Ubuntu 22.04
    • What is the /etc/hosts file in Linux
    • How to Install Angular CLI on Ubuntu 22.04
    • How to Install Composer on Ubuntu 22.04
    • How to Create DMARC Record For Your Domain
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.