Advertisement
D0cEvil

Ansible - Test playbook

Sep 19th, 2022
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 11.55 KB | Software | 0 0
  1. - hosts: testservers
  2.   tasks:
  3.     - name: Installing VM Ware Tools
  4.       yum: name=open-vm-tools state=present
  5.       when: ansible_distribution == "CentOS"
  6.  
  7.     - name: Installing VM Ware Tools Suse
  8.       zypper: name=https://packages.vmware.com/tools/esx/latest/repos/vmware-tools-repo-SLES{{ ansible_distribution_major_version }}.{{ ansible_distribution_release }}-latest.sles{{ ansible_distribution_major_version }}.{{ ansible_architecture }}.rpm
  9.       when: ansible_os_family == "Suse"
  10.  
  11.     - name: VM Ware tools
  12.       service: name=vmtoolsd.service state=restarted enabled=yes
  13.      # when: ansible_distribution == "CentOS" and ansible_virtualization_type =="VMware"
  14.  
  15. - hosts: testservers
  16.   vars:
  17.     db_name: wordpress
  18.     db_user: wpuser
  19.     db_password: P@ssw0rd
  20.  
  21.   tasks:
  22.   #### SYSTEM UPDATE ####
  23.  
  24.   # CentOS #
  25.  
  26.   - name: System Update CentOS
  27.     yum: name=* state=latest
  28.     when: ansible_distribution == "CentOS"
  29.  
  30.   - name: Disable SELinux CentOS
  31.     selinux: state=disabled
  32.     when: ansible_distribution == "CentOS"
  33.  
  34.   - name: Installing epel-rep CentOS
  35.     yum: pkg=epel-release state=latest
  36.     when: ansible_distribution == "CentOS"
  37.  
  38.   # SUSE #
  39.  
  40.   - name: System Update Suse
  41.     zypper: name=* state=latest
  42.     when: ansible_os_family == "Suse"
  43.  
  44.   - name: Apply all patches Suse
  45.     zypper: name=* state=latest type=patch
  46.     when: ansible_os_family == "Suse"
  47.  
  48.   # ALL OS #
  49.  
  50.   - name: Reboot server
  51.     shell: sleep 3 && /sbin/shutdown -r "Ansible system reboot"
  52. #    async: 1
  53.     poll: 0
  54.  
  55.   - name: Waiting for SSH connection
  56.     local_action: wait_for host={{ inventory_hostname }} port=22 delay=20 connect_timeout=200
  57.     become: false
  58.     delegate_to: localhost
  59.  
  60. #### Software Installation ####
  61.  
  62.   - name: Installation Utils CentOS
  63.     yum: pkg={{ item }}
  64.     with_items:
  65.      - wget
  66.       - mc
  67.       - nano
  68.     when: ansible_distribution == "CentOS"
  69.  
  70.   - name: Installation Utils Suse
  71.     zypper: pkg={{ item }}
  72.     with_items:
  73.      - wget
  74.       - mc
  75.     when: ansible_os_family == "Suse"
  76.  
  77. #### APACHE ####
  78.  
  79. # CentOS #
  80.  
  81.   - name: Installation Apache Suse
  82.     yum: pkg=httpd state=present
  83.     when: ansible_distribution == "CentOS"
  84.  
  85.   - name: Enable Apache on System Boot CentOS
  86.     service: name=httpd enabled=yes
  87.     when: ansible_distribution == "CentOS"
  88.  
  89. # Suse #
  90.  
  91.   - name: Installation Apache Suse
  92.     zypper: name=apache2 state=latest
  93.     when: ansible_os_family == "Suse"
  94.  
  95.   - name: Enable Apache on System Boot Suse
  96.     service: name=apache2 enabled=yes
  97.     when: ansible_os_family == "Suse"
  98.  
  99. #### PHP ####
  100.  
  101. # CentOS #
  102.  
  103.   - name: Installation PHP mods CentOS
  104.     yum: pkg={{ item }} state=present
  105.     with_items:
  106.     - php
  107.      - php-gd
  108.      - php-mysql
  109.      - php-devel
  110.     when: ansible_distribution == "CentOS"
  111.  
  112. # Suse #
  113.    
  114.   - name: Installation PHP mods Suse
  115.     zypper: pkg={{ item }} state=present
  116.     with_items:
  117.     - php
  118.      - php-mysql
  119.      - php-gd
  120.     when: ansible_os_family == "Suse"
  121.  
  122. #### MariaDB - CentOS ####
  123.  
  124.  
  125.   - name: Installation MariaDB CentOS
  126.     yum: pkg=mariadb-server state=present
  127.     when: ansible_distribution == "CentOS"
  128.  
  129.   - name: Enable MariaDB on System Boot CentOS
  130.     service: name=mariadb enabled=yes
  131.     when: ansible_distribution == "CentOS"
  132.  
  133.   - name: Installation Pyton Mysql module CentOS
  134.     yum: pkg=MySQL-python state=present
  135.     when: ansible_distribution == "CentOS"
  136.  
  137. #### MySQL - Suse ####
  138.  
  139.   - name: Installation MySQL server Suse
  140.     zypper: pkg=mysql state=present
  141.     when: ansible_os_family == "Suse"
  142.  
  143.   - name: Installation MySQL client Suse
  144.     zypper: pkg=mysql-client state=present
  145.     when: ansible_os_family == "Suse"
  146.  
  147.   - name: Enable MySQL server on System Boot Suse
  148.     service: name=mysql enabled=yes
  149.     when: ansible_os_family == "Suse"
  150.  
  151.   - name: Install Phyton-MySQL module Suse
  152.     zypper: pkg=python-mysql state=present
  153.     when: ansible_os_family == "Suse"
  154.  
  155. #### phpMyAdmin ####
  156.  
  157. # CentOS #
  158.  
  159.   - name: Installation phpMyAdmin
  160.     yum: pkg=phpMyAdmin state=present
  161.     when: ansible_distribution == "CentOS"
  162.  
  163. # Suse #
  164.  
  165. #  - name: Installation phpMyAdmin Suse
  166. #    zypper: pkg=phpMyAdmin
  167. #    when: ansible_os_family == "Suse"  
  168.  
  169. #### Starting services ####
  170.  
  171. # CentOS #
  172.  
  173.   - name: Service Mariadb start CentOS
  174.     service: name=mariadb state=started
  175.     when: ansible_distribution == "CentOS"
  176.  
  177.   - name: Service Apache start CentOS
  178.     service: name=httpd state=started
  179.     when: ansible_distribution == "CentOS"
  180.  
  181. # Suse #
  182.  
  183.   - name: Service MySQL start Suse
  184.     service: name=mysql state=started
  185.     when: ansible_os_family == "Suse"
  186.  
  187.   - name: Service Apache start Suse
  188.     service: name=apache2 state=started
  189.     when: ansible_os_family == "Suse"
  190.  
  191. #### Configuring MariaDB and SQL !!! ALL OS !!! ####
  192.  
  193.   - name: Set root Password
  194.     mysql_user: user=root password=P@ssw0rd host=localhost
  195.     #when: ansible_distribution == "CentOS"
  196.  
  197.   - name: Create MySQL database
  198.     mysql_db: name={{ db_name }} login_user=root login_password=P@ssw0rd state=present
  199.     #when: ansible_distribution == "CentOS"
  200.  
  201.   - name: Create MySQL user
  202.     mysql_user: login_user=root login_password=P@ssw0rd name={{ db_user }} password={{ db_password }} priv=*.*:ALL
  203.     #when: ansible_distribution == "CentOS"
  204.  
  205. #### WordPress ####
  206.  
  207. # ALL OS #
  208.  
  209.   - name: Download LAST version WordPress
  210.     get_url: url=https://wordpress.org/latest.tar.gz dest=/tmp/wordpress.tar.gz validate_certs=no
  211.     #when: ansible_distribution == "CentOS"
  212.  
  213. # CentOS #
  214.  
  215.   - name: Extract WordPress CentOS
  216.     unarchive: src=/tmp/wordpress.tar.gz dest=/var/www/html copy=no
  217.     when: ansible_distribution == "CentOS"
  218.  
  219. # Suse #
  220.  
  221.   - name: Extract WordPress Suse
  222.     unarchive: src=/tmp/wordpress.tar.gz dest=/srv/www/htdocs copy=no
  223.     when: ansible_os_family == "Suse"
  224.  
  225. #### WordPress Config ####
  226.  
  227. # CentOS #  
  228.  
  229.   - name: Create WordPress config file CentOS
  230.     command: mv /var/www/html/wordpress/wp-config-sample.php /var/www/html/wordpress/wp-config.php creates=/var/www/html/wordpress/wp-config.php
  231.     when: ansible_distribution == "CentOS"
  232.  
  233.   - name: Update WordPress config file CentOS
  234.     lineinfile:
  235.      dest=/var/www/html/wordpress/wp-config.php
  236.       regexp="{{ item.regexp }}"
  237.       line="{{ item.line }}"
  238.     with_items:
  239.       - {'regexp': "define\\('DB_NAME', '(.)+'\\);", 'line': "define('DB_NAME', '{{ db_name }}');"}
  240.       - {'regexp': "define\\('DB_USER', '(.)+'\\);", 'line': "define('DB_USER', '{{ db_user }}');"}
  241.       - {'regexp': "define\\('DB_PASSWORD'. '(.)+'\\);", 'line': "define('DB_PASSWORD', '{{ db_password }}');"}
  242.     when: ansible_distribution == "CentOS"
  243.  
  244. # Suse #
  245.  
  246.   - name: Create WordPress config file Suse
  247.     command: mv /srv/www/htdocs/wordpress/wp-config-sample.php /srv/www/htdocs/wordpress/wp-config.php creates=/var/www/html/wordpress/wp-config.php
  248.     when: ansible_os_family == "Suse"
  249.  
  250.   - name: Update WordPress config file Suse
  251.     lineinfile:
  252.      dest=/srv/www/htdocs/wordpress/wp-config.php
  253.       regexp="{{ item.regexp }}"
  254.       line="{{ item.line }}"
  255.     with_items:
  256.       - {'regexp': "define\\('DB_NAME', '(.)+'\\);", 'line': "define('DB_NAME', '{{ db_name }}');"}
  257.       - {'regexp': "define\\('DB_USER', '(.)+'\\);", 'line': "define('DB_USER', '{{ db_user }}');"}
  258.       - {'regexp': "define\\('DB_PASSWORD'. '(.)+'\\);", 'line': "define('DB_PASSWORD', '{{ db_password }}');"}
  259.     when: ansible_os_family == "Suse"
  260.  
  261. #### Firewall Config ####
  262.  
  263. # CentOS #
  264.  
  265.   - name: Add HTTP service CentOS
  266.     command: firewall-cmd --add-service=http --permanent
  267.     when: ansible_distribution == "CentOS"
  268.  
  269.   - name: Add HTTPs service CentOS
  270.     command: firewall-cmd --add-service=https --permanent
  271.     when: ansible_distribution == "CentOS"
  272.  
  273.   - name: Add MySQL service CentOS
  274.     command: firewall-cmd --add-port=3306/tcp --permanent
  275.     when: ansible_distribution == "CentOS"
  276.  
  277.   - name: Restarting firewall CentOS
  278.     command: firewall-cmd --reload
  279.     when: ansible_distribution == "CentOS"
  280.  
  281. # Suse #
  282.  
  283.   - name: Add HTTP service Suse
  284.     lineinfile: dest=/etc/sysconfig/SuSEfirewall2 line='FW_CONFIGURATIONS_EXT="apache2"' create="yes"
  285.     when: ansible_os_family == "Suse"
  286.  
  287.   - name: Add SSH servcie Suse
  288.     lineinfile: dest=/etc/sysconfig/SuSEfirewall2 line='FW_SERVICES_EXT_TCP="ssh"' create="yes"
  289.     when: ansible_os_family == "Suse"
  290.  
  291.   - name: Add MySQL servcie Suse
  292.     lineinfile: dest=/etc/sysconfig/SuSEfirewall2 line='FW_SERVICES_EXT_TCP="3306"' create="yes"
  293.     when: ansible_os_family == "Suse"
  294.  
  295.   - name: Allow ping Suse
  296.     lineinfile: dest=/etc/sysconfig/SuSEfirewall2 line='FW_ALLOW_PING_FW="yes"' create="yes"
  297.     when: ansible_os_family == "Suse"
  298.  
  299.   - name: Reload init Firewall settings Suse
  300.     command: /etc/init.d/SuSEfirewall2_init restart
  301.     when: ansible_os_family == "Suse"
  302.  
  303.   - name: Reload init Firewall settings Suse
  304.     command: /etc/init.d/SuSEfirewall2_init restart
  305.     when: ansible_os_family == "Suse"
  306.  
  307. #### Services ####
  308.  
  309. # CentOS #
  310.  
  311.   - name: MariaDB restart CentOS
  312.     service: name=mariadb state=restarted
  313.     when: ansible_distribution == "CentOS"
  314.  
  315.   - name: Apache restart CentOS
  316.     service: name=httpd state=restarted
  317.     when: ansible_distribution == "CentOS"
  318.  
  319. # Suse #
  320.  
  321.   - name: MySQL restart Suse
  322.     service: name=mysql state=restarted
  323.     when: ansible_distribution == "Suse"
  324.  
  325.   - name: Apache restart Suse
  326.     service: name=apache2 state=restarted
  327.     when: ansible_distribution == "Suse"
  328.  
  329. - hosts: testservers
  330.   vars:
  331.     vmwaretools_tools_version: latest
  332.     vmwaretools_yum_server: https://packages.vmware.com
  333.     vmwaretools_yum_path: /tools
  334.     vmwaretools_repo_version: latest
  335.   tasks:
  336.   - name: Downloading file
  337.     get_url: url={{ vmwaretools_yum_server }}{{ vmwaretools_yum_path }}/esx/{{ vmwaretools_tools_version }}/repos/vmware-tools-repo-SELS{{ ansible_distribution_major_version }}.{{ ansible_distribution_release }}-{{ vmwaretools_repo_version }}.sles{{ ansible_distribution_major_version }}.{{ ansible_architecture }}.rpm dest=/tmp/vmtools.rpm validate_certs=no
  338.     when: ansible_os_family == "Suse" and ansible_distribution_major_version|int == 11
  339.  
  340. - hosts: testservers
  341.   tasks:
  342. ##### Firewall config #####
  343.  
  344. ## CentOS ##
  345.  
  346.    - name: Allow HTTP  CentOS
  347.      command: firewall-cmd --add-service=http --permanent
  348.      when: ansible_distribution == "CentOS"
  349.  
  350.    - name: Allow HTTPs  CentOS
  351.      command: firewall-cmd --add-service=https --permanent
  352.      when: ansible_distribution == "CentOS"
  353.    
  354.    - name: Allow SQL  CentOS
  355.      command: firewall-cmd  --add-port=3306/tcp --permanent
  356.      when: ansible_distribution == "CentOS"
  357.  
  358. ## SUSE ##
  359.  
  360.    - name: Allow HTTP  SUSE
  361.      lineinfile: dest=/etc/sysconfig/SuSEfirewall2 line='FW_CONFIGURATIONS_EXT="apache2"' create=yes
  362.      when: ansible_os_family == "Suse"
  363.  
  364.    - name: Allow SSH  SUSE
  365.      lineinfile: dest=/etc/sysconfig/SuSEfirewall2 line='FW_SERVICES_EXT_TCP="ssh"' create=yes
  366.      when: ansible_os_family == "Suse"
  367.  
  368.    - name: Allow PING  SUSE
  369.      lineinfile: dest=/etc/sysconfig/SuSEfirewall2 line='FW_ALLOW_PING_FW="yes"' create=yes
  370.      when: ansible_os_family == "Suse"
  371.  
  372. ###### Firewall restart ######
  373.  
  374.    - name: Reload firewall service  CentOS
  375.      command: firewall-cmd --reload
  376.      when: ansible_distribution == "CentOS"
  377.  
  378.    - name: Reload init firewall service  SUSE
  379.      command: /etc/init.d/SuSEfirewall2_init restart
  380.      when: ansible_os_family == "Suse"
  381.  
  382.    - name: Reload setup firewall service  SUSE
  383.      command: /etc/init.d/SuSEfirewall2_setup restart
  384.      when: ansible_os_family == "Suse"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement