Updated March 8, 2023
Introduction to Ansible Commands
In this Topic, we are going to learn about the Ansible Commands as Ansible is an engine that automates the deployment of the application, cloud provisioning, etc. It uses a playbook for archiving the job automation process, and the playbook is designed on top of easy and flexible also human-readable format languages like YAML etc. It mostly involves multitier deployment. It connects with nodes through a secured shell host, which states that this setup is built demons.
Basic Ansible Commands
Below are mentioned the basic commands:
- Verify connectivity of host: # ansible <group> -m -ping
- Rebooting host systems: #ansible <group> -a “/sbin/reboot”
- Creating a new user: # ansible <group> -m user -a “name=ansible password=<encrypted password>”
- User Deletion: # ansible <group> -m user -a “name=ansible state=absent”
- Permissions changing: # ansible <group> -m file -a “dest=/home/ansible/file1.txt mode=777”
- Verify package installation with update: # ansible <group> -m yum -a “name=httpd state=latest”
- Verify package installation without an update: # ansible <group> -m yum -a “name= httpd state=present”
- Verify package version: # ansible <group> -m yum -a “name= httpd-1.8 state=present”
- Verify package installation: # ansible <group> -m yum -a “name= httpd state=absent”
- Display host information: # ansible <group> -m setup | less
Intermediate Ansible Commands
- File transfer to more than one server : # Ansible abc -m copy -a “src = /etc/yum.conf dest = /tmp/yum.conf”
- Rebooting more than one group servers : # Ansible abc -a “/sbin/reboot” -f 12
- All adhoc information on the available facts can be gathered using the below command: # Ansible all -m setup
- Help option on the available roles : # ansible-galaxy -h
- Set a timeout in ansible : # ansible all -a “/usr/bin/scrptat” -B 2600 -P 0
- Generate only ansible fact for one host : #ansible <host> -m setup -a ‘filter=ansible_eth*’
- Verify package installation with update : # ansible <group> -m yum -a “name=httpd state=latest”
- Saving all the facts in a temporary directory : # ansible all -m setup –tree /tmp/facts
- Synopsis to Run ansible playbook : # ansible-playbook [options] playbook.yml [playbook2 …]
- Eg: # ansible-playbook –check playbook_a.tml
- Synopsis to run ansible pull : # ansible-pull -U <repository> [options] [<playbook.yml>]
Advanced Ansible Commands
Executing commands on the remote host by using psexec models:
psexec:
hostname: 197.163.12.2
connection_username: username
connection_password: password
executable: powershell.exe
arguments: '-'
stdin: |
Write-Host Hi
Write-Error Error Message
exit
Run process asynchronously using psexec:
psexec:
hostname: server
connection_username: username
connection_password: password
executable: cmd.exe
arguments: /c rmdir C:\temp
asynchronous: yes
Case sensitive password string match:
name: Case insensitive password string match
expect:
command: passwd username
responses:
(?i)password: "MySekretPa$$word"
# you don't want to show passwords in your logs
no_log: true
Tips and Tricks to Use Ansible Commands
- The same role has to be applied for more than one operation for efficient functioning.
- Ansible tasks must always be named.
- YAML tasks must be largely used.
- Variables must also be documented.
- Use assert to bail early in case of error.
- Write meaningful error messages.
- Ansible allows declaring playbooks without their name
E.g.:
hosts: local
tasks:
– user:
name: Test1
state: past
groups: group1
- Asserts in Ansible can be used in order to validate each and every parameter.
E.g.:
name: “Validate is a number, > 0”
assert:
that:
– “{{ version | int }} > 0”
msg: “‘version’ should be a number and > 0, is \”{{version}}\””
- Templates are mainly used to create or modify files that have small parameters at the destination. Tags must be used in moderation. When a role is written, tags can be used to filter tasks in runtime. This helps to imply runtime deployments. Two key problems in these kinds of ansible are below,
- The same tag can be used over and over in all of your roles and collide with each other, preventing you from using exactly the tag you want to use this dispersal of tags makes it difficult to understand exactly what they do.
- He determining the exact output is a complex task
Conclusion
Ansible commands allow you to perform software provisioning and configuration management tasks in a very structured and optimized method.it also holds a strong hand in platform support and cloud integration methods.
Recommended Articles
This has been a guide to Ansible Commands. Here we have discussed basic as well as advanced Ansible Commands and some immediate Commands. You may also look at the following article to learn more –