4
1

More than 3 years have passed since last update.

Ansibleでファイル・フォルダ操作してみる

Last updated at Posted at 2019-11-04

概要

Ansibleで操作対象サーバ(vagrant - centos7)にファイルやフォルダの操作をしてみる。

structure.jpeg

ファイル構成

├ hosts            <- Inventryファイル
├ site.yml         <- Playbookファイル
└ Vagrantfile

Inventryファイル作成

操作対象サーバの設定を行います。
※ vagrantで起動するCentOS7を対象とする

hosts
vagrant-machine ansible_host=127.0.0.1 ansible_port=2222 ansible_user=vagrant ansible_ssh_private_key_file=.vagrant/machines/default/virtualhost/private_key

Playbookファイル作成

ベースとなるPlaybookファイルを作成する。

site.yml
---
- name: Playbook Sample
  hosts: all
  become: true
  tasks:
    - name: install libselinux-python
      yum:
        name: libselinux-python
        state: present

    - name: install EPEL Repository
      yum:
        name: epel-release
        state: present

※ root権限で実行 (become: true)
※ SELinux操作ライブラリ追加
※ EPELリポジトリ追加

ファイル作成 (Playbook)

ファイルを作成し、所有者やアクセス権限を変更してみる。

site.yml
---
- name: Playbook Sample
  hosts: all
  become: true
  tasks:
    - name: install libselinux-python
      yum:
        name: libselinux-python
        state: present

    - name: install EPEL Repository
      yum:
        name: epel-release
        state: present

    - name: ファル(/tmp/hoge.txt)を作成する
      file:
        path: /tmp/hoge.txt
        state: touch

    - name: ファル(/tmp/hoge.txt)の属性を操作する
      file:
        path: /tmp/hoge.txt
        state: file
        owner: vagrant
        group: vagrant
        mode: "u=rwx,g=rx,o=rx"

playbookを実行する。

$ ansible-playbook -i hosts site.yml 

フォルダ作成 (Playbook)

フォルダを作成し、所有者(vagrant)やアクセス権限(0755)を変更してみる。

site.yml
---
- name: Playbook Sample
  hosts: all
  become: true
  tasks:
    - name: install libselinux-python
      yum:
        name: libselinux-python
        state: present

    - name: install EPEL Repository
      yum:
        name: epel-release
        state: present

    - name: フォルダ(/tmp/sample)を作成する
      file:
        path: /tmp/sample
        state: directory
        owner: vagrant
        group: vagrant
        mode: "u=rwx,g=rx,o=rx"
        recurse: true

playbookを実行する。

$ ansible-playbook -i hosts site.yml 

シンボリックリンク作成 (Playbook)

シンボリックリンクを作成し、所有者(vagrant)を変更してみる。

site.yml
---
- name: Playbook Sample
  hosts: all
  become: true
  tasks:
    - name: install libselinux-python
      yum:
        name: libselinux-python
        state: present

    - name: install EPEL Repository
      yum:
        name: epel-release
        state: present

    - name: フォルダ(/tmp/sample)を作成する
      file:
        path: /tmp/sample
        state: directory
        owner: vagrant
        group: vagrant
        mode: "u=rwx,g=rx,o=rx"
        recurse: true

    - name: シンボリックリンク(/tmp/sample)を作成する
      file:
        src: /tmp/sample
        dest: /tmp/link_to_sample
        state: link
        owner: vagrant
        group: vagrant

playbookを実行する。

$ ansible-playbook -i hosts site.yml 

ファイル転送 (Playbook)

操作元のファイルを操作対象サーバへ転送し、所有者(vagrant)とアクセス権限(0644)を変更する。

site.yml
---
- name: Playbook Sample
  hosts: all
  become: true
  tasks:
    - name: install libselinux-python
      yum:
        name: libselinux-python
        state: present

    - name: install EPEL Repository
      yum:
        name: epel-release
        state: present

    - name: ファイル(./origin_file/hoge.txt)を転送する
      copy:
        src: origin_file/hoge.txt
        dest: /tmp/hoge.txt
        owner: vagrant
        group: vagrant
        mode: "u=rw,g=r,o=r"

playbookを実行する。

$ ansible-playbook -i hosts site.yml 

フォルダ転送 (Playbook)

操作元のフォルダを操作対象サーバへ転送し、所有者(vagrant)とアクセス権限(0755)を変更する。

site.yml
---
- name: Playbook Sample
  hosts: all
  become: true
  tasks:
    - name: install libselinux-python
      yum:
        name: libselinux-python
        state: present

    - name: install EPEL Repository
      yum:
        name: epel-release
        state: present

    - name: rsyncコマンドをインストールする
      yum:
        name: rsync
        state: present

    - name: フォルダ(./origin_file/)を転送する
      synchronize:
        src: origin_file
        dest: /tmp/

    - name: フォルダ(./origin_file/)の属性を設定する
      file:
        path: /tmp/origin_file
        state: directory
        owner: vagrant
        group: vagrant
        recurse: true

※ synchronizeモジュール: 転送元と転送先の各サーバに rsyncコマンドが必要

playbookを実行する。

$ ansible-playbook -i hosts site.yml 

以上

4
1
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
4
1