In Lab 6: Basic Commands of Ansible, participants typically delve into the fundamental commands and concepts that form the core of Ansible's automation capabilities. This lab focuses on hands-on experience with commands such as 'ansible' to execute ad-hoc commands on remote hosts, 'ansible-playbook' to run playbook scripts, and 'ansible-galaxy' to manage roles. Participants may also explore concepts like inventories, which define the target hosts, and modules, which represent individual tasks within playbooks. The lab aims to deepen participants' understanding of Ansible's syntax and workflow, empowering them to automate repetitive tasks, configurations, and deployments effectively. Successful completion of Lab 6 equips participants with the foundational skills needed to harness Ansible's automation power for streamlined infrastructure management within their DevOps environments.
vi /etc/ansible/hosts
//“all” pattern referes to all the machines in an inventory
ansible all --list-hosts
ansible <group name> --list-hosts
ansible <group name>\[0\] --list-hosts
//You can refer to hosts within the group by adding a subscript to the group name while giving the pattern
Groupname\[0\] -> Picks the first machine in the group
Groupname\[1\] -> Picks the second machine in the group
Groupname\[-1\] -> Picks the last machine in the group
Groupname\[0:1\] -> Picks first 2 machine in the group
//Group separated by colon can be used to use hosts from multiple groups
Groupname1:groupname2
ansible all --list-hosts
ansible demo –a “ls”
ansible demo –a “ls-al”
ansible demo –a “touch myfile” (re run and verify idempotence)
ansible demo –a “yum install httpd –y” (shows permission denied)
ansible demo –b –a “yum install httpd –y”
ansible demo –b –a “yum remove httpd –y”
ansible \[group\] –m <module> -z <cmd>
ansible demo –m ping
ansible demo –b –m yum –a “pkg=httpd state=present”
ansible demo –b –m yum –a “pkg=httpd state=latest”
ansible demo –b –m yum –a “pkg=httpd state=absent”
// State=present will install it
// State=latest will update
//State=absent will remove it
ansible demo –b –m service –a “name=httpd state=started”
ansible demo –b –m service –a “name=httpd state=restarted”
ansible demo –b –m service –a “name=httpd state=stopped”
ansible demo –b –m user –a “name=raj”
ansible demo –b –m user –a “name=raj state=absent”
ansible demo –m setup
ansible demo –m setup –a ‘filter=\*ipv4\*’
ansible-playbook <playbook>.yml
\---# A list of tasty fruits
fruits:
- Apple
- Orange
- Straberry
- Mango
---
\---#An employee record
Employee:
name: Ram
job: DevOps Engineer
skill: Elite
---