Search code examples
wordpressubuntuansibleextract

Ansible Ubuntu installation - expand wordpress tar.gz to root of /var/www


First time working with Ansible. I have a playbook that sets up wordpress on Ubuntu Trusty and Nginx / MySQL.

I am trying to get the extraction of the wordpress.latest.tar.gz as part of root www folder. So what I want is:

/var/www
   - wp-admin
   - wp-content
   - wp-includes

When I point the extract command to /var/www I get:

ok: [XXX.XX.XX.XXX] => {"changed": false, "msg": "skipped, since /var/www exists"}

When I point it to var/www/wordpress (which does not exist), it works and creates the folder.

My task syntax (that works but places wordpress in its own folder) is:

- name: "Extract archive"
  sudo: yes   
  unarchive: "src=/srv/wordpress.latest.tar.gz 
              dest=/var/www copy=no 
              owner=www-data 
              group=www-data 
              creates=/var/www/wordpress"

I have tried the syntax below but it skips the extraction per above desciption.

- name: "Extract archive"
  sudo: yes   
  unarchive: "src=/srv/wordpress.latest.tar.gz 
              dest=/var/www copy=no 
              owner=www-data 
              group=www-data 
              creates=/var/www"

What have I misunderstood? Thanks!


Solution

  • There are some caveats before I answer:

    • I have no experience with the tool you mention
    • The optional solutions below should be considered a workaround, it's obviously not a direct solution to your problem
    • they depends on your confidence at the command line and the fact yo have access to it (since it's a ubuntu install, I'm guessing it's your own box).

    Experimentation

    But first, let's give something else a go. This option doesn't use any terminal and works only with the tools you have, but I'd do it in a another directory to make sure you don't mess things up.

    I think if you remove the creates=/var/www it may work for you. The reason it may be failing is because the dir is already there, so it's trying to keep you safe. Since you have specified the destination directory anyway, just go with that.

    To test it you could first create a temp dir under /var/www so that you have the following path that exists: /var/www/temp123.

    Then create your playbook with the following options:

    - name: "Extract archive"
      sudo: yes   
      unarchive: "src=/srv/wordpress.latest.tar.gz 
                  dest=/var/www/temp123 copy=no 
                  owner=www-data 
                  group=www-data
                  creates=/var/www/temp123"
    

    If you get the same error, we're on track.

    Now recreate the playbook without the creates "play" (pun intended) so that it looks like this:

    - name: "Extract archive"
      sudo: yes   
      unarchive: "src=/srv/wordpress.latest.tar.gz 
                  dest=/var/www/temp123 copy=no 
                  owner=www-data 
                  group=www-data"
    

    If that works as expected, go ahead and now remove the dest: temp play option.

    Your final play should look like:

    - name: "Extract archive"
      sudo: yes   
      unarchive: "src=/srv/wordpress.latest.tar.gz 
                  dest=/var/www copy=no 
                  owner=www-data 
                  group=www-data"
    

    I hope this helps

    Don't forget to remove all the temp work as you.

    If that didn't work, you could always just try doing it yourself:

    Option 1: create it in a subdirectory and move it

    • install it as you would in a subdirectory–you define a distinct dirname under /var/www/{yourTempDirNameWithoutCurlyBraces}
    • at the terminal and in that temp directory:

      mv * ..

    • you may then remove that directory

    Option 2: install it from the command line

    If you have the file on the server already, change into your/var/www directory and:

    tar -xzvpf /srv/wordpress.latest.tar.gz
         x - extract
         z - uncompress
         v - verbose (see the ouput—this flag is optional)
         p - preserve file permissions (see note)
         f - the file we're extracting
    
     note: I'm not sure what file permissions will be inside the archive
          but I would expect they're set for the webserver already. 
          If they're not, you could always issue:
    
     chown -R www-data:www-data /var/www/
    

    Caveat Reintegrated :D

    In the absence of a direct answer already here, I'm only posting this to get you on your way. Again, I haven't used Ansible or am familiar with it. But installing WordPress yourself is not a difficult task if you want to breakaway and give it a go. Although I don't know the extent of the automation Ansible provides and will apologise if doing it this way just doesn't help your cause

    Good luck.