Ansible Installation and Basic Usage Guide
This guide walks through setting up a two‑node Linux environment, installing Ansible, configuring its inventory and SSH keys, and demonstrates common Ansible commands for managing hosts, checking connectivity, and executing remote tasks.
Before installing Ansible, prepare a small lab consisting of two worker nodes (node1 and node2) and a control server (ansible.server.com). The table below lists each host’s role, hostname, OS release, and IP address.
Role
Hostname
SystemRelease
IP address
node1
node1.server.com
Rhel-6.5_x86_64
192.168.1.63
node2
node2.server.com
Rhel-6.5_x86_64
192.168.1.64
server
ansible.server.com
CentOS-6.5_x86_64
192.168.1.20
1. Preparation
Disable the firewall, change the hostname, and update the /etc/sysconfig/network file on each node. Then log out and log back in (or reboot) to apply the changes. Finally, add host entries to /etc/hosts so the nodes can resolve each other.
#关闭防火墙
service iptables stop
chkconfig iptables off
#更改主机名
hostname node1.server.com
vim /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=node1.server.com
logout # 注销 重新登录 或者是 reboot 重启
#添加hosts解析
tail -3 /etc/hosts
192.168.1.63 node1.server.com #node1
192.168.1.64 node2.server.com #node2
192.168.1.20 ansible.server.com #ansible2. Install Ansible
rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
yum install ansible -y #安装 ansibleAfter installation, the main configuration file is /etc/ansible/ansible.cfg and the default inventory file is /etc/ansible/hosts. The following command lists all files installed by the Ansible package.
rpm -ql ansible | more
/etc/ansible
/etc/ansible/ansible.cfg # main config file
/etc/ansible/hosts # inventory file
/etc/ansible/roles
/usr/bin/ansible
/usr/bin/ansible-console
/usr/bin/ansible-doc
/usr/bin/ansible-galaxy
/usr/bin/ansible-playbook
/usr/bin/ansible-pull
/usr/bin/ansible-vault3. Configure Inventory and SSH Keys
Add the two worker nodes to the inventory under a group called webserver:
tail -3 /etc/ansible/hosts
[webserver]
node1.server.com
node2.server.comGenerate an SSH key pair on the control server and copy the public key to each node to enable password‑less authentication.
ssh-keygen
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]4. Common Ansible Commands
Check the current date on all nodes (the command module is the default, so it can be omitted):
ansible all -a date
ansible all -m command -a dateVerify connectivity using the built‑in ping module: ansible all -m ping # 内建的 ping 模块 Run arbitrary shell commands on every node, for example to echo a message, display disk usage, or show the Ethernet interface configuration:
ansible all -a "/bin/echo hello,world" # 输出信息
ansible all -a "/bin/df -h" # 输出挂载信息
ansible all -a "/sbin/ip addr show dev eth0" # 查看 eth0 网卡信息These steps provide a basic, reproducible Ansible environment for managing Linux hosts.
Thank you for reading; feel free to leave a comment or share this guide with others.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
