Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

Ansible Adhoc Commands Lab & Excercise – Part 1

Program 1 – Write a Ansible Adhoc Commands to create a group called “deploy

How to verify?
$ more /etc/group | grep deploy

Program 2 – Write a Ansible Adhoc Commands to create a user called “deploy-user” which is part of group called “deploy” and with /bin/bash shell.

How to verify?
$ more /etc/passwd | grep deploy-user

Program 3 – Write a Ansible Adhoc commands install package named “httpd” in RHEL/centos.

How to verify?
$ which httpd

Program 4 – Write a Ansible Adhoc commands to start and enable the service named “httpd”

How to verify?
$ ps -eaf | grep httpd

Program 5 – Write a Ansible commands to create a file called “index.html” in /var/www/html with some dummy html contents.

How to verify?
Browse http://x.x.x.x:80

Program 6 – Write a Ansible commands to copy a file called “second.html” in /var/www/html/second.html with some dummy html contents.

How to verify?
Browse http://x.x.x.x/second.html

Program 7 – Write a Ansible commands to install a package called “git”, “wget”.

How to verify?
$ which git
$ which wget

Program 8 – Write a Ansible Adhoc commands to clone git repo. https://github.com/scmgalaxy/ansible-role-template.

How to verify?
$ ls

Program 9 – Write a Ansible commands to reboot a self machine.

Program 10 – Write a Ansible commands to touch a file called “devopsschool.txt” in /opt/ and delete after using ansible adhoc command.

Rajesh Kumar
Follow me
Subscribe
Notify of
guest
3 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Arturo Alvarado Esparza
Arturo Alvarado Esparza
1 year ago

root@ip-172-31-24-147:~# ansible all -i inventory -m group -a “state=present name=deploy” -u ubuntu –key-file=node.pem -b
34.226.195.116 | SUCCESS => {
  “ansible_facts”: {
    “discovered_interpreter_python”: “/usr/bin/python3”
  },
  “changed”: false,
  “gid”: 1001,
  “name”: “deploy”,
  “state”: “present”,
  “system”: false
}
54.174.239.89 | SUCCESS => {
  “ansible_facts”: {
    “discovered_interpreter_python”: “/usr/bin/python3”
  },
  “changed”: false,
  “gid”: 1001,
  “name”: “deploy”,
  “state”: “present”,
  “system”: false
}
54.85.203.183 | SUCCESS => {
  “ansible_facts”: {
    “discovered_interpreter_python”: “/usr/bin/python3”
  },
  “changed”: false,
  “gid”: 1001,
  “name”: “deploy”,
  “state”: “present”,
  “system”: false
}
root@ip-172-31-24-147:~# more /etc/group | grep deploy
deploy:x:1001:
deploydfhh:x:1002:

2
root@ip-172-31-24-147:~# ansible all -i inventory -m user -a “name=deploy-user groups=deploy state=present shell=/bin/bash” -u ubuntu –key-file=node.pem -b 54.174.239.89 | SUCCESS => {
  “ansible_facts”: {
    “discovered_interpreter_python”: “/usr/bin/python3”
  },
  “append”: false,
  “changed”: false,
  “comment”: “”,
  “group”: 1001,
  “groups”: “deploy”,
  “home”: “/home/deploy-user”,
  “move_home”: false,
  “name”: “deploy-user”,
  “shell”: “/bin/bash”,
  “state”: “present”,
  “uid”: 1001
}
34.226.195.116 | SUCCESS => {
  “ansible_facts”: {
    “discovered_interpreter_python”: “/usr/bin/python3”
  },
  “append”: false,
  “changed”: false,
  “comment”: “”,
  “group”: 1001,
  “groups”: “deploy”,
  “home”: “/home/deploy-user”,
  “move_home”: false,
  “name”: “deploy-user”,
  “shell”: “/bin/bash”,
  “state”: “present”,
  “uid”: 1001
}
54.85.203.183 | SUCCESS => {
  “ansible_facts”: {
    “discovered_interpreter_python”: “/usr/bin/python3”
  },
  “append”: false,
  “changed”: false,
  “comment”: “”,
  “group”: 1001,
  “groups”: “deploy”,
  “home”: “/home/deploy-user”,
  “move_home”: false,
  “name”: “deploy-user”,
  “shell”: “/bin/bash”,
  “state”: “present”,
  “uid”: 1001
}
root@ip-172-31-24-147:~# more /etc/passwd | grep deploy-user
deploy-user:x:1001:1001::/home/deploy-user:/bin/bash

3
root@ip-172-31-24-147:~# ansible web -i inventory -m apt -a”state=latest name=httpd” -u ubuntu –key-file=node.pem -b
54.174.239.89 | FAILED! => {
  “ansible_facts”: {
    “discovered_interpreter_python”: “/usr/bin/python3”
  },
  “cache_update_time”: 1695090101,
  “cache_updated”: false,
  “changed”: false,
  “msg”: “‘/usr/bin/apt-get -y -o \”Dpkg::Options::=–force-confdef\” -o \”Dpkg::Options::=–force-confold\”    install ‘httpd” failed: E: Package ‘httpd’ has no installation candidate\n”,
  “rc”: 100,
  “stderr”: “E: Package ‘httpd’ has no installation candidate\n”,
  “stderr_lines”: [
    “E: Package ‘httpd’ has no installation candidate”
  ],
  “stdout”: “Reading package lists…\nBuilding dependency tree…\nReading state information…\nPackage httpd is a virtual package provided by:\n nginx-core 1.18.0-6ubuntu14.4\n apache2 2.4.52-1ubuntu4.6\n\n”,
  “stdout_lines”: [
    “Reading package lists…”,
    “Building dependency tree…”,
    “Reading state information…”,
    “Package httpd is a virtual package provided by:”,
    ” nginx-core 1.18.0-6ubuntu14.4″,
    ” apache2 2.4.52-1ubuntu4.6″,
    “”
  ]
}
54.85.203.183 | FAILED! => {
  “ansible_facts”: {
    “discovered_interpreter_python”: “/usr/bin/python3”
  },
  “cache_update_time”: 1698931978,
  “cache_updated”: false,
  “changed”: false,
  “msg”: “‘/usr/bin/apt-get -y -o \”Dpkg::Options::=–force-confdef\” -o \”Dpkg::Options::=–force-confold\”    install ‘httpd” failed: E: Package ‘httpd’ has no installation candidate\n”,
  “rc”: 100,
  “stderr”: “E: Package ‘httpd’ has no installation candidate\n”,
  “stderr_lines”: [
    “E: Package ‘httpd’ has no installation candidate”
  ],
  “stdout”: “Reading package lists…\nBuilding dependency tree…\nReading state information…\nPackage httpd is a virtual package provided by:\n nginx-light 1.18.0-6ubuntu14.4\n nginx-full 1.18.0-6ubuntu14.4\n nginx-extras 1.18.0-6ubuntu14.4\n lighttpd 1.4.63-1ubuntu3.1\n nginx-core 1.18.0-6ubuntu14.4\n apache2 2.4.52-1ubuntu4.6\n yaws 2.1.1+dfsg-1\n webfs 1.21+ds1-12\n tntnet 2.2.1-4build2\n mini-httpd 1.30-2build1\n micro-httpd 20140814-2.1\n\n”,
  “stdout_lines”: [
    “Reading package lists…”,
    “Building dependency tree…”,
    “Reading state information…”,
    “Package httpd is a virtual package provided by:”,
    ” nginx-light 1.18.0-6ubuntu14.4″,
    ” nginx-full 1.18.0-6ubuntu14.4″,
    ” nginx-extras 1.18.0-6ubuntu14.4″,
    ” lighttpd 1.4.63-1ubuntu3.1″,
    ” nginx-core 1.18.0-6ubuntu14.4″,
    ” apache2 2.4.52-1ubuntu4.6″,
    ” yaws 2.1.1+dfsg-1″,
    ” webfs 1.21+ds1-12″,
    ” tntnet 2.2.1-4build2″,
    ” mini-httpd 1.30-2build1″,
    ” micro-httpd 20140814-2.1″,
    “”
  ]
}
root@ip-172-31-24-147:~# ansible web -i inventory -m apt -a”state=latest name=apache2″ -u ubuntu –key-file=node.pem -b
54.174.239.89 | SUCCESS => {
  “ansible_facts”: {
    “discovered_interpreter_python”: “/usr/bin/python3”
  },
  “cache_update_time”: 1695090101,
  “cache_updated”: false,
  “changed”: false
}
54.85.203.183 | SUCCESS => {
  “ansible_facts”: {
    “discovered_interpreter_python”: “/usr/bin/python3”
  },
  “cache_update_time”: 1698931978,
  “cache_updated”: false,
  “changed”: false
}
root@ip-172-31-24-147:~# which apache2
/usr/sbin/apache2

4
ansible all -i inventory -m service -a”name=apache2 state=started” -u ubuntu –key-file=node.pem -b
root@ip-172-31-24-147:~# ps -fea| grep http
root    17682  7732 35 16:06 pts/33  00:00:00 /usr/bin/python3 /usr/bin/ansible all -i 54.85.203.183,54.174.239.89,34.226.195.116 -m apt -a name=httpd>=2.4 state=latest -u ubuntu –key=node.pem -b
root    17686  17682 1 16:06 pts/33  00:00:00 /usr/bin/python3 /usr/bin/ansible all -i 54.85.203.183,54.174.239.89,34.226.195.116 -m apt -a name=httpd>=2.4 state=latest -u ubuntu –key=node.pem -b
root    17748  17737 0 16:06 pts/27  00:00:00 /usr/bin/apt-mark manual httpd
root    17752  5990 0 16:06 pts/24  00:00:00 grep –color=auto http

5
ansible all -i inventory -m copy -a”src=index.html dest=/var/www/html/index.html” -u ubuntu –key-file=node.pem -b

root@ip-172-31-24-147:~# ansible web -i inventory -m apt -a”state=latest name=git” -u ubuntu –key-file=node.pem -b
54.174.239.89 | SUCCESS => {
  “ansible_facts”: {
    “discovered_interpreter_python”: “/usr/bin/python3”
  },
  “cache_update_time”: 1695090101,
  “cache_updated”: false,
  “changed”: false
}
54.85.203.183 | SUCCESS => {
  “ansible_facts”: {
    “discovered_interpreter_python”: “/usr/bin/python3”
  },
  “cache_update_time”: 1698931978,
  “cache_updated”: false,
  “changed”: false
}
root@ip-172-31-24-147:~# ansible web -i inventory -m apt -a”state=latest name=wget” -u ubuntu –key-file=node.pem -b
54.174.239.89 | SUCCESS => {
  “ansible_facts”: {
    “discovered_interpreter_python”: “/usr/bin/python3”
  },
  “cache_update_time”: 1695090101,
  “cache_updated”: false,
  “changed”: false
}
54.85.203.183 | SUCCESS => {
  “ansible_facts”: {
    “discovered_interpreter_python”: “/usr/bin/python3”
  },
  “cache_update_time”: 1698931978,
  “cache_updated”: false,
  “changed”: false
}
root@ip-172-31-24-147:~# which git
/usr/bin/git
root@ip-172-31-24-147:~# which wget
/usr/bin/wget
root@ip-172-31-24-147:~#

Jacek Szałęga
Jacek Szałęga
1 year ago

Tested on Ubuntu OS.
ansible all -i inventory -m group -a”name=deploy state=present” -u ubuntu –key-file=node.pem -b
ansible all -i inventory -m user -a”name=deploy-user group=deploy shell=/bin/bash” -u ubuntu –key-file=node.pem -b
ansible web -i inventory -m apt -a”state=latest name=apache2″ -u ubuntu –key-file=node.pem -b
ansible web -i inventory -m service -a”name=apache2 state=started” -u ubuntu –key-file=node.pem -b
ansible web -i inventory -m copy -a”src=index.html dest=/var/www/html/” -u ubuntu –key-file=node.pem -b
ansible web -i inventory -m copy -a”content=’second file content’ dest=/var/www/html/second.html” -u ubuntu –key-file=node.pem -b
ansible web -i inventory -m apt -a”state=present name=git,wget” -u ubuntu –key-file=node.pem -b
ansible web -i inventory -m git -a”repo=https://github.com/scmgalaxy/ansible-role-template dest=/root/ansible-role-template clone=yes update=yes” -u ubuntu –key-file=node.pem -b
ansible web -i inventory -m reboot -u ubuntu –key-file=node.pem -b
ansible web -i inventory -m file -a”path=/opt/devopsschool.txt state=touch” -u ubuntu –key-file=node.pem -b
ansible web -i inventory -m file -a”path=/opt/devopsschool.txt state=absent” -u ubuntu –key-file=node.pem -b

trackback
Ansible Assginment - May/2022 - Adhoc Commands - Playbook - Inventory - DevOps - DevSecOps - SRE - DataOps - AIOps
2 years ago

[…] Ansible Adhoc Commands Lab & Excercise – Part 1 Ansible Inventory Lab & Excercise – Part 1 Ansible Playbook Lab & Excercise – Part 2 Search for: […]

3
0
Would love your thoughts, please comment.x
()
x