5
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

備忘録 Ansibleで変数ファイルを利用する

Posted at

Chefの場合、レシピの中で変数を使いたい場合は、attribute属性を使用していました。
Ansibleの場合も似たような感じで、変数ファイルを作成して読み込むことができます。

1. とりあえず、playbookを作成する。

備忘録 Ansibleファイルをコピーする(copyモジュール)で作成したものをベースでやってみます。

test3.yml
---
- hosts: web
  sudo: yes
  vars_files:
   - ../vars/test3.yml
  tasks:
   - name: copy a directory
     copy:
       src: "{{ src_file }}"
       dest: "{{ dest }}"
       mode: "{{ mode }}"
       backup: yes

テスト用のファイルも簡単に作っておきます。

test comment1

1. var_files

playbookの中で、var_filesオプションを利用し、変数ファイルを指定することで、playbookから変数を外だしすることが出来ます。
以下の部分が相当します。
また、今回は、変数ファイルを格納するディレクトリとして、事前に/root/ansible/varsディレクトリを作成しました。

  vars_files:
   - ../vars/test3.yml

ディレクトリ構成は以下のような感じです。

/root/
   └ ansible
       ├─ hosts             ・・・inventoryファイル
       ├─ tasks             ・・・playbook格納場所
       │   └─ test3.yml     ・・・playbook
       ├─ files             ・・・ファイル系操作用ディレクトリ
       │   └─ test3.txt     ・・・テスト用ファイル
       └─ vars        ・・・変数ファイル格納場所
           └─ test3.yml     ・・・変数ファイル

2. 変数の設定

playbookの以下の部分にあたります。

       src: "{{ src_file }}"
       dest: "{{ dest }}"
       mode: "{{ mode }}"

変数は "{{ 変数名 }}" と指定します。
昔の資料を見ているとダブルクオーテーションで囲まなくても良かったみたいですが、今は囲む必要があるようです。
囲まない場合、実行時に以下のようなエラーメッセージがでます。


The offending line appears to be:

     copy:
       src: {{ src_file }}
             ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

2.変数ファイルを作成する

実際に変数ファイルを作成してみます。

---
src_file: ../files/test3.txt
dest: /tmp
mode: 0666

3.playbookを実行する。

実際に実行してみます。

# ansible-playbook -i hosts ./task/test3.yml
[DEPRECATION WARNING]: Instead of sudo/sudo_user, use become/become_user and make sure become_method is
'sudo' (default). This feature will be removed in version 2.6. Deprecation warnings can be disabled by
setting deprecation_warnings=False in ansible.cfg.

PLAY [web] ************************************************************************************************

TASK [Gathering Facts] ************************************************************************************
ok: [brighton002]

TASK [copy a directory] ***********************************************************************************
changed: [brighton002]

PLAY RECAP ************************************************************************************************
brighton002                : ok=2    changed=1    unreachable=0    failed=0

問題無く実行できたようです。

サーバー上で確認してみます。

[root@brighton002 tmp]# pwd
/tmp
[root@brighton002 tmp]# ls -l
total 4
-rw-rw-rw- 1 root root 14 Jan 16 10:31 test3.txt
[root@brighton002 tmp]# cat test3.txt
test comment1
[root@brighton002 tmp]#

想定どおりできました!

Chefよりも扱いやすいかも・・・

5
13
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
5
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?