LoginSignup
0
0

More than 5 years have passed since last update.

備忘録 ChefのFile.joinと同じような事をAnsibleのYAMLでやりたい

Posted at

Ansibleを初めて、基本的な動きはおおよそ理解できてきましたが、どうしても良く分からなかったことは、「ChefのFile.joinと同じような事をAnsibleのYAMLではどうやるのか?」ということ。

例えば、ChefでAttributeの値を複数並べてディレクトリパスを設定したい場合
attributesファイルに設定しておけば、

default['linuxos']['work_dir'] = '/work'
default['linuxos']['file_name'] = 'file.txt'

レシピの中で、以下のようにFile.joinメソッドを利用することで、/work/file.txtというパスを設定することができる。

file = File.join(node['linuxos']['work_dir'],node['linuxos']['file_name']) 

これと同じ事をAnsibleでもやりたかったのですが、意外とはまりました。。。。

1. 失敗したyamlの内容

変数は、"{{ 変数名 }} "と囲むのであれば、"{{ 変数1 }}"/"{{ 変数2 }}" みたいな書き方でいいかと思っていました。

  • 作成したコード
install.yml
---

- hosts: web
  remote_user: ansible
  become: true
  tasks:
   - name: copy zaibbix install
     copy:
       src: "{{ files_dir}}"/"{{ zabbix_cl_mod }}"
       dest: "{{ dest_dir }}"

   - name: install zabbix agent
     yum:
       name: "{{dest_dir}}"/"{{zabbix_cl_mod}}"
       state: present
  • 変数ファイル
all.yml
---
files_dir: /home/ansible/playbooks/files
zabbix_cl_mod: zabbix-agent-3.2.11-1.el7.x86_64.rpm
dest_dir: /tmp
mode: 0644
  • 実行結果
$ ansible-playbook -i hosts ./tasks/zabbix/install.yml  -K
SUDO password:
ERROR! Syntax Error while loading YAML.


The error appears to have been in '/home/ansible/playbooks/tasks/zabbix/install.yml': line 10, column 29, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

       src: "{{ files_dir}}"/"{{ zabbix_cl_mod }}"
                            ^ 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 }}"

exception type: <class 'yaml.parser.ParserError'>
exception: while parsing a block mapping
  in "<unicode string>", line 10, column 8
did not find expected key
  in "<unicode string>", line 10, column 29

変数と変数の間のスラッシュ(/)が問題なのか、エラーとなりました。

src: "{{ files_dir}}"/"{{ zabbix_cl_mod }}"
                     ^ here

どうやら、変数を2つつなげるときは、このやり方ではないようです。。。。

2. 成功したyamlの内容

実は、"{{ files_dir}}"/"{{ zabbix_cl_mod }}" ではなく
   "{{ files_dir}}/{{ zabbix_cl_mod }}" 
と、ダブルクォーテーションは変数ごとではなく変数の最初と、つなげたあとの変数の最後で囲めばよかったようです。

  • 修正したplaybook
install.yml
---

- hosts: web
  remote_user: ansible
  become: true
  tasks:
   - name: copy zaibbix install
     copy:
       src: "{{ files_dir}}/{{ zabbix_cl_mod }}"
       dest: "{{ dest_dir }}"

   - name: install zabbix agent
     yum:
       name: "{{dest_dir}}/{{zabbix_cl_mod}}"
       state: present
  • 実行結果
$ ansible-playbook -i hosts ./tasks/zabbix/install.yml  -K
SUDO password:

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

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

TASK [copy zaibbix install] **********************************************************************
ok: [brighton002]

TASK [install zabbix agent] **********************************************************************
changed: [brighton002]

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

エラー無く実行することができました!

時間がかかりましたが、なんとかうまくできてよかったです。
今回は、teraailで質問して解決できまいしたが、ちょっとした発想の転換がたりなかったようです。

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