LoginSignup
0
0

ansibleで圧縮ファイルを作成する

Posted at

ansibleで圧縮ファイルを作成する

★ この記事は3年前に執筆し、投稿し忘れていたものを投稿したものです

背景

お仕事で、Ansibleでtar.gzファイルを作成する必要がありました。
実際の作業は私自身ではないのですが、会話に参加できるよう、自己啓発を兼ねて試してみました。

参考

community.general.archive – Creates a compressed archive of one or more files or trees
Community General Collection - Ansible Galaxy

どうすればできる?

調べてみると、Ansibleには「community.general.archive」があるようで、こちらを使用すれば簡単に圧縮ファイルが作成できるようです。
ただ、このモジュールは最初からインストールはされていないようで、あとから「comunity.general」をインストールする必要があることがわかりました。

検証ファイル構成

.
├── inventory
├── playbook.yml
├── readme.md
├── work
│   ├── a.txt
│   └── b.txt

インベントリファイル

今回はお試しなのでlocalhost、自サーバをターゲットとします。

./inventory

xxx.xxx.xxx.xxx

プレイブック

動作確認もかねて、pingモジュールを最初に実行したあと、Archiveモジュールを実行します。

./playbook.yml

- hosts: all
  tasks:

  - name: ping
    ping:
    register: result

  - name: archive
    community.general.archive:
      path: /home/xxxxx/job/ansible/archive/work
      dest: /home/xxxxx/job/ansible/archive/work.tgz

手順

  • archiveモジュールを利用するために、community.generalをインストール
$ ansible-galaxy collection install community.general
Process install dependency map
Starting collection install process
Installing 'community.general:3.5.0' to '/home/xxxxx/.ansible/collections/ansible_collections/community/general'
  • tgzファイルの作成をテスト
$ ansible-playbook --ask-pass -i inventory playbook.yml
SSH password:

PLAY [all] *************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************
[DEPRECATION WARNING]: Distribution Ubuntu 18.04 on host xxx.xxx.xxx.xxx should use /usr/bin/python3, but is using
/usr/bin/python for backward compatibility with prior Ansible releases. A future Ansible release will default to using
the discovered platform python for this host. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. This feature
 will be removed in version 2.12. Deprecation warnings can be disabled by setting deprecation_warnings=False in
ansible.cfg.
ok: [xxx.xxx.xxx.xxx]

TASK [ping] ************************************************************************************************************
ok: [xxx.xxx.xxx.xxx]

TASK [archive] *********************************************************************************************************
changed: [xxx.xxx.xxx.xxx]

PLAY RECAP *************************************************************************************************************
xxx.xxx.xxx.xxx              : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

おわりに

Ansibleをインストールしただけでは実現できないようなことも、追加モジュールをインストールすることでいろいろなことができることがわかりました。
ガッツリしたAnsibleを記述したことはありませんが、こういう小さなモジュールを組み合わせることでガッツリした処理も書けるようになると思います。
「小さなことからコツコツと。」
名言だと思います。

実はこの検証には続きがあり、この検証ができたからこそ次の検証に繋がりました。
まさに「小さなことからコツコツと。」をつなぎ合わせることになりました。

0
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
0
0