Master Ansible Automation: Key Features, Setup, and Practical Playbook Commands
This tutorial introduces Ansible, outlines its main features, guides you through preparing a lab environment, configuring hosts and the master node, and demonstrates common modules and command options with clear code examples for effective automation.
Ansible Overview
Official site: https://docs.ansible.com. Ansible is a modern automation and operations tool written in Python, combining advantages of tools like Puppet, Chef, and Fabric to enable bulk system configuration, program deployment, and command execution.
It operates on a modular basis; the core itself does not perform bulk deployment, but the modules it runs provide that capability.
Key Features
Modular: invoke specific modules to complete tasks
Key Python modules: Paramiko, PyYAML, Jinja2 (template engine)
Supports custom modules
Implemented in Python
Simple deployment: agentless, relies on Python and SSH (pre‑installed)
Security: based on OpenSSH
Playbook orchestration support
Idempotent: repeated runs produce the same result
No agent or PKI required
Modules can be written in any programming language
YAML format for task orchestration with rich data structures
Powerful multi‑layer solutions
Lab Environment Preparation
Server Name IP Address
master 192.168.1.20
node01 192.168.1.21
node02 192.168.1.221.0 Configure local /etc/hosts on each node
cat <<EOF >> /etc/hosts
192.168.1.20 master
192.168.1.21 node01
192.168.1.22 node02
EOF2.0 Install Ansible on the master node
yum -y install ansibleAnsible main configuration (ansible.cfg)
cat /etc/ansible/ansible.cfg
inventory = /etc/ansible/host # host inventory file
forks = 5 # concurrency
sudo_user = root # privilege escalation
remote_port = 22 # SSH port
host_key_checking = False # skip host key verification
timeout = 10 # connection timeout
log_path = /var/log/ansible.log
private_key_file = /root/.ssh/id_rsaHosts inventory file
Hosts can be grouped by service type (e.g., web, db).
Verify remote operation
2.1 Operate on a host group
ansible web -a "df -h" # check disk usage for the web group2.2 Operate on specific hosts within a group
2.3 Operate on all hosts
3.0 Configure password‑less SSH
# Generate key pair
ssh-keygen
# Copy the public key to target hosts
ssh-copy-id [email protected]
ssh-copy-id [email protected]3.1 Edit the inventory file and test
cat /etc/ansible/hostsansible web -a "ls /tmp"Verification succeeded.
Common Ansible Modules
shell/command – execute shell commands
copy/file – transfer files
yum – manage packages
user – manage users and groups
git – deploy from source control
service – manage services
setup – gather host facts
4.1 Shell module with sudo escalation
# Run on node01 and node02
useradd zhangfan
echo 123456 | passwd --stdin zhangfan
# Configure sudo on node02
vim /etc/sudoersnode01 does not have sudo privileges.
Master test
ansible web -m shell -a 'ls /root' -u zhangfan -k --become --become-user root -K ansible web \
-m shell \ # specify module
-a 'ls /root' \ # command to run
-u zhangfan -k \ # remote user and prompt for password
--become --become-user root \ # privilege escalation
-K # prompt for sudo password4.2 Copy module
ansible web -m copy -a "src=/root/nginx-1.12.tar dest=/tmp" -u root4.3 File module
ansible web -m file -a "dest=/opt/hello mode=600 state=directory" -u rootFile states:
absent – delete
directory – create directory
file – regular file
hard – hard link
link – symbolic link
touch – empty file
Directory creation succeeded.
Delete directory: ansible web -m file -a "dest=/opt/hello state=absent" Create a file:
ansible web -m file -a "dest=/opt/hello mode=755 state=touch"4.4 Yum module
States: absent (uninstall), present (install).
ansible web -m yum -a 'name=memcached state=present'
ansible web -m yum -a 'name=memcached state=absent'4.5 User module
ansible web -m user -a "name=lisi password=123.com"
ansible web -m user -a "name=php password=123456 shell=/sbin/nologin"Delete user:
ansible web -m user -a "name=lisi state=absent"4.6 Git module
Ensure an empty directory exists first.
ansible web -m git -a "repo=https://github.com/ansible/ansible.git dest=/mnt/ansible"4.7 Service module
Start nginx service:
ansible web -m service -a "name=nginx state=started"Enable service at boot:
ansible web -m service -a "name=nginx enabled=true"4.8 Setup module
Print all gathered facts: ansible web -m setup Get OS family:
ansible web -m setup -a "filter=ansible_os_family*"Get memory information:
ansible web -m setup -a "filter=ansible_*_mb"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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
