Sunday, February 20, 2022

Ansible - Ad-hoc Command Assignment

 Create a setup of 3 ubuntu Servers where one of the servers is the Master server where Ansible is installed whereas the other 2 webservers are managed nodes.

Perform the below tasks by using Ansible Adhoc commands.

1. Install Apache server on  webservers using shell module

2. Stop Apache service on webservers using service module

3. Restart Apache service on webservers using  service module.

4. Uninstall Apache service using apt module

5. Run date command on webservers.

6. Create a file call app.java under /tmp/java folder

7. Add some content to app.java file.

8. Read app.java file using shell module.

9. Delete app.java file.

10. Install tree command using apt module. 

20 comments:

Naveen said...

1. ansible webservers -m shell -a "apt install apache2 -y"
2. ansible webservers -m service -a "name=apache2 state=stopped"
3. ansible webservers -m service -a "name=apache2 state=restarted"
4. ansible webservers -m apt -a "name=apache2 state=absent purge=true"
5. ansible webservers -m shell -a "date"
6. ansible webservers -m shell -a "mkdir /tmp/java"
ansible webservers -m shell -a "touch /tmp/java/app.java"
7. ansible webservers -m copy -a "content='Hello World' dest=/tmp/java/app.java"
8. ansible webservers -m shell -a "cat /tmp/java/app.java"
9. ansible webservers -m shell -a "rm /tmp/java/app.java"
10. ansible webservers -m apt -a "name=tree state=present"

Chin YZ said...

1. ansible webservers -m shell -a "apt install apache2 -y"
2. ansible webservers -m service -a "name=apache2 state=stopped"
3. ansible webservers -m service -a "name=apache2 state=restarted"
4. ansible webservers -m service -a "name=apache2 state=absent purge=yes"
5. ansible webservers -m shell -a "date"
6. ansible webservers -m shell -a "mkdir /tmp/java && touch /tmp/java/app.java"
7. ansible webservers -m shell -a "echo “add some content to app.java” >> /tmp/java/app.java"
8. ansible webservers -m shell -a "cat /tmp/java/app.java”
9. ansible webservers -m shell -a "rm /tmp/java/app.java”
10. ansible webservers -m apt -a "name=tree state=present”

Unknown said...

ans1:

ansible all -m shell -a "apt install apache2 -y"

ans2:ansible webservers -m service -a "name=apache2 state=stopped"

ans3:ansible webservers -m service -a "name=apache2 state=restarted"

ans4:ansible webservers -m apt -a "name=apache2 state=absent purge=yes"

ans5:ansible webservers -m shell -a "$date"

ans6:ansible all -m shell -a "touch /tmp/app.java"

ans7:ansible all -m shell -a "echo 'whatssapp' >> /tmp/app.java"

ans8:ansible all -m shell -a "cat /tmp/app.java"

ans9:ansible all -m shell -a "rm /tmp/app.java"

ans10: ansible all -m shell -a "sudo apt install tree"

Unknown said...

ken

Pong said...

Submitted: pong

prerequisite: create the master virtual machine and child nodes

ssh-keygen
cat ~/.ssh/id_rsa.pub
sudo vi /root/.ssh/authorized_keys

sudo vi /etc/ansible/hosts #& include the following

[dbservers]

192.168.33.10



[webservers]

192.168.33.11

192.168.33.12

Then,

sudo vi /etc/ansible/ansible.cfg
Remove '#' from '#inventory = /etc/ansible/hosts'

1.
ansible webservers -m shell -a " apt install apache2 -y"

2.
ansible webservers -m service -a "name=apache2 state=started"
ansible webservers -m service -a "name=apache2 state=stopped"

3.
ansible webservers -m service -a "name=apache2 state=restarted"

4.
ansible webservers -m apt -a "name=apache2 state=absent purge=yes"

5.
ansible webservers -m shell -a "date=true"

6.
ansible webservers,dbservers -m shell -a "touch app.java /tmp/java"

7.
ansible webservers,dbservers -m copy -a "content='Hello World' dest=/tmp/java/app.java"

8.
ansible webservers,dbservers -m shell -a "cat /tmp/java/app.java"

9.
ansible webservers,dbservers -m shell -a "remove app.java /tmp/java/app.java"

10.

ansible webservers -m apt -a "name=tree state=present"

Unknown said...

Mikraj

1. Install Apache server on webservers using shell module
ansible webservers -m shell -a "apt install apache2 -y"

2. Stop Apache service on webservers using service module
ansible webservers -m service -a "name=apache2 state=stopped"

3. Restart Apache service on webservers using service module.
ansible webservers -m service -a "name=apache2 state=restarted"

4. Uninstall Apache service using apt module
ansible webservers -m apt -a "name=apache2 state=absent purge=yes"


5. Run date command on webservers.
ansible webservers -m shell -a "date"

6. Create a file call app.java under /tmp/java folder
ansible webservers -m shell -a "mkdir /tmp/java/"
ansible webservers -m shell -a "touch app.java /tmp/java"

7. Add some content to app.java file.
ansible webservers -m shell -a "echo 'hello world' > app.java"

8. Read app.java file using shell module.
ansible webservers -m shell -a "cat app.java"

9. Delete app.java file.
ansible webservers -m shell -a "rm -ifr app.java"

10. Install tree command using apt module.
ansible webservers -m apt -a "name=tree state=present"

Jerome said...

1. Install Apache server on  webservers using shell module
- On Master server, > ansible webservers -m shell -a "apt install apache2 -y"
- To check, run on both managed nodes > systemctl status apache2
- Active: active (running)

2. Stop Apache service on webservers using service module
- On Master server, > ansible webservers -m service -a "name=apache2 state=stopped"
- To check, run on both managed nodes > systemctl status apache2
- Active: inactive (dead)

3. Restart Apache service on webservers using  service module.
- On Master server, > ansible webservers -m service -a "name=apache2 state=reloaded"
- To check, run on both managed nodes > systemctl status apache2
- Active: active (running)

4. Uninstall Apache service using apt module
- On Master server, > ansible webservers -m apt -a "name=apache2 state=absent purge=yes"
- To check, run on both managed nodes > systemctl status apache2
- Unit apache2.service could not be found.

5. Run date command on webservers.
- On both managed nodes > date
- Mon Feb 21 06:41:32 UTC 2022

6. Create a file call app.java under /tmp/java folder
- mkdir /tmp/java
- touch /tmp/java app.java

7. Add some content to app.java file.
- echo "Hello there" >> /tmp/java/app.java

8. Read app.java file using shell module.
- ansible dbservers -m shell -a "cat /tmp/java/app.java"

9. Delete app.java file.
- rm /tmp/java/app.java

10. Install tree command using apt module. 
- ansible dbservers -m apt -a "name=tree state=present"

Raman said...

Excellent

Asyraf said...

Asyraf

1. Install Apache server on webservers using shell module

ansible webservers -m shell -a " apt install apache2 -y"

2. Stop Apache service on webservers using service module

ansible webservers -m service -a "name=apache2 state=stopped"

3. Restart Apache service on webservers using service module.

ansible webservers -m service -a "name=apache2 state=stopped"

4. Uninstall Apache service using apt module

ansible webservers -m apt -a "name=apache2 state=absent purge=yes"

5. Run date command on webservers.

ansible webservers,dbservers -m shell -a "date"

6. Create a file call app.java under /tmp/java folder

ansible webservers,dbservers -m shell -a "mkdir /tmp/javafolder"

ansible webservers -m shell -a "touch /tmp/javafolder/app.java"

7. Add some content to app.java file.

ansible webservers -m copy -a "content='Hello Ansible' dest=/tmp/javafolder/app.java"

8. Read app.java file using shell module.

ansible webservers -m shell -a "cat /tmp/javafolder/app.java"

9. Delete app.java file.

ansible webservers -m shell -a "rm /tmp/javafolder/app.java"

10. Install tree command using apt module.

ansible webservers -m apt -a "name=tree state=present"

Raman said...

Excellent

Raman said...

Great work

Muhd Irfan Abdul Rahman said...

Answer:

1. ansible webservers -m shell -a "apt install apache2 -y"
2. ansible webservers -m service -a "name=apache2 state=stopped"
3. ansible webservers -m service -a "name=apache2 state=restarted"
4. ansible webservers -m apt -a "name=apache2 state=absent purge=yes"
5. ansible webservers -m shell -a "date"
Answer for 6:
6.1 ansible webservers -m shell -a "mkdir /tmp/java/"
6.2 ansible webservers -m shell -a "touch app.java /tmp/java"
7. ansible webservers -m copy -a "content='Hello World' dest=/tmp/java/app.java"
8. ansible webservers -m shell -a "cat /tmp/java/app.java"
9. ansible webservers -m shell -a "rm -ifr /tmp/java/app.java"
10. ansible webservers -m apt -a "name=tree state=present"

Raman said...

Great

Raman said...

Great

Unknown said...

tree: ans 10ansible webservers -m apt -a "name=tree state=present"

Raman said...

Great Work

Asyraf said...

3. ansible webservers -m service -a "name=apache2 state=restarted"

Raman said...

Excellent

Raman said...

Excellent

kp said...

1. ansible webservers -m shell -a "apt install apache2 -y"
2. ansible webservers -m service -a "name=apache2 state=started"
ansible webservers -m service -a "name=apache2 state=stopped"
3.ansible webservers -m service -a "name=apache2 state=restarted"
4.ansible webservers -m shell -a "apt purge apache2 -y"
5.ansible webservers -m shell -a "date"
6. ansible webservers -m shell -a "mkdir /tmp/java"
ansible webservers -m shell -a "touch /tmp/java/app.java"
7. ansible webservers -m shell -a "echo "hello" >> /tmp/java/app.java"
8. ansible webservers -m shell -a "cat /tmp/java/app.java"
9. ansible webservers -m shell -a "rm /tmp/java/app.java"
10.ansible webservers -m shell -a "apt install tree -y"

Post a Comment