diff --git a/Vagrantfile b/Vagrantfile index 752de8d..f8aaaef 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -1,22 +1,26 @@ VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.box = "ubuntu/jammy64" - config.vm.define "web1" do |master| - master.vm.hostname = "web1" - master.vm.network "forwarded_port", guest: 80, host: 8080 - master.vm.network "private_network", ip: "192.168.56.2" - end - config.vm.define "web2" do |master| - master.vm.hostname = "web2" - master.vm.network "forwarded_port", guest: 80, host: 8081 - master.vm.network "private_network", ip: "192.168.56.3" - end config.vm.provider "virtualbox" do |v| v.memory = 1024 v.cpus = 2 end - config.vm.provision "ansible" do |ansible| - ansible.playbook = "ansible/playbook.yaml" - ansible.vault_password_file = "vault_pass" + N = 3 + (1..N).each do |machine_id| + config.vm.define "web#{machine_id}" do |machine| + machine.vm.hostname = "web#{machine_id}" + machine.vm.network "forwarded_port", guest: 80, host: "808#{machine_id+1}" + machine.vm.network "private_network", ip: "192.168.56.#{machine_id+1}" + # Only execute once the Ansible provisioner, + # when all the machines are up and ready. + if machine_id == N + machine.vm.provision :ansible do |ansible| + # Disable default limit to connect to all the machines + ansible.limit = "all" + ansible.playbook = "ansible/playbook.yaml" + ansible.vault_password_file = "vault_pass" + end + end + end end end diff --git a/ansible/playbook.yaml b/ansible/playbook.yaml index 2f52b0e..b72635f 100644 --- a/ansible/playbook.yaml +++ b/ansible/playbook.yaml @@ -11,8 +11,36 @@ db_user: wp php: version: 8.1 + gluster: + brick_dir: /srv/data/wordpress + mount_dir: /var/www/html/wordpress + vol_name: wordpress_vol + dir_owner: wordpress + dir_group: www-data + cluster: + - 192.168.56.2 + - 192.168.56.3 + - 192.168.56.4 + pre_tasks: + - debug: var=ansible_all_ipv4_addresses[1] + # we need gluster on all servers to configure peering + - name: Install gluster + ansible.builtin.apt: + update_cache: true + cache_valid_time: 3600 + name: + - glusterfs-server + become: true + + - name: start service + ansible.builtin.service: + name: glusterd + state: started + enabled: true + become: true roles: - nginx - mysql - php + - glusterfs - wordpress diff --git a/ansible/roles/glusterfs/tasks/main.yaml b/ansible/roles/glusterfs/tasks/main.yaml new file mode 100644 index 0000000..551ecc5 --- /dev/null +++ b/ansible/roles/glusterfs/tasks/main.yaml @@ -0,0 +1,52 @@ +--- +- name: Create brick dir + ansible.builtin.file: + path: "{{ gluster.brick_dir }}" + state: directory + become: true + +- name: Create volume + gluster.gluster.gluster_volume: + state: present + name: "{{ gluster.vol_name }}" + bricks: "{{ gluster.brick_dir }}" + cluster: "{{ gluster.cluster }}" + force: true + run_once: true + become: true + +- name: Start volume + gluster.gluster.gluster_volume: + state: started + name: "{{ gluster.vol_name }}" + run_once: true + become: true + +- name: Mount volume + mount: + name: "{{ gluster.mount_dir }}" + src: "{{ ansible_all_ipv4_addresses[1] }}:/{{ gluster.vol_name }}" + fstype: glusterfs + opts: "defaults,_netdev" + state: mounted + become: true + +- name: Add group + ansible.builtin.group: + name: "{{ gluster.dir_owner }}" + become: true + +- name: Add user + ansible.builtin.user: + name: "{{ gluster.dir_owner }}" + group: "{{ gluster.dir_group }}" + become: true + +- name: Change ownership + file: + path: "{{ gluster.mount_dir }}" + owner: "{{ gluster.dir_owner }}" + group: "{{ gluster.dir_group }}" + state: directory + mode: "0755" + become: true diff --git a/ansible/roles/php/handlers/main.yaml b/ansible/roles/php/handlers/main.yaml index 056585b..bc0c65e 100644 --- a/ansible/roles/php/handlers/main.yaml +++ b/ansible/roles/php/handlers/main.yaml @@ -3,3 +3,5 @@ service: name: php{{ php.version }}-fpm state: restarted + enabled: true + become: true diff --git a/ansible/roles/wordpress/tasks/main.yaml b/ansible/roles/wordpress/tasks/main.yaml index 03e24a4..26bbd99 100644 --- a/ansible/roles/wordpress/tasks/main.yaml +++ b/ansible/roles/wordpress/tasks/main.yaml @@ -3,12 +3,14 @@ ansible.builtin.get_url: url: https://de.wordpress.org/latest-de_DE.tar.gz dest: /tmp/wordpress.tar.gz + run_once: true - name: Extract wordpress ansible.builtin.unarchive: src: /tmp/wordpress.tar.gz dest: "{{ wordpress.dir | dirname }}" remote_src: true + run_once: true become: true - name: Add wordpress group @@ -26,14 +28,5 @@ ansible.builtin.template: src: "wp-config.php.j2" dest: "{{ wordpress.dir }}/wp-config.php" + run_once: true become: true - -- name: Change ownership of wordpress installation - ansible.builtin.file: - path: "{{ wordpress.dir }}" - owner: wordpress - group: www-data - state: directory - recurse: true - become: true -