dev-resources.site
for different kinds of informations.
How to install ansible and run your first ansible Ad-Hoc Command.
Published at
12/5/2024
Categories
devops
ansible
cloud
cloudcomputing
Author
kartik_p
Author
8 person written this
kartik_p
open
Install Ansible and Run Your First Playbook
Prerequisites
-
Create two EC2 instances:
- Main instance: Install Ansible.
- Target instance: The system to configure.
Step 1: Install Ansible on the Main Instance
Commands:
sudo apt update
sudo apt install ansible
Verify Installation:
ansible --version
Step 2: Set Up Passwordless Authentication
On the Main Instance:
- Generate an SSH Key Pair:
ssh-keygen
The public key is usually located at ~/.ssh/id_rsa.pub
.
-
Copy the Public Key:
Open the file with a text editor (e.g.,
vim
) and copy its contents.
On the Target Instance:
- Log in:
ssh <username>@<target_server_ip>
-
Paste the Public Key into
~/.ssh/authorized_keys
:
vim ~/.ssh/authorized_keys
Save changes with :wq
.
- Test SSH: From the main instance:
ssh <target_server_ip>
Passwordless authentication should now work.
Step 3: Create an Inventory File
- Create a file named
inventory
:
[webservers]
192.168.1.10
192.168.1.11
[databases]
192.168.1.20
Step 4: Run Ad-Hoc Commands
- Run a Command Across All Servers:
ansible -i inventory all -m shell -a "command_to_execute"
- Example:
ansible -i inventory webservers -m shell -a "ls -la"
Step 5: Write and Run Playbooks
-
Create a Playbook (e.g.,
playbook.yml
):
---
- name: Update and upgrade systems
hosts: all
become: yes
tasks:
- name: Update package lists
apt:
update_cache: yes
- name: Upgrade packages
apt:
upgrade: yes
- Run the Playbook:
ansible-playbook playbook.yml -i inventory
By following these steps, you’ll have a basic Ansible setup to manage your infrastructure effortlessly.
ansible Article's
30 articles in total
Managing Lines in Text Files with Ansible Lineinfile Module
read article
Ansible
read article
Unlocking Automation with Ansible Playbooks
read article
Mastering Ansible on macOS A Step by Step Guide
read article
Vecchio RaspberryPi, Nuova HomeLab!
read article
Ansible Ad-Hoc Commands: Complete Guide for Beginners
read article
Ansible Patterns for Ad-Hoc Commands and Playbooks: Complete Guide for Beginners
read article
How to Manage Kubernetes with Ansible
read article
Ansible fail2ban
read article
Continous Integration And Continous Deployment Of A Full-Stack Docker-Compose Application
read article
How to Keep Your Playbooks Secure Using Ansible Vault
read article
How to apply configurations to multiple hosts using Ansible
read article
Ansible Yum Module : Installing & Removing Packages
read article
How to install ansible and run your first ansible Ad-Hoc Command.
currently reading
How to manage dependencies in Ansible roles?
read article
Mastering Ansible Playbooks: Step by Step Guide
read article
Automating Production-grade multi-node Kubernetes with KUBESPRAY on multipass with Just a single command
read article
Let's Explore Configuration Drift Management.
read article
Install Tailscale With Ansible
read article
Ansible Facts: Complete Guide for Beginners
read article
Ansible Collections: Complete Guide for Beginners
read article
Ansible Playbooks: Complete Guide for Beginners
read article
Ansible Roles and Plugins: Complete Guide for Beginners
read article
Ansible Tasks: Complete guide for Beginners
read article
Ansible Jinja2: Complete Guide for Beginners
read article
Ansible Inventory: Complete Guide for Beginner
read article
How to troubleshoot issues with Ansible ad-hoc commands?
read article
Ansible: Installation and Configuration Guide for Beginners
read article
Mastering Ansible: The Essential Guide for DevOps Engineers
read article
Automating Docker Workflows with Ansible: A Complete Guide
read article
Featured ones: