LoginSignup
2
1

More than 3 years have passed since last update.

[Ansible]変数とベタ書きとを組み合わせて値を指定する方法(超小ネタ)

Last updated at Posted at 2019-09-01

Ansibleで変数とベタ書きとを組み合わせて値を指定する方法

先日、社内のAnsible勉強会(TUF研修)にて冒頭で挙げた方法を調べたのでまとめます。

※ちょっと待って!そんなことより TUF研修 が気になるという方はこちらを。

それでは話を戻します。
たとえば任意のファイル名を指定するとき、このように書くことがよくあります。

input = BASE_DIR + 'sample.yml'

真っ先に思いつく、変数 + 'ベタ書き'の方法でやってみました。下のymlはwordpressの初期設定をするというplaybookの一部です。

---
- hosts: web
  become: yes
  vars:   
    wp_url: http://wordpress.org/latest.tar.gz
    wp_saved_at: /tmp/wordpress.tar.gz
    wp_unarchived_at: /var/www/html/

  tasks:
    - name: get_wp
      get_url:
        url: "{{ wp_url }}"
        dest: "{{ wp_saved_at }}"
    - name: unarchive wp
      unarchive:
        src: "{{ wp_saved_at }}"
        dest: "{{ wp_unarchived_at }}"
        copy: no
    - name: chown wp user
      file:
        path: "{{ wp_unarchived_at }}" + /wordpress
        owner: ansible
        group: ansible
        recurse: yes 

結果はsyntax errorでだめでした。

ERROR! Syntax Error while loading YAML.
  did not find expected key

The error appears to have been in '/usr/etc/ansible/playbook/web.yml': line 60, column 40, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

      file:
        path: "{{ wp_unarchived_at }}" + /wordpress
                                       ^ 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 }}"

どうもAnsibleは 変数を{{ variable }}の{{}}で認識 するらしいですね。

このようにpathの 値全体を""で囲う ように変えたところうまくいきました。

- path: "{{ wp_unarchived_at }}" + /wordpress
+ path: "{{ wp_unarchived_at }}/wordpress"

---
- hosts: web
  become: yes
  vars:
    wp_url: http://wordpress.org/latest.tar.gz
    wp_saved_at: /tmp/wordpress.tar.gz
    wp_unarchived_at: /var/www/html/

  tasks:
    - name: get_wp
      get_url:
        url: "{{ wp_url }}"
        dest: "{{ wp_saved_at }}"
    - name: unarchive wp
      unarchive:
        src: "{{ wp_saved_at }}"
        dest: "{{ wp_unarchived_at }}"
        copy: no
    - name: chown wp user
      file:
        path: "{{ wp_unarchived_at }}/wordpress"
        owner: ansible
        group: ansible
        recurse: yes 

なお、""を外しても同じくsyntax errorでした。

- path: "{{ wp_unarchived_at }}/wordpress"
+ path: {{ wp_unarchived_at }}/wordpress

文法ミスがないかだけ確認する方法

今回のような文法ミスがないか確認するうえで、ansible-playbookコマンドの末尾に--syntax-checkを追加するとコマンド自体は空打ちして文法的に間違っていないか確認してくれます。

文法的に間違っている場合はこのように出力されます。

$ ansible-playbook playbook/web.yml --syntax-check
ERROR! Syntax Error while loading YAML.
  did not find expected key

The error appears to have been in '/usr/etc/ansible/playbook/web.yml': line 64, column 37, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

        #path: "{{ wp_unarchived_at }}/wordpress"
        path: {{ wp_unarchived_at }}/wordpress
                                    ^ 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 }}"

文法的に正しい場合はplaybookのパスが出力されます。

$ ansible-playbook playbook/web.yml --syntax-check

playbook: playbook/web.yml

余談ですが、文法チェックしてくれるウェブサイトもちらほらあるので、GUIで確認したい方はぜひ。
yamllint

P.S. Twitterもやってるのでフォローしていただけると泣いて喜びます!
@gkzvoice

2
1
1

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