1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Ansible】unarchive モジュールで展開先のディレクトリ名を変えたい

Last updated at Posted at 2026-01-19

複数の WordPress を Ansible でインストールするときに、それぞれ別のディレクトリ名で WordPress を展開したいときってありますよね。私はあります。(例:site-a, site-b など)

いったんインストールして、ディレクトリ名を変更するのもアリですが、ここでは unarchive モジュールのオプションを使って、異なる名前のディレクトリに展開しましょう。

普通に解凍すると、以下のように圧縮ファイルと同階層に wordpress ディレクトリが作られてそこに展開されます。

tar -xzvf wordpress.ja.tar.gz

├── wordpress.ja.tar.gz
│
└── wordpress
    ├── index.php
    ├── readme.html
    ├── wp-admin
    │   ├── aaa.php
    │   └── bbb.php
    ├── wp-contents
    │   ├── xxx.php
    │   └── yyy.php
    └── wp-includes
        └── mmm.php

tar コマンドには、--strip-components というオプションがあり、これに 1 を指定することにより最上位の階層 (上記の例では wordpress/) を取り除いて展開することができます。

tar -xzvf wordpress.ja.tar.gz --strip-components=1

├── wordpress.ja.tar.gz
│
├── index.php
├── readme.html
├── wp-admin
│   ├── aaa.php
│   └── bbb.php
├── wp-contents
│   ├── xxx.php
│   └── yyy.php
└── wp-includes
    └── mmm.php

同様のオプションを Ansible の unarchive モジュールでも利用することができます。

あらかじめ、異なる名前の展開先ディレクトリを作っておき、そこを dest として、 unachive モジュールの extra_opts パラメータに --strip-components=1 オプションを指定すれば、ディレクトリ名を変えて tar.gz ファイルを展開することができます。

- name: create foobar directory
  ansible.builtin.file:
    path: /var/www/foobar
    state: directory
    mode: '0755'

- name: unarchive wordpress gzip file to foobar
  ansible.builtin.unarchive:
    src: /usr/local/src/wordpress.ja.tar.gz
    dest: /var/www/foobar
    remote_src: yes
    extra_opts:
      - --strip-components=1

お試しあれ。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?