Ansible

[Ansible] assert の基本 ( 文字列を比較する )

playbookの例

- hosts: localhost
  vars:
    some_string: MY NAME IS ME
    assert_string: MY NAME IS ME

    # 変数同士を比較する
    - assert:
        that: some_string == assert_string

    # 変数とベタ書きの文字列を比較する
    - assert:
        that: some_string == 'MY NAME IS ME'

    # 変数と「ansible的な変数展開後の文字列」を比較する
    - assert:
        that: some_string == '{{ assert_string }}'

   # ベタ書きの文字列同士を比較する ( YAML文法的にダブルクオートで囲っているが、ansibleの処理ためではない )
    - assert:
        that: "'MY NAME IS ME' == 'MY NAME IS ME'"

実行結果

全ての assert にパスする。

TASK [assert] ************************************************************************************************************************************
ok: [localhost] => {
    "changed": false, 
    "msg": "All assertions passed"
}

TASK [assert] ************************************************************************************************************************************
ok: [localhost] => {
    "changed": false, 
    "msg": "All assertions passed"
}

TASK [assert] ************************************************************************************************************************************
ok: [localhost] => {
    "changed": false, 
    "msg": "All assertions passed"
}

TASK [assert] ************************************************************************************************************************************
ok: [localhost] => {
    "changed": false, 
    "msg": "All assertions passed"
}

TASK [assert] ************************************************************************************************************************************
ok: [localhost] => {
    "changed": false, 
    "msg": "All assertions passed"
}

TASK [assert] ************************************************************************************************************************************
ok: [localhost] => {
    "changed": false, 
    "msg": "All assertions passed"
}

PLAY RECAP ***************************************************************************************************************************************
localhost                  : ok=7    changed=0    unreachable=0    failed=0   

環境

  • ansible 2.6.0

参考