てくなべ (tekunabe)

ansible / network automation / 学習メモ

[Ansible] Jinja2 構文を展開せずに文字列そのままとして指定する

通常は変数展開される

tower_credential_type モジュールを利用して、カスタムクレデンシャルの injectors オプションを利用して、以下のような値を投入する Playbook を作ろうと思ったことがありました。

f:id:akira6592:20200405125246p:plain
こういうのを登録したい

しかし、以下の「ここ」のように普通にしていしまうと、custom_usercustom_password という変数を展開しようとして、変数が undifined というエラーになってしまいます。

- name: cred_type_custom
  description: my credential type
  kind: cloud
  inputs:
    fields:
      - type: string
        id: custom_user
        label: custom_user
      - type: string
        id: custom_password
        label: custom_password
        secret: true
    required:
      - custom_user
      - custom_password
  injectors:
    extra_vars:
      custom_user: '{{ custom_user }}'    # ここ
      custom_password: '{{ custom_password }}'    # ここ

!unsafe タグを使用する

以下のように、!unsafe タグを付けると展開されなくなり、文字列として指定できます。

...(略)...
      extra_vars:
        custom_user: !unsafe '{{ custom_user }}'
        custom_password: !unsafe '{{ custom_password }}'

他に良い方法ありそうですが思いつかず・・・

参考

docs.ansible.com