Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save koichirok/3a2a0470a3c6095e8d8007fc98729775 to your computer and use it in GitHub Desktop.
Save koichirok/3a2a0470a3c6095e8d8007fc98729775 to your computer and use it in GitHub Desktop.
ansible cannot store the externally obtained string "yes" as-is as "fact"
---
- name: host
hosts: localhost
gather_facts: false
tasks:
- block:
- name: show ansible version
ansible.builtin.debug:
var: ansible_version.full
- name: create "yes.txt" which content is "yes"
ansible.builtin.copy:
dest: "yes.txt"
content: "yes"
mode: 0644
- name: load content of "yes.txt"
ansible.builtin.slurp:
path: "yes.txt"
register: slurped_yes
- name: show content of "yes.txt"
ansible.builtin.debug:
msg: "{{ slurped_yes.content | b64decode }}"
- name: set content of yes.txt as "should_be_string_yes" fact
ansible.builtin.set_fact:
should_be_string_yes: "{{ slurped_yes.content | b64decode }}"
- name: show value of "should_be_string_yes" fact
ansible.builtin.debug:
var: should_be_string_yes
- name: assert "should_be_string_yes" == "yes"
ansible.builtin.assert:
that:
- should_be_string_yes == "yes"
always:
- name: delete created "yes.txt"
ansible.builtin.file:
path: "yes.txt"
state: absent
@koichirok
Copy link
Author

koichirok commented Apr 3, 2022

Execution results with ansible 2.12.3:

% ansible-playbook ansible-convert-externally-obtained-string-yes-to-true.yml

PLAY [host] *****************************************************************************

TASK [show ansible version] *************************************************************
ok: [localhost] => {
    "ansible_version.full": "2.12.3"
}

TASK [create "yes.txt" which content is "yes"] ******************************************
changed: [localhost]

TASK [load content of "yes.txt"] ********************************************************
ok: [localhost]

TASK [show content of "yes.txt"] ********************************************************
ok: [localhost] => {
    "msg": "yes"
}

TASK [set content of yes.txt as "should_be_string_yes" fact] ****************************
ok: [localhost]

TASK [show value of "should_be_string_yes" fact] ****************************************
ok: [localhost] => {
    "should_be_string_yes": true
}

TASK [assert "should_be_string_yes" == "yes"] *******************************************
fatal: [localhost]: FAILED! => {
    "assertion": "should_be_string_yes == \"yes\"",
    "changed": false,
    "evaluated_to": false,
    "msg": "Assertion failed"
}

TASK [delete created "yes.txt"] *********************************************************
changed: [localhost]

PLAY RECAP ******************************************************************************
localhost                  : ok=7    changed=2    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment