We can manage the ansible nodes from simple CLI commands. The ansible commands are easy to run on CLI. The playbooks are used these simple patterns of commands. In this blog, we understand all commands step by step. Now check this blog for the ansible basic commands.
Topics cover:
– Check the ansible version,
– Ping the remote hosts,
– List all ansible nodes and check nodes in a particular host group
– Copy, create and delete the file or directory.
– Use the Ansible modules like shell, copy, file, service, yum, etc on CLI
Note: Check the link to “Install the ansible on CentOS 8 and Ubuntu 18.08, 20.04“
Check ansible version:
How to check the installed ansible version on the ansible node?
# ansible --version
ansible 2.9.7
Connect and test remote ansible node:
In this example, we categories the ansible node like test node, database node, application.
Add the following text into the /etc/ansible/hosts file for testing. [test]
192.168.0.101
[database]
192.168.0.102
192.168.0.103
[application]
192.168.0.30
List the ansible hosts in the /etc/ansible/hosts file:
# ansible --list-hosts <all/host_group_name>
Command description:--list-hosts:
List the hosts in the /etc/ansible/hosts file.all:
All group names of the hosts.host_group_name:
Group name specify in the /etc/ansible/hosts file.
Examples:# ansible --list-hosts all
Ouput:
# ansible --list-hosts test
# ansible --list-hosts database
# ansible --list-hosts application
Ping the host:
The following command checks the remote ansible node is reachable or not.# ansible -m ping <host_group/IP>
Command description:-m:
Execute the ansible module name.ping:
Ansible Module name.
host_group/IP: Group of hosts/IP in the file /etc/ansible/hosts.
Examples:# ansible -m ping test
Check the following output of the above commands.
# ansible -m ping 192.168.0.110
Run Linux command in ansible:
The following command runs the Linux shell command using ansible CLI.# ansible -vv -m shell -a '<linux_command>' <host_group/IP>
Command description:-vv:
Verbose, add multiple ‘v’ means to increase the display messages.-m:
Execute the ansible module name. shell:
Ansible Module name.-a:
Module arguments.linux_command:
Linux commands like hostname, hostname -I, ls -lrth
, etc.host_group/IP:
Group of hosts/IP in the file /etc/ansible/hosts.
Examples:# ansible test -v -m shell -a 'hostname -I'
# ansible test -v -m shell -a 'ls -lrth'# ansible test -v -m shell -a 'rpm -q vim-common'
(Note: The above commands, display the final output and not in-progress output.)
# ansible test -v -m shell -a 'rpm -q ansible'
# ansible test -vv -m shell -a 'yum install -y epel-release && yum install -y ansible'
In the following output, the vim-common package already installed, and the ansible package is not installed.
Ansible copy module:
The following command copies the file from the current instance (source) to a remote ansible node (destination).# ansible test -m copy -a \ "src=<file_from_copy_source_path> \ dest=<file_to_copy_destination_path>"
Command description:test:
The host group specified in the /etc/ansible/hosts file in the current ansible node.
OR Instead of “test”, you can use IP.
(Note: If you use IP then an ansible host can ssh passwordless to that IP(instance).
Check the link for “passwordless ssh” step 6.)-m:
Execute the ansible module name.-a:
Module arguments.copy:
Ansible Module name.src:
The source file path from where copy.dest:
The destination file path to where copy. (The file path present in the “test” host group or IP.)
Example:# ansible test -m copy -a "src=/home/ansible/test.txt dest=/tmp"
Output:
Ansible file module (Change the file/directory permission):
The following command changes the file or directory permission on the remote ansible node.# ansible test -m file -a "dest=<file_destination> mode=<permision in interger format> owner=<user_name> group=<group_name>"
Command description:test:
The host group specified in the /etc/ansible/hosts file.
OR Instead of “test”, you can use IP.-m:
Execute the ansible module name.file:
Ansible Module name.-a:
Module arguments.dest:
The file destination present in the “test” host group or IP.mode:
The file permission in the integer format. (read) r=4 (write)w=2 (execute)x=1. The file permission 640. Check the following combinations.
Read & write (rw) | Write(w) | Execute(x) |
4 + 2 = 6 | 4 | 0 |
Owner | Group | Others |
6 | 4 | 0 |
Owner:
User name. You can specify the existing user name.Group:
The user group name or group name. You can specify the existing group name.
Example:# ansible test -m file -a "dest=/tmp/test.txt mode=640 owner=ansible group=ansible"
Output:
Ansible file state:
The following command creates or removes the file on the remote ansible node.# ansible test -m file -a "dest=<file_destination> state=<file_state>"
Command description:test:
The host group specified in the /etc/ansible/hosts file.
The “test” is a group of nodes. As a result which runs the command on nodes in the group.-m:
Execute the ansible module name.file:
Ansible Module name.-a:
Module arguments.dest:
The file or directory destination present in the “test” host group or IP.state:
The state like create(touch), delete(absent).
Examples:
Create a file on the remote ansible node which is a destination:# ansible test -m file -a "dest=/tmp/test.txt state=touch"
Delete a file on the remote ansible node which is a destination:# ansible test -m file -a "dest=/tmp/test.txt state=absent"
Output:
Ansible install, remove packages:
The following ansible command install, remove packages on the remote ansible node using yum package manager.# ansible test -m yum -a "name=<package_name> state=<package_state>"
Command description:test:
The host group specified in the /etc/ansible/hosts file.
OR Instead of “test”, you can use IP.-m:
Execute the ansible module name.yum:
Ansible Module name.-a:
Ansible Module arguments.name:
Package name to install or remove.state:
The state like install or remove.
Examples:
Install vim package:# ansible test -m yum -a "name=vim state=present"
Remove vim package:# ansible test -m yum -a "name=vim-common state=absent"
Output:
Ansible service module:
The following ansible command installs, start, restart, and stop service on the remove ansible node. # ansible test -m service -a "name=<service_name> state=<service_state>"
Command description:test:
The host group specified in the /etc/ansible/hosts file.
OR Instead of “test”, you can use IP.-m:
Execute the ansible module name.
service: Ansible Module name.-a:
Ansible Module arguments.name:
Service name to start, restart, or stop.state:
Service state which is the start, restart, or stop.
Examples:
Install bind server using ansible command.# ansible test -m yum -a "name=bind state=present"
Start, restart, and stop. The service name for the bind server is named service using ansible command.# ansible test -m service -a "name=named state=started"
# ansible test -m service -a "name=named state=restarted"
# ansible test -m service -a "name=named state=stopped"
Output:
Ansible Doc Module:
The following ansible command displays the doc of Ansible modules.# ansible-doc --list
It displays the ansible module.
Examples:# ansible-doc -s service
-s:
Playbook snippet service:
Ansible Module name in ansible. You can specify any ansible module name apt, yum, etc.
Also, check the -j option instead of -s. The -j , –json option displays the output in JSON format.
Check the following output of the above commands. The output is truncated.
The Ansible Facts:
The ansible facts retrieve the information of remote instances like distribution release, fqdn (fully qualified domain name), hostname, OS family Linux, windows, etc. IPv4 or IPv6. Check the following command with their examples.
# ansible test -m setup -a "filter=<paramters>"
-m:
Execute the ansible module name.setup:
Ansible Module name.-a:
Ansible Module argumentsfilter=<paramters>:
Parameters in the setup module.
Check the following parameters.
– ansible_distribution:
The distribution like CentOS, Redhat Enterprise Linux, Ubuntu, etc
– ansible_distribution_release:
The distribution release like CentOS (core), Ubuntu (Trusty Tahr, Precise Pangolin, etc)
– ansible_distribution_version: The distribution version like CenOS 7,8, Ubuntu 12.04, 14.04 , 20.04, etc.
– ansible_fqdn:
The fqdn (fully qualified domain name) is an absolute domain name like host1.smarttechfunda.com
– ansible_hostname:
The hostname, the name of the instance is host1 in host1.smarttechfunda.com.
– ansible_os_family:
The OS family-like Redhat, Debian, etc.
– ansible_pkg_mgr:
The package manager like yum, dnf, apt, etc
– ansible_default_ipv4.address:
Display the ipv4 like 192.168.0.1.
– ansible_default_ipv6.address:
Display the ipv6 hexadecimal number like 2001:db8:0:0:1:0:0:1
Examples:
(Note: The following command takes a few seconds to execute. As a result, it takes some time to retrieve the information.)# ansible test -m setup -a "filter=ansible_distribution*"
# ansible test -m setup -a "filter=ansible_hostname"
Output:
The following display all subsets of ansible_facts like ansible_all_ipv4_addresses, ansible_all_ipv6_addresses, ansible_architecture, etc.# ansible test -m setup -a gather_subset=all | less
-m:
Execute the ansible module name.setup:
Ansible Module name.-a:
Ansible Module argumentsgather_subset:
Display all subsets of ansible facts. Instead of “all” as input, you can specify the min, network, hardware, virtual, etc.|
: Combine the two Linux shell commands.less
: The less command displays the output in a scrolling pattern. So that user can easily scroll up or down the output.
Check the following output of the above command. The rest output is truncated due to long output.
Coming soon check the network info, ansible-galaxy init, and how to write a simple ansible-playbooks
No comments! Be the first commenter?