Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------------------------------------------------- Task 1-------------------------------------------
- #### Inventory ####
- [ec2-user@ip-172-31-10-28 ~]$ cat inventory
- [amazon]
- 172.31.5.81 ansible_port=22 ansible_ssh_user="ec2-user" ansible_ssh_private_key_file="aws.pem"
- [ubuntu]
- 172.31.0.96 ansible_port=22 ansible_ssh_user="ubuntu" ansible_ssh_private_key_file="aws.pem"
- [ec2-user@ip-172-31-10-28 ~]$ echo "The SQL database running on the server is {{ package }} version {{ version }}">myfile.txt
- [ec2-user@ip-172-31-10-28 ~]$
- [ec2-user@ip-172-31-10-28 ~]$
- [ec2-user@ip-172-31-10-28 ~]$ cat myfile.txt
- The SQL database running on the server is {{ package }} version {{ version }}
- [ec2-user@ip-172-31-10-28 ~]$
- [ec2-user@ip-172-31-10-28 ~]$
- [ec2-user@ip-172-31-10-28 ~]$
- #### Copy and template module running in localhost ####
- [ec2-user@ip-172-31-10-28 ~]$ cat copy.yml
- ---
- - name: "Copying files"
- hosts: localhost
- vars:
- package: mysql-server
- version: 5.8
- tasks:
- - name: "Copying file using the copy module"
- copy:
- src: ./myfile.txt
- dest: /tmp/copy.txt
- - name: "Copying files using the template module"
- template:
- src: ./myfile.txt
- dest: /tmp/template.txt
- [ec2-user@ip-172-31-10-28 ~]$ ansible-playbook copy.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 [Copying files] ***************************************************************************************************
- TASK [Gathering Facts] *************************************************************************************************
- ok: [localhost]
- TASK [Copying file using the copy module] ******************************************************************************
- changed: [localhost]
- TASK [Copying files using the template module] *************************************************************************
- changed: [localhost]
- PLAY RECAP *************************************************************************************************************
- localhost : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- [ec2-user@ip-172-31-10-28 ~]$
- [ec2-user@ip-172-31-10-28 ~]$
- [ec2-user@ip-172-31-10-28 ~]$
- [ec2-user@ip-172-31-10-28 ~]$ cat /tmp/copy.txt
- The SQL database running on the server is {{ package }} version {{ version }}
- [ec2-user@ip-172-31-10-28 ~]$
- [ec2-user@ip-172-31-10-28 ~]$
- [ec2-user@ip-172-31-10-28 ~]$ cat /tmp/template.txt
- The SQL database running on the server is mysql-server version 5.8
- [ec2-user@ip-172-31-10-28 ~]$
- #### Copy module with remote_src ####
- ---
- - name: "Copying files"
- hosts: amazon
- vars:
- package: mysql-server
- version: 5.8
- tasks:
- - name: "Copying file using the copy module"
- copy:
- src: /etc/os-release
- dest: /tmp
- remote_src: true
- ec2-user@ip-172-31-10-28 ~]$ ansible-playbook -i inventory copy.yml
- PLAY [Copying files] ***************************************************************************************************
- TASK [Gathering Facts] *************************************************************************************************
- ok: [172.31.5.81]
- TASK [Copying file using the copy module] ******************************************************************************
- ok: [172.31.5.81]
- PLAY RECAP *************************************************************************************************************
- 172.31.5.81 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- [ec2-user@ip-172-31-10-28 ~]$ ansible -i inventory amazon -m command -a "cat /tmp/os-release"
- 172.31.5.81 | CHANGED | rc=0 >>
- NAME="Amazon Linux"
- VERSION="2023"
- ID="amzn"
- ID_LIKE="fedora"
- VERSION_ID="2023"
- PLATFORM_ID="platform:al2023"
- PRETTY_NAME="Amazon Linux 2023"
- ANSI_COLOR="0;33"
- CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2023"
- HOME_URL="https://aws.amazon.com/linux/"
- BUG_REPORT_URL="https://github.com/amazonlinux/amazon-linux-2023"
- SUPPORT_END="2028-03-01"
- -------------------------------------------------------------- Task 2 -------------------------------------------
- [ec2-user@ip-172-31-10-28 ~]$ echo "<h1><center>Hello World! This is running in version 1 </h1></center>">index.html
- [ec2-user@ip-172-31-10-28 ~]$
- [ec2-user@ip-172-31-10-28 ~]$
- [ec2-user@ip-172-31-10-28 ~]$ cat index.html
- <h1><center>Hello World! This is running in version 1 </h1></center>
- [ec2-user@ip-172-31-10-28 ~]$
- #### without condition ###
- [ec2-user@ip-172-31-10-28 ~]$
- ---
- - name: "Apache installation"
- hosts: amazon
- become: true
- vars:
- httpd_owner: apache
- httpd_group: apache
- tasks:
- - name: "Apache installation"
- yum:
- name: httpd
- state: present
- - name: "Copying files to the document root"
- copy:
- src: ./index.html
- dest: /var/www/html/
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- - name: "Servic restart with condition"
- service:
- name: httpd
- state: restarted
- enabled: true
- [ec2-user@ip-172-31-10-28 ~]$ ansible-playbook -i inventory register.yml
- PLAY [Apache installation] *********************************************************************************************
- TASK [Gathering Facts] *************************************************************************************************
- ok: [172.31.5.81]
- TASK [Apache installation] *********************************************************************************************
- changed: [172.31.5.81]
- TASK [Copying files to the document root] ******************************************************************************
- changed: [172.31.5.81]
- TASK [Servic restart with condition] ***********************************************************************************
- changed: [172.31.5.81]
- PLAY RECAP *************************************************************************************************************
- 172.31.5.81 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- #### with condition ###
- - name: "Apache installation"
- hosts: amazon
- become: true
- vars:
- httpd_owner: apache
- httpd_group: apache
- tasks:
- - name: "Apache installation"
- yum:
- name: httpd
- state: present
- - name: "Copying files to the document root"
- copy:
- src: ./index.html
- dest: /var/www/html/
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- register: copy_status
- - name: "Servic restart with condition"
- when: copy_status.changed == true
- service:
- name: httpd
- state: restarted
- enabled: true
- [ec2-user@ip-172-31-10-28 ~]$ echo "<h1><center>Hello World! This is running in version 2 </h1></center>">index.html
- #### Condition triggers when register status changed ####
- [ec2-user@ip-172-31-10-28 ~]$ ansible-playbook -i inventory register.yml
- PLAY [Apache installation] *********************************************************************************************
- TASK [Gathering Facts] *************************************************************************************************
- ok: [172.31.5.81]
- TASK [Apache installation] *********************************************************************************************
- ok: [172.31.5.81]
- TASK [Copying files to the document root] ******************************************************************************
- changed: [172.31.5.81]
- TASK [Servic restart with condition] ***********************************************************************************
- changed: [172.31.5.81]
- PLAY RECAP *************************************************************************************************************
- 172.31.5.81 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- #### Condition triggers when register status= false and service tasked skipping ####
- [ec2-user@ip-172-31-10-28 ~]$ ansible-playbook -i inventory register.yml
- PLAY [Apache installation] *********************************************************************************************
- TASK [Gathering Facts] *************************************************************************************************
- ok: [172.31.5.81]
- TASK [Apache installation] *********************************************************************************************
- ok: [172.31.5.81]
- TASK [Copying files to the document root] ******************************************************************************
- ok: [172.31.5.81]
- TASK [Servic restart with condition] ***********************************************************************************
- skipping: [172.31.5.81]
- PLAY RECAP *************************************************************************************************************
- 172.31.5.81 : ok=3 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
- -------------------------------------------------------------- Task 3 -------------------------------------------
- [[ec2-user@ip-172-31-10-28 ~]$ cat <<'EOF' > index.php
- <?php
- $hostname = shell_exec('echo $HOSTNAME');
- echo "<h1><center> $hostname </h1></center>";
- echo "<h1><center> Zomato App - Version 1 </h1></center>";
- ?>
- EOF
- #### fetch module ###
- [ec2-user@ip-172-31-10-28 ~]$ ansible -i inventory amazon -m fetch -a "src=/etc/httpd/conf/httpd.conf dest=./ flat=true"
- 172.31.5.81 | CHANGED => {
- "changed": true,
- "checksum": "fcfc3261dbe43ccf2a521213571849cf36e277c0",
- "dest": "/home/ec2-user/httpd.conf",
- "md5sum": "abc2d98f55602c033626f046ce72fffc",
- "remote_checksum": "fcfc3261dbe43ccf2a521213571849cf36e277c0",
- "remote_md5sum": null
- }
- #### Playbook ###
- ---
- - name: "Apache and php installation"
- hosts: amazon
- become: true
- vars:
- packages:
- - httpd
- - php
- httpd_owner: apache
- httpd_group: apache
- httpd_port: 80
- Docphp: index.php
- Dochtml: index.html
- tasks:
- - name: "Apache and php installation"
- yum:
- name: "{{ packages }}"
- state: present
- register: packages_status
- - name: "httpd conf file copy"
- template:
- src: ./httpd.conf.tmpl
- dest: /etc/httpd/conf/httpd.conf
- register: conf_status
- - name: "Copying index.php"
- copy:
- src: ./index.php
- dest: /var/www/html
- register: index_status
- - name: "Retsarting and enabling php-fpm if condition equals to true"
- when: packages_status.changed == true or index_status.changed == true
- service:
- name: php-fpm
- state: restarted
- enabled: true
- - name: "Retsarting and enabling httpd if condition equals to true"
- when: packages_status.changed == true or conf_status.changed == true
- service:
- name: httpd
- state: restarted
- enabled: true
- ### made changes to httpd.conf via addind document root index with the variables ###
- #Listen 12.34.56.78:80
- Listen {{ httpd_port }}
- <IfModule dir_module>
- DirectoryIndex {{ Docphp }} {{ Dochtml }}
- </IfModule>
- #### update the port to 8080 and index.php to version2 ###
- [ec2-user@ip-172-31-10-28 ~]$ ansible-playbook -i inventory httpd.yml
- PLAY [Apache and php installation] *************************************************************************************
- TASK [Gathering Facts] *************************************************************************************************
- ok: [172.31.5.81]
- TASK [Apache and php installation] *************************************************************************************
- ok: [172.31.5.81]
- TASK [httpd conf file copy] ********************************************************************************************
- changed: [172.31.5.81]
- TASK [Copying index.php] ***********************************************************************************************
- changed: [172.31.5.81]
- TASK [Retsarting and enabling php-fpm if condition equals to true] *****************************************************
- changed: [172.31.5.81]
- TASK [Retsarting and enabling httpd if condition equals to true] *******************************************************
- changed: [172.31.5.81]
- PLAY RECAP *************************************************************************************************************
- 172.31.5.81 : ok=6 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- -------------------------------------------------------------- Task 4 -------------------------------------------
- [ec2-user@ip-172-31-10-28 ~]$ ansible -i inventory ubuntu -m fetch -a "src=/etc/nginx/sites-enabled/default dest=./default.conf flat=true"
- 172.31.0.96 | CHANGED => {
- "changed": true,
- "checksum": "e042f85b2efaf32b539247298d66d9bd5a40068f",
- "dest": "/home/ec2-user/default.conf",
- "md5sum": "f1f26aef86f90a484f3a2f46ccc46ff6",
- "remote_checksum": "e042f85b2efaf32b539247298d66d9bd5a40068f",
- "remote_md5sum": null
- }
- [ec2-user@ip-172-31-10-28 ~]$
- [ec2-user@ip-172-31-10-28 ~]$
- [ec2-user@ip-172-31-10-28 ~]$ ls -l default.conf
- -rw-r--r--. 1 ec2-user ec2-user 2412 Jun 22 11:44 default.conf
- [ec2-user@ip-172-31-10-28 ~]$ ansible -i inventory ubuntu -b -m apt -a "name=nginx,nginx-common,nginx-core state=absent purge=true"
- 172.31.0.96 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/bin/python3"
- },
- "changed": true,
- "stderr": "",
- "stderr_lines": [],
- ### Playbook ###
- ---
- - name: "Nginx installation in ubuntu OS"
- hosts: ubuntu
- become: true
- vars:
- nginx_port: 8080
- nginx_user: www-data
- nginx_group: www-data
- tasks:
- - name: "Nginx installation"
- apt:
- name: nginx
- state: present
- update_cache: true
- - name: "Copying conf to remote hosts"
- template:
- src: ./default.conf
- dest: /etc/nginx/sites-enabled/default
- register: nginxconf_status
- - name: "Copying index.html to DOC root"
- copy:
- src: ./index.html
- dest: /var/www/html/
- owner: "{{ nginx_user }}"
- group: "{{ nginx_group }}"
- - name: "Nginx service restart when condition meets"
- when: nginxconf_status.changed == true
- service:
- name: nginx
- state: restarted
- enabled: true
- #### Output ####
- [ec2-user@ip-172-31-10-28 ~]$ ansible-playbook -i inventory nginx.yml
- PLAY [Nginx installation in ubuntu OS] *********************************************************************************************************************
- TASK [Gathering Facts] *************************************************************************************************************************************
- ok: [172.31.0.96]
- TASK [Nginx installation] **********************************************************************************************************************************
- changed: [172.31.0.96]
- TASK [Copying conf to remote hosts] ************************************************************************************************************************
- changed: [172.31.0.96]
- TASK [Copying index.html to DOC root] **********************************************************************************************************************
- changed: [172.31.0.96]
- TASK [Nginx service restart when condition meets] **********************************************************************************************************
- changed: [172.31.0.96]
- PLAY RECAP *************************************************************************************************************************************************
- 172.31.0.96 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Advertisement
Add Comment
Please, Sign In to add comment