[Ansible]apache,php,mariadbをインストールする


ApacheとPHPとMariadbをインストールしてくれます。
なんだかんだで稀によくインストールする機会が発生してしまうので、
いい加減毎回Googleでインストール方法を調べてコードをコピペから卒業しようと作りました。

コード自体は数か月前に作成したものなので、
もしかしたら今はもうyumじゃなくdnfだけで対応可能かも。


#なんだか忘れたけど下記設定しないと動かない。
#sed '/module_set_locale = False/a\invalid_task_attribute_failed=False' /etc/ansible/ansible.cfg
#export ANSIBLE_INVALID_TASK_ATTRIBUTE_FAILED=False

- hosts: localhost
  connection: local
  vars:
    mysql_root_password: "あああ"
    mysql_db_password: "あああ"
    wordpress_db_name: "あああ"
    wordpress_db_user: "あああ"
    wordpress_db_password: "あああ"
  tasks:
    - name: yum update
      yum:
        name: '*'
        state: latest

#####Apache#####

    - name: Check install apache
      shell: rpm -qa | grep httpd | grep -v httpd-filesystem | wc -l
      args:
        warn: false
      register: check_ins_apache

    - name: Install apache
      dnf:
        name: 'httpd'
        state: present
      when: "'0'  in check_ins_apache.stdout"

    - name: Make sure a service is running
      systemd:
        state: started
        name: httpd
      when: "'0'  in check_ins_apache.stdout"

    - name: enable service httpd and ensure it is not masked
      systemd:
        name: httpd
        enabled: yes
        masked: no
      when: "'0'  in check_ins_apache.stdout"

    - name: edit apache directoryindex
      replace: >-
        dest='/etc/httpd/conf/httpd.conf'
        regexp='DirectoryIndex index.html'
        replace='DirectoryIndex index.php index.htm index.html'

    - name: edit apache servername
      replace: >-
        dest='/etc/httpd/conf/httpd.conf'
        regexp='#ServerName www.example.com:80'
        replace='ServerName hayate.moe.hm'

    - name: edit apache documentroot
      replace: >-
        dest='/etc/httpd/conf/httpd.conf'
        regexp='DocumentRoot \"/var/www/html\"'
        replace='DocumentRoot \"/var/www\"'


#####PHP#####

    - name: Check install remi
      shell: rpm -qa | grep remi | wc -l
      args:
        warn: false
      register: check_ins_remi

    - name: Install remi.repo
      shell: rpm -i https://rpms.remirepo.net/enterprise/remi-release-8.rpm
      args:
        warn: false
      when: "'0'  in check_ins_remi.stdout"

    - name: Check install php
      shell: rpm -qa | grep php | wc -l
      args:
        warn: false
      register: check_ins_php

    - name: php7.4 install
      shell: 'dnf module install -y php:remi-7.4'
      when: "'0'  in check_ins_php.stdout"

    - name: get php.ini
      shell: "cat /etc/php.ini"
      ignore_errors: True
      args:
        warn: false
      register: phpini

    - name: edit memory_limit
      replace: >-
        dest='/etc/php.ini'
        regexp='memory_limit = 128M'
        replace='memory_limit = 256M'
      when: "'memory_limit = 128M' in phpini.stdout"

    - name: edit expose_php
      replace: >-
        dest='/etc/php.ini'
        regexp='expose_php = On'
        replace='expose_php = Off'
      when: "'expose_php = On' in phpini.stdout"

    - name: edit upload_max_filesize
      replace: >-
        dest='/etc/php.ini'
        regexp='upload_max_filesize = 2M'
        replace='upload_max_filesize = 128M'
      when: "'upload_max_filesize = 2M' in phpini.stdout"

    - name: edit post_max_size
      replace: >-
        dest='/etc/php.ini'
        regexp='post_max_size = 8M'
        replace='post_max_size = 128M'
      when: "'post_max_size = 8M' in phpini.stdout"

    - name: edit post_max_size
      replace: >-
        dest='/etc/php.ini'
        regexp=';date.timezone ='
        replace='date.timezone = Asia/Tokyo'
      when: "';date.timezone =' in phpini.stdout"

#####MariaDB#####

    - name: Check install mariadb
      shell: rpm -qa | grep mariadb | wc -l
      args:
        warn: false
      register: check_ins_mariadb

    - name: Install MariaDB
      yum:
        name: "{{ item }}"
        state: present
        enablerepo: remi
      with_items:
        - mariadb
        - mariadb-server
      when: "'0'  in check_ins_mariadb.stdout"

    - name: Install remi.repo
      dnf:
        name: 'python3-PyMySQL'
        state: present
      when: "'0'  in check_ins_mariadb.stdout"

    - name: Make sure a service is running
      systemd:
        state: started
        name: mariadb
      when: "'0'  in check_ins_mariadb.stdout"

    - name: enable service mariadb and ensure it is not masked
      systemd:
        name: mariadb
        enabled: yes
        masked: no
      when: "'0'  in check_ins_mariadb.stdout"

    - name: root アカウントのパスワードを設定
      mysql_user:
        name: root
        host: localhost
        password: "{{ mysql_root_password }}"
      when: "'0'  in check_ins_mariadb.stdout"

    - name: データベース wordpress を作成
      mysql_db:
        login_user: root
        login_password: "{{ mysql_db_password }}"
        name: "{{ wordpress_db_name }}"
        state: present
      when: "'0'  in check_ins_mariadb.stdout"

    - name: MySQLユーザー wordpress を作成し,wordpress.* にすべての権限を与える
      mysql_user:
        login_user: root
        login_password: "{{ mysql_db_password }}"
        name: "{{ wordpress_db_user }}"
        password: "{{ wordpress_db_password }}"
        priv: "wordpress.*:ALL"
        host: localhost
        state: present
      when: "'0'  in check_ins_mariadb.stdout"