17
15

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 の template ファイルで item を利用する方法

Last updated at Posted at 2016-01-21

前書き

1つだけの単語の違いだけで template ファイルを複数用意して使うのは格好が悪いと思っていました。item を template ファイルで使えないのかと試したところ、以外と単純で簡単に使えることが最近分かりました。結果を先に言うと、with_items を用意し、template ファイルの配置したい箇所で変数 item を使うだけです。
Vagrant を使って簡単な例を紹介します。

準備

ディレクトリ構造

.
├── Vagrantfile
├── example.j2   # template ファイル
└── playbook.yml # playbook

Vagrantfile

Vagrantfile
Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu/trusty64"
  config.vm.provision "ansible" do |ansible|                                                                                                                                                                                                                                
    ansible.playbook = "playbook.yml"                                                                                                                                                                                                                                           
  end                                                                                                                                                                                                                                                                       
end 

template ファイルで item を利用する

template ファイル ( example.j2 ) を ゲスト OS の ~/ ( /home/vagrant ) 以下に配置する例です。

配列

playbook.yml
- hosts: all
  user: vagrant
  tasks:
  - template: src=example.j2 dest=~/{{ item }}
    with_items:
      - John
      - Alice
example.j2
{{ item }}
ansible-playbook
$ vagrant provision
==> default: Running provisioner: ansible...

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

GATHERING FACTS *************************************************************** 
ok: [default]

TASK: [template src=example.j2 dest=~/{{ item }}] ***************************** 
changed: [default] => (item=John)
changed: [default] => (item=Alice)

PLAY RECAP ******************************************************************** 
default                    : ok=2    changed=1    unreachable=0    failed=0
ssh
$ vagrant ssh
結果
$ cat John 
John
$ cat Alice 
Alice

連想配列

playbook.yml
- hosts: all
  user: vagrant
  tasks:
  - template: src=example.j2 dest=~/{{ item.name }}
    with_items:
      - {name: John, message: "Hello John!"}
      - {name: Alice, message: "Hello Alice!"}
example.j2
{{ item.name }}
{{ item.message }}
ansible-playbook
$ vagrant provision
==> default: Running provisioner: ansible...

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

GATHERING FACTS *************************************************************** 
ok: [default]

TASK: [template src=example.j2 dest=~/{{ item.name }}] ************************ 
changed: [default] => (item={'message': 'Hello John!', 'name': 'John'})
changed: [default] => (item={'message': 'Hello Alice!', 'name': 'Alice'})

PLAY RECAP ******************************************************************** 
default                    : ok=2    changed=1    unreachable=0    failed=0
ssh
$ vagrant ssh
結果
$ cat John 
John
Hello John!
$ cat Alice 
Alice
Hello Alice!
17
15
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
17
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?