Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ANSIBLE-HANDLERS
- ================
- $ cat site.yml
- ---
- - name: "simple site hosting"
- become: yes
- hosts: amazon
- vars:
- httpd_owner: apache
- httpd_group: apache
- httpd_port: 80
- httpd_domain: mysite.1by2.online
- tasks:
- - name: "install httpd"
- yum:
- name: httpd
- state: present
- - name: "creating conf file from template file"
- template:
- src: "./httpd.conf.j2"
- dest: "/etc/httpd/conf/httpd.conf"
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- - name: "creating virtualhost from template file"
- template:
- src: "./virtualhost.conf.j2"
- dest: "/etc/httpd/conf.d/{{ httpd_domain }}.conf"
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- - name: "creating document root"
- file:
- path: "/var/www/html/{{ httpd_domain }}"
- state: directory
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- - name: "create index file"
- copy:
- content: "<h1><center>{{ ansible_fqdn }} </h1></center>"
- dest: "/var/www/html/{{ httpd_domain }}/index.html"
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- - name: "restarting service"
- service:
- name: httpd
- state: restarted
- enabled: true
- $ ansible-playbook -i hosts site.yml
- PLAY [simple site hosting] *********************************************************************************************************************
- TASK [Gathering Facts] ***********************************************************************************************************************
- ok: [172.31.41.110]
- TASK [install httpd] ***********************************************************************************************************************
- changed: [172.31.41.110]
- TASK [creating conf file from template file] ***************************************************************************************************
- changed: [172.31.41.110]
- TASK [creating virtualhost from template file] *************************************************************************************************
- changed: [172.31.41.110]
- TASK [creating document root] ******************************************************************************************************************
- changed: [172.31.41.110]
- TASK [create index file] ***********************************************************************************************************************
- changed: [172.31.41.110]
- TASK [restarting service] **********************************************************************************************************************
- changed: [172.31.41.110]
- PLAY RECAP ***********************************************************************************************************************
- 172.31.41.110 : ok=7 changed=6 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- $ ansible -i hosts all -b -m shell -a "yum remove httpd -y ; rm -rf /etc/httpd/* /var/www/html/*"
- =======================================================================================================================
- $ cat site.yml
- ---
- - name: "simple site hosting"
- become: yes
- hosts: amazon
- vars:
- httpd_owner: apache
- httpd_group: apache
- httpd_port: 80
- httpd_domain: mysite.1by2.online
- tasks:
- - name: "install httpd"
- yum:
- name: httpd
- state: present
- - name: "creating conf file from template file"
- template:
- src: "./httpd.conf.j2"
- dest: "/etc/httpd/conf/httpd.conf"
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- - name: "creating virtualhost from template file"
- template:
- src: "./virtualhost.conf.j2"
- dest: "/etc/httpd/conf.d/{{ httpd_domain }}.conf"
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- - name: "creating document root"
- file:
- path: "/var/www/html/{{ httpd_domain }}"
- state: directory
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- - name: "create index file"
- copy:
- content: "<h1><center>{{ ansible_fqdn }} </h1></center>"
- dest: "/var/www/html/{{ httpd_domain }}/index.html"
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- handlers:
- - name: "restarting service"
- service:
- name: httpd
- state: restarted
- enabled: true
- $ ansible-playbook -i hosts site.yml
- PLAY [simple site hosting] *********************************************************************************************************************
- TASK [Gathering Facts] ***********************************************************************************************************************
- ok: [172.31.41.110]
- TASK [install httpd] ***********************************************************************************************************************
- changed: [172.31.41.110]
- TASK [creating conf file from template file] ***************************************************************************************************
- changed: [172.31.41.110]
- TASK [creating virtualhost from template file] *************************************************************************************************
- changed: [172.31.41.110]
- TASK [creating document root] ******************************************************************************************************************
- changed: [172.31.41.110]
- TASK [create index file] ***********************************************************************************************************************
- changed: [172.31.41.110]
- PLAY RECAP ***********************************************************************************************************************
- 172.31.41.110 : ok=6 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- $ ansible -i hosts all -b -m shell -a "yum remove httpd -y ; rm -rf /etc/httpd/* /var/www/html/*"
- $ cat site.yml
- ---
- - name: "simple site hosting"
- become: yes
- hosts: amazon
- vars:
- httpd_owner: apache
- httpd_group: apache
- httpd_port: 80
- httpd_domain: mysite.1by2.online
- tasks:
- - name: "install httpd"
- yum:
- name: httpd
- state: present
- - name: "creating conf file from template file"
- template:
- src: "./httpd.conf.j2"
- dest: "/etc/httpd/conf/httpd.conf"
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- notify:
- - "apache-restart"
- - name: "creating virtualhost from template file"
- template:
- src: "./virtualhost.conf.j2"
- dest: "/etc/httpd/conf.d/{{ httpd_domain }}.conf"
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- notify:
- - "apache-restart"
- - name: "creating document root"
- file:
- path: "/var/www/html/{{ httpd_domain }}"
- state: directory
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- - name: "create index file"
- copy:
- content: "<h1><center>{{ ansible_fqdn }} </h1></center>"
- dest: "/var/www/html/{{ httpd_domain }}/index.html"
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- handlers:
- - name: "apache-restart"
- service:
- name: httpd
- state: restarted
- enabled: true
- $ ansible-playbook -i hosts site.yml
- PLAY [simple site hosting] *********************************************************************************************************************
- TASK [Gathering Facts] ***********************************************************************************************************************
- ok: [172.31.41.110]
- TASK [install httpd] ***********************************************************************************************************************
- changed: [172.31.41.110]
- TASK [creating conf file from template file] ***************************************************************************************************
- changed: [172.31.41.110]
- TASK [creating virtualhost from template file] *************************************************************************************************
- changed: [172.31.41.110]
- TASK [creating document root] ******************************************************************************************************************
- changed: [172.31.41.110]
- TASK [create index file] ***********************************************************************************************************************
- changed: [172.31.41.110]
- RUNNING HANDLER [apache-restart] ***************************************************************************************************************
- changed: [172.31.41.110]
- PLAY RECAP ***********************************************************************************************************************
- 172.31.41.110 : ok=7 changed=6 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- $ ansible-playbook -i hosts site.yml
- PLAY [simple site hosting] *********************************************************************************************************************
- TASK [Gathering Facts] ***********************************************************************************************************************ok: [172.31.41.110]
- TASK [install httpd] ***********************************************************************************************************************
- ok: [172.31.41.110]
- TASK [creating conf file from template file] ***************************************************************************************************
- ok: [172.31.41.110]
- TASK [creating virtualhost from template file] *************************************************************************************************
- ok: [172.31.41.110]
- TASK [creating document root] ******************************************************************************************************************
- ok: [172.31.41.110]
- TASK [create index file] ***********************************************************************************************************************
- ok: [172.31.41.110]
- PLAY RECAP ***********************************************************************************************************************
- 172.31.41.110 : ok=6 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- =======================================================================================================================
- $ ansible -i hosts all -b -m shell -a "yum remove httpd -y ; rm -rf /etc/httpd/* /var/www/html/*"
- $ cat site.yml
- ---
- - name: "simple site hosting"
- become: yes
- hosts: amazon
- vars:
- httpd_owner: apache
- httpd_group: apache
- httpd_port: 80
- httpd_domain: mysite.1by2.online
- tasks:
- - name: "install httpd"
- yum:
- name: httpd
- state: present
- - name: "creating conf file from template file"
- template:
- src: "./httpd.conf.j2"
- dest: "/etc/httpd/conf/httpd.conf"
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- notify:
- - "apache-restart"
- - "php-restart"
- - name: "creating virtualhost from template file"
- template:
- src: "./virtualhost.conf.j2"
- dest: "/etc/httpd/conf.d/{{ httpd_domain }}.conf"
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- notify:
- - "apache-restart"
- - name: "creating document root"
- file:
- path: "/var/www/html/{{ httpd_domain }}"
- state: directory
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- - name: "create index file"
- copy:
- content: "<h1><center>{{ ansible_fqdn }} </h1></center>"
- dest: "/var/www/html/{{ httpd_domain }}/index.html"
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- handlers:
- - name: "apache-restart"
- service:
- name: httpd
- state: restarted
- enabled: true
- - name: "php-restart"
- debug:
- msg: "restarting PHP service"
- $ ansible-playbook -i hosts site.yml
- PLAY [simple site hosting] *********************************************************************************************************************
- TASK [Gathering Facts] ***********************************************************************************************************************
- ok: [172.31.41.110]
- TASK [install httpd] ***********************************************************************************************************************
- changed: [172.31.41.110]
- TASK [creating conf file from template file] ***************************************************************************************************
- changed: [172.31.41.110]
- TASK [creating virtualhost from template file] *************************************************************************************************
- changed: [172.31.41.110]
- TASK [creating document root] ******************************************************************************************************************
- changed: [172.31.41.110]
- TASK [create index file] ***********************************************************************************************************************
- changed: [172.31.41.110]
- RUNNING HANDLER [apache-restart] ***************************************************************************************************************
- changed: [172.31.41.110]
- RUNNING HANDLER [php-restart] ******************************************************************************************************************
- ok: [172.31.41.110] => {
- "msg": "restarting PHP service"
- }
- PLAY RECAP ***********************************************************************************************************************
- 172.31.41.110 : ok=8 changed=6 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- $ cat virtualhost.conf.j2
- <VirtualHost *:{{ httpd_port }}>
- ServerName {{ httpd_domain }}
- DocumentRoot /var/www/html/{{ httpd_domain }}
- DirectoryIndex index.html index.php index.jsp
- <Directory "/var/www/html/{{ httpd_domain }}">
- AllowOverride all
- </Directory>
- </VirtualHost>
- $ ansible-playbook -i hosts site.yml
- PLAY [simple site hosting] *****************************************************
- TASK [Gathering Facts] *********************************************************
- ok: [172.31.41.110]
- TASK [install httpd] ***********************************************************
- ok: [172.31.41.110]
- TASK [creating conf file from template file] ***********************************
- ok: [172.31.41.110]
- TASK [creating virtualhost from template file] *********************************
- changed: [172.31.41.110]
- TASK [creating document root] **************************************************
- ok: [172.31.41.110]
- TASK [create index file] *******************************************************
- ok: [172.31.41.110]
- RUNNING HANDLER [apache-restart] ***********************************************
- changed: [172.31.41.110]
- PLAY RECAP *********************************************************************
- 172.31.41.110 : ok=7 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- =======================================================================================================================
- # cat /etc/ansible/ansible.cfg | grep roles_path
- roles_path = /etc/ansible/roles
- # cd /etc/ansible/roles/
- # ls -l
- total 0
- # yum install tree -y &> /dev/null
- # ansible-galaxy init lamp
- - Role lamp was created successfully
- # tree lamp/
- lamp/
- ├── defaults
- │ └── main.yml
- ├── files
- ├── handlers
- │ └── main.yml
- ├── meta
- │ └── main.yml
- ├── README.md
- ├── tasks
- │ └── main.yml
- ├── templates
- ├── tests
- │ ├── inventory
- │ └── test.yml
- └── vars
- └── main.yml
- 8 directories, 8 files
- # cat lamp/vars/main.yml
- ---
- httpd_owner: apache
- httpd_group: apache
- httpd_port: 80
- httpd_domain: mysite.1by2.online
- mariadb_root_pass: admin123
- mariadb_extra_user: wp_user
- mariadb_extra_pass: wp_user_pass
- mariadb_extra_db: wp_db
- wp_url: https://wordpress.org/wordpress-6.0.3.tar.gz
- # cat lamp/files/test.php
- <?php phpinfo(); ?>
- # cat lamp/files/test.html
- <h1><center>apache is working</center></h1>
- # tree lamp/
- lamp/
- ├── defaults
- │ └── main.yml
- ├── files
- │ ├── test.html
- │ └── test.php
- ├── handlers
- │ └── main.yml
- ├── meta
- │ └── main.yml
- ├── README.md
- ├── tasks
- │ └── main.yml
- ├── templates
- ├── tests
- │ ├── inventory
- │ └── test.yml
- └── vars
- └── main.yml
- 8 directories, 10 files
- # cp /home/ec2-user/*.j2 lamp/templates/
- # tree lamp/
- lamp/
- ├── defaults
- │ └── main.yml
- ├── files
- │ ├── test.html
- │ └── test.php
- ├── handlers
- │ └── main.yml
- ├── meta
- │ └── main.yml
- ├── README.md
- ├── tasks
- │ └── main.yml
- ├── templates
- │ ├── httpd.conf.j2
- │ ├── virtualhost.conf.j2
- │ └── wp-config.php.j2
- ├── tests
- │ ├── inventory
- │ └── test.yml
- └── vars
- └── main.yml
- 8 directories, 13 files
- # cat lamp/handlers/main.yml
- ---
- - name: "apache-restart"
- service:
- name: httpd
- state: restarted
- enabled: true
- # cat lamp/tasks/main.yml
- ---
- - name: "installing httpd"
- yum:
- name: httpd
- state: present
- - name: "installing php"
- shell: amazon-linux-extras install php7.4 -y
- notify:
- - "apache-restart"
- - name: "creating conf file from template file"
- template:
- src: "./httpd.conf.j2"
- dest: "/etc/httpd/conf/httpd.conf"
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- notify:
- - "apache-restart"
- - name: "creating virtualhost from template file"
- template:
- src: "./virtualhost.conf.j2"
- dest: "/etc/httpd/conf.d/{{ httpd_domain }}.conf"
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- notify:
- - "apache-restart"
- - name: "creating document root"
- file:
- path: "/var/www/html/{{ httpd_domain }}"
- state: directory
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- - name: "creating sample files"
- copy:
- src: "{{ item }}"
- dest: "/var/www/html/{{ httpd_domain }}/"
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- with_items:
- - test.php
- - test.html
- - name: "Mariadb - installing package"
- yum:
- name:
- - mariadb-server
- - MySQL-python
- state: present
- - name: "Mariadb - restarting & enabling service"
- service:
- name: mariadb
- state: restarted
- enabled: true
- - name: "Mariadb - updating root pass"
- ignore_errors: true
- mysql_user:
- login_user: root
- login_password: ""
- name: root
- password: "{{ mariadb_root_pass }}"
- host_all: true
- - name: "Mariadb - removing anonymous users"
- mysql_user:
- login_user: root
- login_password: "{{ mariadb_root_pass }}"
- name: ""
- password: ""
- host_all: true
- state: absent
- - name: "Mariadb - creating db"
- mysql_db:
- login_user: root
- login_password: "{{ mariadb_root_pass }}"
- name: "{{ mariadb_extra_db }}"
- state: present
- - name: "Mariadb - creating user"
- mysql_user:
- login_user: root
- login_password: "{{ mariadb_root_pass }}"
- name: "{{ mariadb_extra_user }}"
- password: "{{ mariadb_extra_pass }}"
- state: present
- host: "%"
- priv: "{{ mariadb_extra_db }}.*:ALL"
- =======================================================================================================================
- # ansible-galaxy init update
- - Role update was created successfully
- # tree update/
- update/
- ├── defaults
- │ └── main.yml
- ├── files
- ├── handlers
- │ └── main.yml
- ├── meta
- │ └── main.yml
- ├── README.md
- ├── tasks
- │ └── main.yml
- ├── templates
- ├── tests
- │ ├── inventory
- │ └── test.yml
- └── vars
- └── main.yml
- 8 directories, 8 files
- # cat update/tasks/main.yml
- ---
- - name: "updating - {{ ansible_os_family }}"
- when: ansible_distribution == "Amazon" and ansible_os_family == "RedHat"
- yum:
- name: "*"
- state: latest
- - name: "updating - {{ ansible_os_family }}"
- when: ansible_distribution == "Ubuntu" and ansible_os_family == "Debian"
- apt:
- name: "*"
- state: latest
- update_cache: true
- # logout
- $ whoami
- ec2-user
- $ pwd
- /home/ec2-user
- $ cat main.yml
- ---
- - name: "demo role"
- hosts: all
- become: true
- roles:
- - update
- - lamp
- $ ansible-playbook -i hosts main.yml
- PLAY [demo role] ***************************************************************
- TASK [Gathering Facts] *********************************************************
- ok: [172.31.41.110]
- TASK [update : updating - RedHat] **********************************************
- ok: [172.31.41.110]
- TASK [update : updating - RedHat] **********************************************
- skipping: [172.31.41.110]
- TASK [lamp : installing httpd] *************************************************
- ok: [172.31.41.110]
- TASK [lamp : installing php] ***************************************************
- changed: [172.31.41.110]
- TASK [lamp : creating conf file from template file] ****************************
- ok: [172.31.41.110]
- TASK [lamp : creating virtualhost from template file] **************************
- ok: [172.31.41.110]
- TASK [lamp : creating document root] *******************************************
- ok: [172.31.41.110]
- TASK [lamp : creating sample files] ********************************************
- changed: [172.31.41.110] => (item=test.php)
- changed: [172.31.41.110] => (item=test.html)
- TASK [lamp : Mariadb - installing package] *************************************
- changed: [172.31.41.110]
- TASK [lamp : Mariadb - restarting & enabling service] **************************
- changed: [172.31.41.110]
- TASK [lamp : Mariadb - updating root pass] *************************************
- changed: [172.31.41.110]
- TASK [lamp : Mariadb - removing anonymous users] *******************************
- changed: [172.31.41.110]
- TASK [lamp : Mariadb - creating db] ********************************************
- changed: [172.31.41.110]
- TASK [lamp : Mariadb - creating user] ******************************************
- changed: [172.31.41.110]
- RUNNING HANDLER [lamp : apache-restart] ****************************************
- changed: [172.31.41.110]
- PLAY RECAP *********************************************************************
- 172.31.41.110 : ok=15 changed=9 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
- $ cat wp-config.php.j2
- define(‘DB_NAME’, '{{ mariadb_extra_db }}');
- define(‘DB_USER’, '{{ mariadb_extra_user }}');
- define(‘DB_PASSWORD’, '{{ mariadb_extra_pass }}');
- define(‘DB_HOST’, ‘localhost’);
- $ cat main.yml
- ---
- - name: "demo role"
- hosts: all
- become: true
- roles:
- - update
- - lamp
- tasks:
- - name: "Wordpress - downloading archive"
- get_url:
- url: "{{ wp_url }}"
- dest: "/tmp/wordpress.tar.gz"
- - name: "Wordpress - extracting archive"
- unarchive:
- src: "/tmp/wordpress.tar.gz"
- dest: "/tmp/"
- remote_src: true
- - name: "Wordpress - copying files"
- copy:
- src: "/tmp/wordpress/"
- dest: "/var/www/html/{{ httpd_domain }}/"
- remote_src: true
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- - name: "Wordpress - creating wp-config file from template"
- template:
- src: "./wp-config.php.j2"
- dest: "/var/www/html/{{ httpd_domain }}/wp-config.php"
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- - name: "Post Installation - restarting"
- service:
- name: "{{ item }}"
- state: restarted
- enabled: true
- with_items:
- - httpd
- - mariadb
- - name: "Post Installation - cleanup"
- file:
- name: "{{ item }}"
- state: absent
- with_items:
- - "/tmp/wordpress/"
- - "/tmp/wordpress.tar.gz"
- $ ansible -i hosts all -b -m shell -a "yum remove httpd php-* mariadb-server -y ; rm -rf /etc/httpd/* /var/www/html/* /var/lib/mysql/*"
- $ ansible-playbook -i hosts main.yml
- PLAY [demo role] ***********************************************************************************************************************
- TASK [Gathering Facts] ***********************************************************************************************************************
- ok: [172.31.41.110]
- TASK [update : updating - RedHat] **************************************************************************************************************
- ok: [172.31.41.110]
- TASK [update : updating - RedHat] **************************************************************************************************************
- skipping: [172.31.41.110]
- TASK [lamp : installing httpd] *****************************************************************************************************************
- changed: [172.31.41.110]
- TASK [lamp : installing php] *******************************************************************************************************************
- changed: [172.31.41.110]
- TASK [lamp : creating conf file from template file] ********************************************************************************************
- changed: [172.31.41.110]
- TASK [lamp : creating virtualhost from template file] ******************************************************************************************
- changed: [172.31.41.110]
- TASK [lamp : creating document root] ***********************************************************************************************************
- changed: [172.31.41.110]
- TASK [lamp : creating sample files] ************************************************************************************************************
- changed: [172.31.41.110] => (item=test.php)
- changed: [172.31.41.110] => (item=test.html)
- TASK [lamp : Mariadb - installing package] *****************************************************************************************************
- changed: [172.31.41.110]
- TASK [lamp : Mariadb - restarting & enabling service] ******************************************************************************************
- changed: [172.31.41.110]
- TASK [lamp : Mariadb - updating root pass] *****************************************************************************************************
- changed: [172.31.41.110]
- TASK [lamp : Mariadb - removing anonymous users] ***********************************************************************************************
- changed: [172.31.41.110]
- TASK [lamp : Mariadb - creating db] ************************************************************************************************************
- changed: [172.31.41.110]
- TASK [lamp : Mariadb - creating user] **********************************************************************************************************
- changed: [172.31.41.110]
- TASK [Wordpress - downloading archive] *********************************************************************************************************
- changed: [172.31.41.110]
- TASK [Wordpress - extracting archive] **********************************************************************************************************
- changed: [172.31.41.110]
- TASK [Wordpress - copying files] ***************************************************************************************************************
- changed: [172.31.41.110]
- TASK [Wordpress - creating wp-config file from template] ***************************************************************************************
- changed: [172.31.41.110]
- TASK [Post Installation - restarting] **********************************************************************************************************
- changed: [172.31.41.110] => (item=httpd)
- changed: [172.31.41.110] => (item=mariadb)
- TASK [Post Installation - cleanup] *************************************************************************************************************
- changed: [172.31.41.110] => (item=/tmp/wordpress/)
- changed: [172.31.41.110] => (item=/tmp/wordpress.tar.gz)
- RUNNING HANDLER [lamp : apache-restart] ********************************************************************************************************
- changed: [172.31.41.110]
- PLAY RECAP ***********************************************************************************************************************
- 172.31.41.110 : ok=21 changed=19 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement