Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------------------------------------------------- Task 1-------------------------------------------
- #### Inventory ####
- [ec2-user@ip-172-31-4-194 ~]$ cat inventory
- [amazon]
- 172.31.7.181 ansible_port=22 ansible_ssh_user="ec2-user" ansible_ssh_private_key_file="mumbai.pem"
- 172.31.13.111 ansible_port=22 ansible_ssh_user="ec2-user" ansible_ssh_private_key_file="mumbai.pem"
- [ubuntu]
- 172.31.14.172 ansible_port=22 ansible_ssh_user="ubuntu" ansible_ssh_private_key_file="mumbai.pem"
- [debian]
- 172.31.10.94 ansible_port=22 ansible_ssh_user="admin" ansible_ssh_private_key_file="mumbai.pem"
- #### Ping module ####
- [ec2-user@ip-172-31-4-194 ~]$ ansible -i inventory all -f 1 -m ping
- [WARNING]: Platform linux on host 172.31.7.181 is using the discovered Python interpreter at /usr/bin/python3.9, but
- future installation of another Python interpreter could change the meaning of that path. See
- https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more information.
- 172.31.7.181 | SUCCESS => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/bin/python3.9"
- },
- "changed": false,
- "ping": "pong"
- }
- [WARNING]: Platform linux on host 172.31.13.111 is using the discovered Python interpreter at /usr/bin/python3.9, but
- future installation of another Python interpreter could change the meaning of that path. See
- https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more information.
- 172.31.13.111 | SUCCESS => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/bin/python3.9"
- },
- "changed": false,
- "ping": "pong"
- }
- 172.31.14.172 | SUCCESS => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/bin/python3"
- },
- "changed": false,
- "ping": "pong"
- }
- 172.31.10.94 | SUCCESS => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/bin/python3"
- },
- "changed": false,
- "ping": "pong"
- }
- #### Playbook without when condition ####
- [ec2-user@ip-172-31-4-194 ~]$ cat apache-installation.yml
- ---
- - name: "Installing apache on various OS distributions"
- hosts: all
- become: true
- tasks:
- - name: "Apache install on amazon linux OS"
- yum:
- name: httpd
- state: present
- - name: "Apache service restart and enable"
- service:
- name: httpd
- state: restarted
- enabled: true
- - name: "Copying files to the Document root"
- copy:
- content: "<h1><center> Hello World! This is a apache webserver running in amazon OS</h1></center>"
- dest: /var/www/html/index.html
- owner: apache
- group: apache
- - name: "Apache install on ubuntu/debian OS"
- apt:
- name: apache2
- state: present
- update_cache: true
- - name: "Apache service restart and enable"
- service:
- name: apache2
- state: restarted
- enabled: true
- - name: "Copying files to the Document root"
- copy:
- content: "<h1><center> Hello World! This is a apache webserver running in ubuntu/debian OS</h1></center>"
- dest: /var/www/html/index.html
- owner: www-data
- group: www-data
- ##### Output ####
- [ec2-user@ip-172-31-4-194 ~]$ ansible-playbook -i inventory apache-installation.yml
- PLAY [Installing apache on various OS distributions] *******************************************************************
- TASK [Gathering Facts] *************************************************************************************************
- ok: [172.31.10.94]
- ok: [172.31.13.111]
- ok: [172.31.14.172]
- ok: [172.31.7.181]
- TASK [Apache install on amazon linux OS] *******************************************************************************
- fatal: [172.31.10.94]: FAILED! => {"ansible_facts": {"pkg_mgr": "apt"}, "changed": false, "msg": ["Could not detect which major revision of yum is in use, which is required to determine module backend.", "You should manually specify use_backend to tell the module whether to use the yum (yum3) or dnf (yum4) backend})"]}
- fatal: [172.31.14.172]: FAILED! => {"ansible_facts": {"pkg_mgr": "apt"}, "changed": false, "msg": ["Could not detect which major revision of yum is in use, which is required to determine module backend.", "You should manually specify use_backend to tell the module whether to use the yum (yum3) or dnf (yum4) backend})"]}
- changed: [172.31.13.111]
- changed: [172.31.7.181]
- TASK [Apache service restart and enable] *******************************************************************************
- changed: [172.31.13.111]
- changed: [172.31.7.181]
- TASK [Copying files to the Document root] ******************************************************************************
- changed: [172.31.7.181]
- changed: [172.31.13.111]
- TASK [Apache install on ubuntu/debian OS] ******************************************************************************
- [WARNING]: Updating cache and auto-installing missing dependency: python3-apt
- fatal: [172.31.7.181]: FAILED! => {"changed": false, "cmd": "apt-get update", "msg": "[Errno 2] No such file or directory: b'apt-get'", "rc": 2, "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
- fatal: [172.31.13.111]: FAILED! => {"changed": false, "cmd": "apt-get update", "msg": "[Errno 2] No such file or directory: b'apt-get'", "rc": 2, "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
- PLAY RECAP *************************************************************************************************************
- 172.31.10.94 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
- 172.31.13.111 : ok=4 changed=3 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
- 172.31.14.172 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
- 172.31.7.181 : ok=4 changed=3 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
- -------------------------------------------------------------- Task 2 -------------------------------------------
- #### Playbook with when condition ####
- ---
- - name: "Installing apache on various OS distributions"
- hosts: all
- become: true
- tasks:
- - name: "Apache install on amazon linux OS"
- when: ansible_os_family == "RedHat" and ansible_distribution == "Amazon"
- yum:
- name: httpd
- state: present
- - name: "Apache service restart and enable"
- when: ansible_os_family == "RedHat" and ansible_distribution == "Amazon"
- service:
- name: httpd
- state: restarted
- enabled: true
- - name: "Copying files to the Document root"
- when: ansible_os_family == "RedHat" and ansible_distribution == "Amazon"
- copy:
- content: "<h1><center> Hello World! This is a apache webserver running in amazon OS</h1></center>"
- dest: /var/www/html/index.html
- owner: apache
- group: apache
- - name: "Apache install on ubuntu/debian OS"
- when: ansible_os_family == "Debian" or ansible_distribution == "Ubuntu"
- apt:
- name: apache2
- state: present
- update_cache: true
- - name: "Apache service restart and enable"
- when: ansible_os_family == "Debian" or ansible_distribution == "Ubuntu"
- service:
- name: apache2
- state: restarted
- enabled: true
- - name: "Copying files to the Document root"
- when: ansible_os_family == "Debian" or ansible_distribution == "Ubuntu"
- copy:
- content: "<h1><center> Hello World! This is a apache webserver running in ubuntu/debian OS</h1></center>"
- dest: /var/www/html/index.html
- owner: www-data
- group: www-data
- #### Output ####
- [ec2-user@ip-172-31-4-194 ~]$ ansible-playbook -i inventory apache-installation.yml
- PLAY [Installing apache on various OS distributions] *******************************************************************
- TASK [Gathering Facts] *************************************************************************************************
- ok: [172.31.10.94]
- ok: [172.31.14.172]
- [WARNING]: Platform linux on host 172.31.13.111 is using the discovered Python interpreter at /usr/bin/python3.9, but
- future installation of another Python interpreter could change the meaning of that path. See
- https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more information.
- ok: [172.31.13.111]
- [WARNING]: Platform linux on host 172.31.7.181 is using the discovered Python interpreter at /usr/bin/python3.9, but
- future installation of another Python interpreter could change the meaning of that path. See
- https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more information.
- ok: [172.31.7.181]
- TASK [Apache install on amazon linux OS] *******************************************************************************
- skipping: [172.31.14.172]
- skipping: [172.31.10.94]
- ok: [172.31.7.181]
- ok: [172.31.13.111]
- TASK [Apache service restart and enable] *******************************************************************************
- skipping: [172.31.14.172]
- skipping: [172.31.10.94]
- changed: [172.31.7.181]
- changed: [172.31.13.111]
- TASK [Copying files to the Document root] ******************************************************************************
- skipping: [172.31.14.172]
- skipping: [172.31.10.94]
- ok: [172.31.7.181]
- ok: [172.31.13.111]
- TASK [Apache install on ubuntu/debian OS] ******************************************************************************
- skipping: [172.31.7.181]
- skipping: [172.31.13.111]
- changed: [172.31.14.172]
- changed: [172.31.10.94]
- TASK [Apache service restart and enable] *******************************************************************************
- skipping: [172.31.7.181]
- skipping: [172.31.13.111]
- changed: [172.31.10.94]
- changed: [172.31.14.172]
- TASK [Copying files to the Document root] ******************************************************************************
- skipping: [172.31.7.181]
- skipping: [172.31.13.111]
- changed: [172.31.10.94]
- changed: [172.31.14.172]
- PLAY RECAP *************************************************************************************************************
- 172.31.10.94 : ok=4 changed=3 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
- 172.31.13.111 : ok=4 changed=1 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
- 172.31.14.172 : ok=4 changed=3 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
- 172.31.7.181 : ok=4 changed=1 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
- [ec2-user@ip-172-31-4-194 ~]$ cat inventory
- [amazon]
- 172.31.7.181 ansible_port=22 ansible_ssh_user="ec2-user" ansible_ssh_private_key_file="mumbai.pem"
- 172.31.13.111 ansible_port=22 ansible_ssh_user="ec2-user" ansible_ssh_private_key_file="mumbai.pem"
- [ubuntu]
- 172.31.14.172 ansible_port=22 ansible_ssh_user="ubuntu" ansible_ssh_private_key_file="mumbai.pem"
- [debian]
- 172.31.10.94 ansible_port=22 ansible_ssh_user="admin" ansible_ssh_private_key_file="mumbai.pem"
- -------------------------------------------------------------- Task 3 -------------------------------------------
- #### Debug ####
- [ec2-user@ip-172-31-4-194 ~]$ cat debug.yml
- ---
- - name: "Debug and printing values"
- hosts: localhost
- vars:
- webserver: nginx
- version: 1.4
- tasks:
- - name: "Printing the webserver name"
- debug:
- var: webserver
- - name: "Printing the webserver version"
- debug:
- var: version
- - name: "Printing the webserver name and version"
- debug:
- msg: "The localhost is running on a {{ webserver }} webserver and its version is {{ version }}"
- - name: "Printing the facts of RAM used"
- debug:
- msg: "Total memory is {{ ansible_memtotal_mb }} and free memory is {{ ansible_memfree_mb }} "
- - name: "Printing the kernel version through facts"
- debug:
- msg: "The kernel version for the server running on is {{ ansible_kernel }}"
- - name: "Printing the fqdn of the server"
- debug:
- var: ansible_fqdn
- #### Output ###
- [ec2-user@ip-172-31-4-194 ~]$ ansible-playbook debug.yml
- [WARNING]: No inventory was parsed, only implicit localhost is available
- [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match
- 'all'
- PLAY [Debug and printing values] ***************************************************************************************
- TASK [Gathering Facts] *************************************************************************************************
- ok: [localhost]
- TASK [Printing the webserver name] *************************************************************************************
- ok: [localhost] => {
- "webserver": "nginx"
- }
- TASK [Printing the webserver version] **********************************************************************************
- ok: [localhost] => {
- "version": 1.4
- }
- TASK [Printing the webserver name and version] *************************************************************************
- ok: [localhost] => {
- "msg": "The localhost is running on a nginx webserver and its version is 1.4"
- }
- TASK [Printing the facts of RAM used] **********************************************************************************
- ok: [localhost] => {
- "msg": "Total memory is 949 and free memory is 125 "
- }
- TASK [Printing the kernel version through facts] ***********************************************************************
- ok: [localhost] => {
- "msg": "The kernel version for the server running on is 6.1.29-50.88.amzn2023.x86_64"
- }
- TASK [Printing the fqdn of the server] *********************************************************************************
- ok: [localhost] => {
- "ansible_fqdn": "ip-172-31-4-194.ap-south-1.compute.internal"
- }
- PLAY RECAP *************************************************************************************************************
- localhost : ok=7 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- #### Debug module with nested json and using when condition ####
- [ec2-user@ip-172-31-4-194 ~]$ cat debug-condition.yml
- ---
- - name: "Debug module with nested and condition"
- hosts: all
- tasks:
- - name: "Printing the nested values"
- debug:
- msg: "Total:{{ ansible_memory_mb.real.total }}MB Used:{{ ansible_memory_mb.real.used }}MB Free:{{ ansible_memory_mb.real.free }}MB"
- - name: "Printing all the RAM used"
- debug:
- var: ansible_memory_mb.real.used
- - name: "Printing all the RAM execeding 600 MB of usage"
- when: ansible_memory_mb.real.used >= 600
- debug:
- msg: "HOSTNAME:{{ ansible_fqdn }} MEM_USED:{{ ansible_memory_mb.real.used }}"
- ### Output ####
- [ec2-user@ip-172-31-4-194 ~]$ ansible-playbook -i inventory debug-condition.yml
- PLAY [Debug module with nested and condition] **************************************************************************************************************
- TASK [Gathering Facts] *************************************************************************************************************************************
- ok: [172.31.10.94]
- ok: [172.31.7.181]
- ok: [172.31.13.111]
- ok: [172.31.14.172]
- TASK [Printing the nested values] **************************************************************************************************************************
- ok: [172.31.7.181] => {
- "msg": "Total:949MB Used:859MB Free:90MB"
- }
- ok: [172.31.13.111] => {
- "msg": "Total:949MB Used:860MB Free:89MB"
- }
- ok: [172.31.14.172] => {
- "msg": "Total:965MB Used:803MB Free:162MB"
- }
- ok: [172.31.10.94] => {
- "msg": "Total:975MB Used:537MB Free:438MB"
- }
- TASK [Printing all the RAM used] ***************************************************************************************************************************
- ok: [172.31.7.181] => {
- "ansible_memory_mb.real.used": "859"
- }
- ok: [172.31.13.111] => {
- "ansible_memory_mb.real.used": "860"
- }
- ok: [172.31.14.172] => {
- "ansible_memory_mb.real.used": "803"
- }
- ok: [172.31.10.94] => {
- "ansible_memory_mb.real.used": "537"
- }
- TASK [Printing all the RAM execeding 600 MB of usage] ******************************************************************************************************
- ok: [172.31.7.181] => {
- "msg": "HOSTNAME:ip-172-31-7-181.ap-south-1.compute.internal MEM_USED:859"
- }
- ok: [172.31.13.111] => {
- "msg": "HOSTNAME:ip-172-31-13-111.ap-south-1.compute.internal MEM_USED:860"
- }
- ok: [172.31.14.172] => {
- "msg": "HOSTNAME:ip-172-31-14-172.ap-south-1.compute.internal MEM_USED:803"
- }
- skipping: [172.31.10.94]
- PLAY RECAP *************************************************************************************************************************************************
- 172.31.10.94 : ok=3 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
- 172.31.13.111 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- 172.31.14.172 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- 172.31.7.181 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- -------------------------------------------------------------- Task 4 -------------------------------------------
- #### Docker installation ####
- [ec2-user@ip-172-31-4-194 ~]$ cat docker.yml
- ---
- - name: "Docker installation"
- hosts: amazon
- become: true
- tasks:
- - name: "Docker installtion in amazon linux OS"
- yum:
- name: docker
- state: present
- - name: "Docker service start & enable"
- service:
- name: docker
- state: started
- enabled: true
- - name: "groupadd docker"
- user:
- name: ec2-user
- append: true
- groups: docker
- #### Output ####
- [ec2-user@ip-172-31-4-194 ~]$ ansible-playbook -i inventory docker.yml
- PLAY [Docker installation] *********************************************************************************************************************************
- TASK [Gathering Facts] *************************************************************************************************************************************
- [WARNING]: Platform linux on host 172.31.7.181 is using the discovered Python interpreter at /usr/bin/python3.9, but future installation of another Python
- interpreter could change the meaning of that path. See https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more
- information.
- ok: [172.31.7.181]
- [WARNING]: Platform linux on host 172.31.13.111 is using the discovered Python interpreter at /usr/bin/python3.9, but future installation of another Python
- interpreter could change the meaning of that path. See https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more
- information.
- ok: [172.31.13.111]
- TASK [Docker installtion in amazon linux OS] ***************************************************************************************************************
- changed: [172.31.13.111]
- changed: [172.31.7.181]
- TASK [Docker service start & enable] ***********************************************************************************************************************
- changed: [172.31.13.111]
- changed: [172.31.7.181]
- TASK [groupadd docker] *************************************************************************************************************************************
- changed: [172.31.13.111]
- changed: [172.31.7.181]
- PLAY RECAP *************************************************************************************************************************************************
- 172.31.13.111 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- 172.31.7.181 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- -------------------------------------------------------------- Task 5 -------------------------------------------
- ### adhoc command to remove apache2 ####
- [ec2-user@ip-172-31-4-194 ~]$ ansible -i inventory ubuntu,debian -b -m apt -a "name=apache2 state=absent"
- 172.31.10.94 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/bin/python3"
- },
- "changed": true,
- "stderr": "",
- "stderr_lines": [],
- 172.31.14.172 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/bin/python3"
- },
- "changed": true,
- "stderr": "",
- "stderr_lines": [],
- #### Playbook ####
- [ec2-user@ip-172-31-4-194 ~]$ cat nginx.yml
- ---
- - name: "Nginx installation on debian family"
- hosts: ubuntu,debian
- become: true
- tasks:
- - name: "Nginx installation"
- apt:
- name: nginx
- state: present
- update_cache: true
- - name: "Nginx start and enable"
- service:
- name: nginx
- state: restarted
- enabled: true
- - name: "Copying files to the document root"
- copy:
- content: "<h1><center> Hello World! The webserver is nginx and it is running in {{ ansible_os_family }} family with {{ ansible_distribution }} OS</h1></center>"
- dest: /var/www/html/index.html
- owner: www-data
- group: www-data
- #### Output ####
- [ec2-user@ip-172-31-4-194 ~]$ ansible-playbook -i inventory nginx.yml
- PLAY [Nginx installation on debian family] *****************************************************************************************************************
- TASK [Gathering Facts] *************************************************************************************************************************************
- ok: [172.31.10.94]
- ok: [172.31.14.172]
- TASK [Nginx installation] **********************************************************************************************************************************
- ok: [172.31.10.94]
- ok: [172.31.14.172]
- TASK [Nginx start and enable] ******************************************************************************************************************************
- changed: [172.31.14.172]
- changed: [172.31.10.94]
- TASK [Copying files to the document root] ******************************************************************************************************************
- changed: [172.31.10.94]
- changed: [172.31.14.172]
- PLAY RECAP *************************************************************************************************************************************************
- 172.31.10.94 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- 172.31.14.172 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Advertisement
Add Comment
Please, Sign In to add comment