From 4334e4bae04e0336fa913f041c7387253f70cd4f Mon Sep 17 00:00:00 2001 From: Stephen Granger Date: Tue, 5 Feb 2019 11:54:15 -0800 Subject: [PATCH] Include boto3 library for running s3_sync Ansible module. Attempt to make Ansible plays idempotent. --- course_scripts/main.tf | 1 + course_scripts/s3update.yml | 30 ++++++++++++++++------- course_scripts/wordpress.yml | 46 ++++++++++++++++++++++++++---------- 3 files changed, 56 insertions(+), 21 deletions(-) diff --git a/course_scripts/main.tf b/course_scripts/main.tf index 21408e9..9730f94 100644 --- a/course_scripts/main.tf +++ b/course_scripts/main.tf @@ -455,6 +455,7 @@ cat < userdata #!/bin/bash /usr/bin/aws s3 sync s3://${aws_s3_bucket.code.bucket} /var/www/html/ /bin/touch /var/spool/cron/root +sudo /usr/bin/yum install -y python27-boto3 sudo /bin/echo '*/5 * * * * aws s3 sync s3://${aws_s3_bucket.code.bucket} /var/www/html/' >> /var/spool/cron/root EOF EOT diff --git a/course_scripts/s3update.yml b/course_scripts/s3update.yml index eb6b82f..7572876 100644 --- a/course_scripts/s3update.yml +++ b/course_scripts/s3update.yml @@ -2,12 +2,26 @@ - hosts: dev become: yes remote_user: ec2-user + tasks: - - name: Update S3 code bucket - command: aws s3 sync /var/www/html/ s3://{{ s3code }}/ --delete - - shell: echo "define('WP_SITEURL','http://dev."{{ domain }}".com');" >> wp-config.php - args: - chdir: /var/www/html/ - - shell: echo "define('WP_HOME','http://dev."{{ domain }}".com');" >> wp-config.php - args: - chdir: /var/www/html/ + - name: Update S3 code bucket + s3_sync: + region: us-west-2 + bucket: "{{ s3code }}" + delete: True + file_root: /var/www/html/ + + - name: Append WP_SITEURL to wp config + lineinfile: + regexp: "^define[(]'WP_SITEURL'" + line: "define('WP_SITEURL','http://dev.{{ domain }}.com');" + path: "/var/www/html/wp-config.php" + create: True + state: present + + - name: Append WP_HOME to wp config + lineinfile: + regexp: "^define[(]'WP_HOME'" + line: "define('WP_HOME','http://dev.{{ domain }}.com');" + path: "/var/www/html/wp-config.php" + state: present diff --git a/course_scripts/wordpress.yml b/course_scripts/wordpress.yml index 1c4caa0..300376a 100644 --- a/course_scripts/wordpress.yml +++ b/course_scripts/wordpress.yml @@ -2,23 +2,43 @@ - hosts: dev become: yes remote_user: ec2-user + + vars: + - apache_packages: + - httpd + - php + - php-mysql + tasks: - - name: Install Apache. - yum: name={{ item }} state=present - with_items: - - httpd - - php - - php-mysql - - name: Download WordPress - get_url: url=http://wordpress.org/wordpress-latest.tar.gz dest=/var/www/html/wordpress.tar.gz force=yes - - name: Extract WordPress - command: "tar xzf /var/www/html/wordpress.tar.gz -C /var/www/html --strip-components 1" + - name: Install apache + yum: + name: "{{ apache_packages }}" + state: present + + - name: Download Wordpress + get_url: + url: http://wordpress.org/wordpress-latest.tar.gz + dest: /var/www/html/wordpress.tar.gz + force: yes + + - name: Extract Wordpress + unarchive: + remote_src: True + src: /var/www/html/wordpress.tar.gz + dest: /var/www/html + extra_opts: + - "--strip-components=1" + - name: Make my directory tree readable file: path: /var/www/html/ - mode: u=rwX,g=rX,o=rX + mode: "u=rwX,g=rX,o=rX" recurse: yes owner: apache group: apache - - name: Make sure Apache is started now and at boot. - service: name=httpd state=started enabled=yes + + - name: Make sure Apache is started now and at bood + service: + name: httpd + state: started + enabled: True