Ansible Core Concepts and Basic Command Examples
This article introduces essential Ansible terminology such as control node, managed nodes, inventory, host files, modules, tasks, playbooks, and roles, and demonstrates basic commands for user creation, directory management, file deletion, and package handling on Linux hosts.
During the use of Ansible, some terminology is encountered; we will introduce them first.
Important Ansible terms related to nodes include control node, managed node, inventory, and host file:
Control node : The host where Ansible is installed, also called the Ansible server or management machine. It runs tasks and requires Python and Ansible dependencies. Note: Ansible cannot be installed on Windows.
Managed nodes : Also called client machines, these are the target servers on which Ansible executes tasks.
Inventory : The list of managed nodes, i.e., all hosts to be managed.
Host file : The inventory list is usually stored in a file named hosts. In the host file you can use IP addresses or hostnames, specify authentication information, and group hosts by user. The default file is /etc/ansible/hosts, but a custom host file can be specified with the -i option.
Modules : Code blocks that perform specific tasks such as adding users, uploading files, or pinging hosts. Ansible ships with over 450 modules, and the Ansible Galaxy repository contains about 1,600 additional modules.
Task : An operation executed on a managed node. Single-line ad‑hoc commands can be used to run a task.
Playbook : A YAML‑formatted, repeatable list of tasks that makes writing and sharing automation easier. Many community playbooks are available on GitHub.
Roles : Introduced in Ansible 1.2, roles provide a hierarchical, structured way to organize playbooks, automatically loading variable files, tasks, handlers, etc.
Example 1 – Create a user on all hosts in the webservers group: [root@master ~]# ansible webservers -a 'useradd mzl' Output:
192.168.210.181 | CHANGED | rc=0 192.168.210.177 | CHANGED | rc=0 192.168.210.176 | CHANGED | rc=0Example 2 – Create a directory with mode 600 on the managed hosts:
[root@master ~]# ansible webservers -m file -a "dest=/tmp/mzl mode=600 state=directory" -u rootExample 3 – Delete a previously copied file on the managed hosts:
[root@master ~]# ansible webservers -m file -a "dest=/usr/local/src/nginx-1.15.6.tar.gz state=directory" -u rootExample 4 – Manage a package and start a service:
[root@master ~]# ansible webservers -m yum -a "name=memcached state=present" -u rootStart the memcached service:
[root@master ~]# ansible webservers -m shell -a "systemctl start memcached"Verify the service status:
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.
Practical DevOps Architecture
Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.
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.
