[Ansible]使用变量和硬编码的组合来指定值的方法(小窍门)

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 

结果由于语法错误而无效。

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: "{{ 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 

此外,即使去除“”,仍然出现语法错误。

- 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

顺便说一句,我也在Weibo上活跃,如果你能关注我,我将欣喜若狂!@gkzvoice

bannerAds