Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------------------------------------------------- Task -------------------------------------------
- -----------------------Adhoc method ------------------------------
- ------- Inventory --------------
- [ec2-user@ip-172-31-11-206 ~]$ cat inventory
- [amazon]
- 172.31.7.158 ansible_port=22 ansible_ssh_user="ec2-user" ansible_ssh_private_key_file="aws.pem"
- [ubuntu]
- 172.31.3.223 ansible_port=22 ansible_ssh_user="ubuntu" ansible_ssh_private_key_file="aws.pem"
- [ec2-user@ip-172-31-11-206 ~]$
- ------- Ping module --------------
- [ec2-user@ip-172-31-11-206 ~]$ ansible -i inventory -m ping amazon
- The authenticity of host '172.31.7.158 (172.31.7.158)' can't be established.
- ED25519 key fingerprint is SHA256:iIE3RBdA5K9GXiqiuGSrsjl8Yy+YnRSizu5wUySLitY.
- This key is not known by any other names
- Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
- 172.31.7.158 | SUCCESS => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/bin/python3.9"
- },
- "changed": false,
- "ping": "pong"
- }
- [ec2-user@ip-172-31-11-206 ~]$
- [ec2-user@ip-172-31-11-206 ~]$
- [ec2-user@ip-172-31-11-206 ~]$ ansible -i inventory -m ping ubuntu
- The authenticity of host '172.31.3.223 (172.31.3.223)' can't be established.
- ED25519 key fingerprint is SHA256:pg1/KnS/g2w+2b2EaoZmMHS6V4Zyi8jCTFmPeOh8Xt0.
- This key is not known by any other names
- Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
- 172.31.3.223 | SUCCESS => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/bin/python3"
- },
- "changed": false,
- "ping": "pong"
- }
- --------- YUM module -----------
- --- error -----
- [ec2-user@ip-172-31-11-206 ~]$ ansible -i inventory amazon -m yum -a "name=httpd state=present"
- 172.31.7.158 | FAILED! => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/bin/python3.9"
- },
- "changed": false,
- "msg": "This command has to be run under the root user.",
- "results": []
- }
- ----Fix with become b flag------
- [ec2-user@ip-172-31-11-206 ~]$ ansible -i inventory amazon -b -m yum -a "name=httpd state=present"
- 172.31.7.158 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/bin/python3.9"
- },
- "changed": true,
- "msg": "",
- "rc": 0,
- ------------ apt module ------------
- [ec2-user@ip-172-31-11-206 ~]$ ansible -i inventory ubuntu -b -m apt -a "name=apache2 state=present"
- 172.31.3.223 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/bin/python3"
- },
- "cache_update_time": 1684202990,
- "cache_updated": false,
- "changed": true,
- "stderr": "",
- ----- state=absent ---------
- [ec2-user@ip-172-31-11-206 ~]$ ansible -i inventory ubuntu -b -m apt -a "name=apache2 state=absent"
- 172.31.3.223 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/bin/python3"
- },
- "changed": true,
- "stderr": "",
- ----- back to present ------
- [ec2-user@ip-172-31-11-206 ~]$ ansible -i inventory ubuntu -b -m apt -a "name=apache2 state=present"
- 172.31.3.223 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/bin/python3"
- },
- "cache_update_time": 1684202990,
- "cache_updated": false,
- "changed": true,
- ----- state=started -----
- [ec2-user@ip-172-31-11-206 ~]$ ansible -i inventory ubuntu -b -m service -a "name=apache2 state=started enabled=true"
- 172.31.3.223 | SUCCESS => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/bin/python3"
- },
- "changed": false,
- "enabled": true,
- "name": "apache2",
- "state": "started",
- ------- state=restarted ------
- [ec2-user@ip-172-31-11-206 ~]$ ansible -i inventory ubuntu -b -m service -a "name=apache2 state=restarted enabled=true"
- 172.31.3.223 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/bin/python3"
- },
- "changed": true,
- "enabled": true,
- "name": "apache2",
- "state": "started",
- ---------------- Service module ----------------
- [ec2-user@ip-172-31-11-206 ~]$ ansible -i inventory amazon -b -m service -a "name=httpd state=started enabled=true"
- [WARNING]: Platform linux on host 172.31.7.158 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.158 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/bin/python3.9"
- },
- "changed": true,
- "enabled": true,
- "name": "httpd",
- "state": "started",
- [ec2-user@ip-172-31-11-206 ~]$ ansible -i inventory ubuntu -b -m service -a "name=apache2 state=started enabled=true"
- 172.31.3.223 | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/bin/python3"
- },
- "changed": true,
- "enabled": true,
- "name": "apache2",
- "state": "started",
- -------------- Copy module -------------
- [ec2-user@ip-172-31-11-206 ~]$ ansible -i inventory amazon -b -m copy -a "src=./website/ dest=/var/www/html owner=apache group=apache"
- 172.31.7.158 | CHANGED => {
- "changed": true,
- "dest": "/var/www/html/",
- "src": "/home/ec2-user/./website/"
- }
- [ec2-user@ip-172-31-11-206 ~]$ ansible -i inventory ubuntu -b -m copy -a "src=./website/ dest=/var/www/html/ owner=www-data group=www-data"
- 172.31.3.223 | CHANGED => {
- "changed": true,
- "dest": "/var/www/html/",
- "src": "/home/ec2-user/./website/"
- }
- ------------------------------------------------- Playbook ----------------------------------------------
- [ec2-user@ip-172-31-11-206 ~]$ cat amazon.yml
- ---
- - name: "httpd and php installation"
- hosts: amazon
- vars:
- httpd_owner: apache
- httpd_group: apache
- become: true
- tasks:
- - name: "httpd install for amazon linux 2023"
- yum:
- name: httpd,php
- state: latest
- - name: "httpd enable"
- service:
- name: httpd
- state: started
- enabled: true
- - name: "php-fpm enable"
- service:
- name: php-fpm
- state: started
- enabled: true
- - name: "Copying website"
- copy:
- src: ./website/
- dest: /var/www/html
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- [ec2-user@ip-172-31-11-206 ~]$ ansible-playbook -i inventory amazon.yml
- PLAY [httpd and php installation] ******************************************************************************************
- TASK [Gathering Facts] *****************************************************************************************************
- [WARNING]: Platform linux on host 172.31.7.158 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.158]
- TASK [httpd install for amazon linux 2023] *********************************************************************************
- changed: [172.31.7.158]
- TASK [httpd enable] ********************************************************************************************************
- changed: [172.31.7.158]
- TASK [php-fpm enable] ******************************************************************************************************
- changed: [172.31.7.158]
- TASK [Copying website] *****************************************************************************************************
- changed: [172.31.7.158]
- PLAY RECAP *****************************************************************************************************************
- 172.31.7.158 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- ---------------- ubuntu.yml ---------------
- ---
- - name: "apache and php installation"
- hosts: ubuntu
- vars:
- apache_owner: www-data
- apache_group: www-data
- become: true
- tasks:
- - name: "Apache and php install for ubuntu 22.04 LTS"
- apt:
- name: apache2,php8.1-fpm
- state: present
- - name: "apache2 enable"
- service:
- name: apache2
- state: started
- enabled: true
- - name: "php-fpm enable"
- service:
- name: php8.1-fpm
- state: started
- enabled: true
- - name: "Copying website"
- copy:
- src: ./website/
- dest: /var/www/html
- owner: "{{ apache_owner }}"
- group: "{{ apache_group }}"
- [ec2-user@ip-172-31-11-206 ~]$ ansible-playbook -i inventory ubuntu.yml
- PLAY [apache and php installation] *****************************************************************************************
- TASK [Gathering Facts] *****************************************************************************************************ok: [172.31.3.223]
- TASK [Apache and php install for ubuntu 22.04 LTS] *************************************************************************changed: [172.31.3.223]
- TASK [apache2 enable] ******************************************************************************************************ok: [172.31.3.223]
- TASK [php-fpm enable] ******************************************************************************************************ok: [172.31.3.223]
- TASK [Copying website] *****************************************************************************************************changed: [172.31.3.223]
- PLAY RECAP *****************************************************************************************************************
- 172.31.3.223 : ok=5 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- ---- Version 5 upgrade -----
- [ec2-user@ip-172-31-11-206 ~]$ ansible-playbook -i inventory ubuntu.yml
- PLAY [apache and php installation] ****************************************************************************************
- TASK [Gathering Facts] ****************************************************************************************************
- ok: [172.31.3.223]
- TASK [Apache and php install for ubuntu 22.04 LTS] ************************************************************************
- ok: [172.31.3.223]
- TASK [apache2 enable] *****************************************************************************************************
- ok: [172.31.3.223]
- TASK [php-fpm enable] *****************************************************************************************************
- ok: [172.31.3.223]
- TASK [Copying website] ****************************************************************************************************
- changed: [172.31.3.223]
- PLAY RECAP ****************************************************************************************************************
- 172.31.3.223 : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Advertisement
Add Comment
Please, Sign In to add comment