Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------------------------------------------------- Task 1-------------------------------------------
- #### variables ####
- [ec2-user@ip-172-31-45-107 lamp-project]$ cat apache.vars
- ---
- httpd_port: 80
- httpd_site: blog.chrisich.fun
- httpd_owner: apache
- httpd_group: apache
- packages:
- - httpd
- - php-fpm
- [ec2-user@ip-172-31-45-107 lamp-project]$
- [ec2-user@ip-172-31-45-107 lamp-project]$
- [ec2-user@ip-172-31-45-107 lamp-project]$ cat mariadb.vars
- ---
- mysql_packages:
- - mariadb105-server
- - php8.1-mysqlnd
- - python3-pip
- mysqlrootpasswd: mysqlroot123
- mysql_docroot: /var/lib/mysql
- blog_user: blog_user
- blog_passwd: blog123
- blog_db: blog_db
- [ec2-user@ip-172-31-45-107 lamp-project]$
- [ec2-user@ip-172-31-45-107 lamp-project]$ cat wordpress.vars
- ---
- wordpress_url: https://wordpress.org/wordpress-6.2.tar.gz
- #### Playbook ####
- ---
- - name : "Wordpress LAMP installation"
- hosts: amazon
- become: true
- vars_files:
- - apache.vars
- - mariadb.vars
- - wordpress.vars
- tasks:
- - name: "Installing Apache and php-fpm"
- yum:
- name: "{{ packages }}"
- state: present
- register: package_status
- tags:
- - lamp
- - name: "Copying the httpd conf template to the remote machine"
- template:
- src: ./httpd.conf.tmpl
- dest: /etc/httpd/conf/httpd.conf
- register: conf_status
- tags:
- - lamp
- - name: "Creating the document root /var/www/html/{{ httpd_site }}"
- file:
- path: "/var/www/html/{{ httpd_site }}"
- state: directory
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- tags:
- - lamp
- - name: "Copying the virtual host template to /etc/httpd/conf.d/{{ httpd_site }}"
- template:
- src: ./virtualhost.conf.tmpl
- dest: "/etc/httpd/conf.d/default.conf"
- register: virtualhost_status
- tags:
- - lamp
- - name: "Copying the contents to document root"
- when: package_status.changed == true
- copy:
- src: ./www/
- dest: "/var/www/html/{{ httpd_site }}"
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- register: index_status
- tags:
- - lamp
- - name: "Service php-fpm restart/enable when condition meets"
- when: package_status.changed == true or index_status.changed == true
- service:
- name: php-fpm
- state: restarted
- enabled: true
- tags:
- - lamp
- - name: "Service httpd restart/enable when condition meets"
- when: package_status.changed == true or conf_status.changed == true or virtualhost_status.changed == true
- service:
- name: httpd
- state: restarted
- enabled: true
- tags:
- - lamp
- - name: "Installing mariadb required packages"
- yum:
- name: "{{ mysql_packages }}"
- state: present
- tags:
- - lamp
- - mysql
- - name: "Restarting mariadb server"
- service:
- name: mariadb
- state: restarted
- enabled: true
- tags:
- - lamp
- - mysql
- - name: "Installing the pip3 module for mysql"
- pip:
- name: PyMySQL
- state: present
- tags:
- - lamp
- - mysql
- - name: "Setting root password for mysql"
- mysql_user:
- login_user: root
- login_password: ""
- login_unix_socket: "{{ mysql_docroot }}/mysql.sock"
- name: root
- password: "{{ mysqlrootpasswd }}"
- ignore_errors: true
- tags:
- - lamp
- - mysql
- - name: "Creating a datababe {{ blog_db }}"
- mysql_db:
- login_user: root
- login_password: "{{ mysqlrootpasswd }}"
- login_unix_socket: "{{ mysql_docroot }}/mysql.sock"
- name: "{{ blog_db }}"
- tags:
- - lamp
- - mysql
- - name: "Creating an extra user {{ blog_user }}"
- mysql_user:
- login_user: root
- login_password: "{{ mysqlrootpasswd }}"
- login_unix_socket: "{{ mysql_docroot }}/mysql.sock"
- name: "{{ blog_user}}"
- password: "{{ blog_passwd }}"
- host: "%"
- priv: "{{ blog_db }}.*:ALL"
- tags:
- - lamp
- - mysql
- - name: "Wget the wordpress"
- get_url:
- url: "{{ wordpress_url }}"
- dest: /tmp/wordpress.tar.gz
- tags: wordpress
- - name: "unarchive the worpress url"
- unarchive:
- src: /tmp/wordpress.tar.gz
- dest: /tmp/
- remote_src: true
- tags: wordpress
- - name: "Copying the wordpress contents to the docroot"
- copy:
- src: /tmp/wordpress/
- dest: "/var/www/html/{{ httpd_site }}"
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- remote_src: true
- tags: wordpress
- - name: "Copying wp-config.php to remote server"
- template:
- src: ./wp-config.php.tmpl
- dest: "/var/www/html/{{ httpd_site }}/wp-config.php"
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- tags: wordpress
- - name: "Cleanup"
- file:
- name: "{{ item }}"
- state: absent
- with_items:
- - /tmp/wordpress.tar.gz
- - /tmp/wordpress
- tags: wordpress
- #### Output ####
- [ec2-user@ip-172-31-45-107 lamp-project]$ ansible-vault encrypt apache.vars mariadb.vars wordpress.vars
- New Vault password:
- Confirm New Vault password:
- Encryption successful
- [ec2-user@ip-172-31-45-107 lamp-project]$ ansible-playbook -i inventory lamp.yml -t lamp,mysql,wordpress --ask-vault-pass
- Vault password:
- PLAY [Wordpress LAMP installation] *************************************************************************************************************************
- TASK [Gathering Facts] *************************************************************************************************************************************
- [WARNING]: Platform linux on host 172.31.42.193 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.42.193]
- TASK [Installing Apache and php-fpm] ***********************************************************************************************************************
- changed: [172.31.42.193]
- TASK [Copying the httpd conf template to the remote machine] ***********************************************************************************************
- changed: [172.31.42.193]
- TASK [Creating the document root /var/www/html/blog.chrisich.fun] ******************************************************************************************
- changed: [172.31.42.193]
- TASK [Copying the virtual host template to /etc/httpd/conf.d/blog.chrisich.fun] ****************************************************************************
- changed: [172.31.42.193]
- TASK [Copying the contents to document root] ***************************************************************************************************************
- changed: [172.31.42.193]
- TASK [Service php-fpm restart/enable when condition meets] *************************************************************************************************
- changed: [172.31.42.193]
- TASK [Service httpd restart/enable when condition meets] ***************************************************************************************************
- changed: [172.31.42.193]
- TASK [Installing mariadb required packages] ****************************************************************************************************************
- changed: [172.31.42.193]
- TASK [Restarting mariadb server] ***************************************************************************************************************************
- changed: [172.31.42.193]
- TASK [Installing the pip3 module for mysql] ****************************************************************************************************************
- ok: [172.31.42.193]
- TASK [Setting root password for mysql] *********************************************************************************************************************
- changed: [172.31.42.193]
- TASK [Creating a datababe blog_db] *************************************************************************************************************************
- changed: [172.31.42.193]
- TASK [Creating an extra user blog_user] ********************************************************************************************************************
- changed: [172.31.42.193]
- TASK [Wget the wordpress] **********************************************************************************************************************************
- changed: [172.31.42.193]
- TASK [unarchive the worpress url] **************************************************************************************************************************
- changed: [172.31.42.193]
- TASK [Copying the wordpress contents to the docroot] *******************************************************************************************************
- changed: [172.31.42.193]
- TASK [Copying wp-config.php to remote server] **************************************************************************************************************
- changed: [172.31.42.193]
- TASK [Cleanup] *********************************************************************************************************************************************
- changed: [172.31.42.193] => (item=/tmp/wordpress.tar.gz)
- changed: [172.31.42.193] => (item=/tmp/wordpress)
- PLAY RECAP *************************************************************************************************************************************************
- 172.31.42.193 : ok=19 changed=17 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- ------------------------------------------------------------ Task 2---------------------------------------------------------------------------------------------------
- ---
- - name : "Wordpress LAMP installation"
- hosts: amazon
- become: true
- vars_files:
- - apache.vars
- - mariadb.vars
- - wordpress.vars
- tasks:
- - name: "Installing Apache and php-fpm"
- yum:
- name: "{{ packages }}"
- state: present
- register: package_status
- tags:
- - never
- - lamp
- - name: "Copying the httpd conf template to the remote machine"
- template:
- src: ./httpd.conf.tmpl
- dest: /etc/httpd/conf/httpd.conf
- register: conf_status
- tags:
- - never
- - lamp
- - name: "Creating the document root /var/www/html/{{ httpd_site }}"
- file:
- path: "/var/www/html/{{ httpd_site }}"
- state: directory
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- tags:
- - never
- - lamp
- - name: "Copying the virtual host template to /etc/httpd/conf.d/{{ httpd_site }}"
- template:
- src: ./virtualhost.conf.tmpl
- dest: "/etc/httpd/conf.d/default.conf"
- register: virtualhost_status
- tags:
- - never
- - lamp
- - name: "Copying the contents to document root"
- when: package_status.changed == true
- copy:
- src: ./www/
- dest: "/var/www/html/{{ httpd_site }}"
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- register: index_status
- tags:
- - never
- - lamp
- - name: "Service php-fpm restart/enable when condition meets"
- when: package_status.changed == true or index_status.changed == true
- service:
- name: php-fpm
- state: restarted
- enabled: true
- tags:
- - never
- - lamp
- - name: "Service httpd restart/enable when condition meets"
- when: package_status.changed == true or conf_status.changed == true or virtualhost_status.changed == true
- service:
- name: httpd
- state: restarted
- enabled: true
- tags:
- - never
- - lamp
- - name: "Installing mariadb required packages"
- yum:
- name: "{{ mysql_packages }}"
- state: present
- tags:
- - never
- - lamp
- - mysql
- - name: "Restarting mariadb server"
- service:
- name: mariadb
- state: restarted
- enabled: true
- tags:
- - never
- - lamp
- - mysql
- - name: "Installing the pip3 module for mysql"
- pip:
- name: PyMySQL
- state: present
- tags:
- - never
- - lamp
- - mysql
- - name: "Setting root password for mysql"
- mysql_user:
- login_user: root
- login_password: ""
- login_unix_socket: "{{ mysql_docroot }}/mysql.sock"
- name: root
- password: "{{ mysqlrootpasswd }}"
- ignore_errors: true
- tags:
- - never
- - lamp
- - mysql
- - name: "Creating a datababe {{ blog_db }}"
- mysql_db:
- login_user: root
- login_password: "{{ mysqlrootpasswd }}"
- login_unix_socket: "{{ mysql_docroot }}/mysql.sock"
- name: "{{ blog_db }}"
- tags:
- - never
- - lamp
- - mysql
- - name: "Creating an extra user {{ blog_user }}"
- mysql_user:
- login_user: root
- login_password: "{{ mysqlrootpasswd }}"
- login_unix_socket: "{{ mysql_docroot }}/mysql.sock"
- name: "{{ blog_user}}"
- password: "{{ blog_passwd }}"
- host: "%"
- priv: "{{ blog_db }}.*:ALL"
- tags:
- - never
- - lamp
- - mysql
- - name: "Wget the wordpress"
- get_url:
- url: "{{ wordpress_url }}"
- dest: /tmp/wordpress.tar.gz
- tags: never,wordpress
- - name: "unarchive the worpress url"
- unarchive:
- src: /tmp/wordpress.tar.gz
- dest: /tmp/
- remote_src: true
- tags: never,wordpress
- - name: "Copying the wordpress contents to the docroot"
- copy:
- src: /tmp/wordpress/
- dest: "/var/www/html/{{ httpd_site }}"
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- remote_src: true
- tags: never,wordpress
- - name: "Copying wp-config.php to remote server"
- template:
- src: ./wp-config.php.tmpl
- dest: "/var/www/html/{{ httpd_site }}/wp-config.php"
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- tags: never,wordpress
- - name: "Cleanup"
- file:
- name: "{{ item }}"
- state: absent
- with_items:
- - /tmp/wordpress.tar.gz
- - /tmp/wordpress
- tags: never,wordpress
- #### Output {checking whether playbook is running without tags} ####
- [ec2-user@ip-172-31-45-107 lamp-project]$ ansible-playbook -i inventory lamp.yml --ask-vault-pass
- Vault password:
- PLAY [Wordpress LAMP installation] *************************************************************************************************************************
- TASK [Gathering Facts] *************************************************************************************************************************************
- [WARNING]: Platform linux on host 172.31.42.193 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.42.193]
- PLAY RECAP *************************************************************************************************************************************************
- 172.31.42.193 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- ------------------------------------------------------------ Task 3 --vault-password-file ---------------------------------------------------------------------------------------------------
- [ec2-user@ip-172-31-45-107 lamp-project]$ cat passwd.sh
- #!/bin/bash
- aws secretsmanager get-secret-value --secret-id ansible_vault | jq --raw-output .SecretString | jq -r ".vault_passwd"
- #### output ####
- [ec2-user@ip-172-31-45-107 lamp-project]$ ansible-playbook -i inventory -t wordpress lamp.yml --vault-password-file=passwd.sh
- PLAY [Wordpress LAMP installation] *************************************************************************************************************************
- TASK [Gathering Facts] *************************************************************************************************************************************
- [WARNING]: Platform linux on host 172.31.42.193 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.42.193]
- TASK [Wget the wordpress] **********************************************************************************************************************************
- changed: [172.31.42.193]
- TASK [unarchive the worpress url] **************************************************************************************************************************
- changed: [172.31.42.193]
- TASK [Copying the wordpress contents to the docroot] *******************************************************************************************************
- ok: [172.31.42.193]
- TASK [Copying wp-config.php to remote server] **************************************************************************************************************
- ok: [172.31.42.193]
- TASK [Cleanup] *********************************************************************************************************************************************
- changed: [172.31.42.193] => (item=/tmp/wordpress.tar.gz)
- changed: [172.31.42.193] => (item=/tmp/wordpress)
- PLAY RECAP *************************************************************************************************************************************************
- 172.31.42.193 : ok=6 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Advertisement
Add Comment
Please, Sign In to add comment