<p>I believe this is due to non existing variable attribute.<br />
Most likely <code>chain_from_certificate1</code> and <code>chain_from_certificate2</code> don't have <code>stdout</code></p>
<p>Did you try debug by printing the variables?<br />
With <code>stdout</code>:<br />
Did this throw similar failure?</p>
<pre><code>    - name: print variable
      debug:
        msg: &quot;{{ chain_from_certificate1.stdout }}&quot;
</code></pre>
<p>Without <code>stdout</code>:<br />
What is output of this?<br />
Will this print same output as of manually execute <code>openssl x509 -issuer -noout -in Certiticate1</code>?</p>
<pre><code>    - name: print variable
      debug:
        msg: &quot;{{ chain_from_certificate1 }}&quot;
</code></pre>
<p>My assumption is that the script set the value of <code>stdout</code> in <code>chain_from_certificate1</code> and <code>chain_from_certificate2</code> rather than the <code>stdout: &lt;value&gt;</code>. Therefor <code>stdout</code> do not exist as attribute.<br />
Or <code>chain_from_certificate1</code> and <code>chain_from_certificate2</code> contain another data structure.</p>
<p>The key is to print them out.</p>

CC BY-SA 4.0
2020-10-10T00:16:09.580Z
2020-10-10T00:33:44.063Z
2020-10-10T00:33:44.063Z
akane
<p><strong>Q: &quot;<em>My question how to compare <code>AnsibleUnsafeText</code>?</em>&quot;</strong></p>
<p>Just as usual via <code>==</code>.</p>
<p><strong>Q: &quot;<em>Is it necessary to convert them to string before comparison?</em>&quot;</strong></p>
<p>No.</p>
<p>A minimal example playbook</p>
<pre class="lang-yaml prettyprint-override"><code>---
- hosts: localhost
  become: false
  gather_facts: false

  tasks:

    - shell:
        cmd: |
          openssl x509 -issuer -noout -in &quot;/etc/pki/ca-trust/source/anchors/DC=com,DC=example,CN=immediate_one.pem&quot;
      register: cert1

    - shell:
        cmd: |
          openssl x509 -issuer -noout -in &quot;/etc/pki/ca-trust/source/anchors/DC=com,DC=example,CN=immediate_two.pem&quot;
      register: cert2

    - debug:
        msg:
          - &quot;value: '{{ cert1.stdout }}' is of type: {{ cert1.stdout | type_debug }}&quot;
          - &quot;value: '{{ cert2.stdout }}' is of type: {{ cert2.stdout | type_debug }}&quot;

    - assert:
        that:
          cert1.stdout == cert2.stdout
        fail_msg: &quot;[ERR] Different issuer&quot;
        success_msg: &quot;[OK] Identic issuer&quot;
</code></pre>
<p>will result into an output of</p>
<pre class="lang-yaml prettyprint-override"><code>TASK [debug] **************************************************************************
ok: [localhost] =&gt;
  msg:
  - 'value: ''issuer=DC = com, DC = example, CN = root'' is of type: AnsibleUnsafeText'
  - 'value: ''issuer=DC = com, DC = example, CN = root'' is of type: AnsibleUnsafeText'

TASK [assert] *************************************************************************
ok: [localhost] =&gt; changed=false
  msg: '[OK] Identic issuer'

</code></pre>

CC BY-SA 4.0
2024-08-13T12:41:24.083Z
2024-08-13T12:47:51.287Z
2024-08-13T12:47:51.287Z
U880D
<p>I am using the ansible shell module to compare the chain of 2 SSL certificate with openssl (they have the same chain):</p>
<pre><code>openssl x509 -issuer -noout -in Certiticate1
openssl x509 -issuer -noout -in Certiticate2
</code></pre>
<p>I register the output in variables chain_from_certificate1 and chain_from_certificate2 then i compares there standard output:</p>
<pre><code>- name: Compare the Chain
    fail:
      msg: Error The chains aren't the same
    when: chain_from_certificate1.stdout !=  chain_from_certificate2.stdout
</code></pre>
<p>Ansible tell me that the chains are not similar (and it's false because they are).</p>
<p>I think that the issue came from the comparison operator because the type of the variable are &quot;AnsibleUnsafeText&quot; and not string.</p>
<p>My question how to compare 2 AnsibleUnsafeText ? and it's necessary to convert them to string before comparison ?</p>
<p>Thank you in advance.</p>

CC BY-SA 4.0
2020-10-09T11:16:50.700Z
Did you manually confirm the output of `openssl x509 -issuer -noout -in Certiticate1`
and `openssl x509 -issuer -noout -in Certiticate2` are the same?
akane
CC BY-SA 4.0
2020-10-09T11:47:44.627Z
@akane Yes i aleardy check manually and they are the same
Med
<ansible>
Convert AnsibleUnsafeText to String
Med
# Convert AnsibleUnsafeText to String

(Asked by: username_0 on 2020-10-09)

I am using the ansible shell module to compare the chain of 2 SSL certificate with openssl (they have the same chain):
openssl x509 -issuer -noout -in Certiticate1
openssl x509 -issuer -noout -in Certiticate2

I register the output in variables chain_from_certificate1 and chain_from_certificate2 then i compares there standard output:
- name: Compare the Chain
    fail:
      msg: Error The chains aren't the same
    when: chain_from_certificate1.stdout !=  chain_from_certificate2.stdout

Ansible tell me that the chains are not similar (and it's false because they are).
I think that the issue came from the comparison operator because the type of the variable are "AnsibleUnsafeText" and not string.
My question how to compare 2 AnsibleUnsafeText ? and it's necessary to convert them to string before comparison ?
Thank you in advance.

**Tags:** ansible

> **Comments:**
> **username_1**: Did you manually confirm the output of `openssl x509 -issuer -noout -in Certiticate1` and `openssl x509 -issuer -noout -in Certiticate2` are the same?  
> **username_0**: @username_1 Yes i aleardy check manually and they are the same  

---

## 2 answers

### Answer by username_1 on 2020-10-10. Score: 0 | Accepted answer

I believe this is due to non existing variable attribute.
Most likely chain_from_certificate1 and chain_from_certificate2 don't have stdout
Did you try debug by printing the variables?
With stdout:
Did this throw similar failure?
    - name: print variable
      debug:
        msg: "{{ chain_from_certificate1.stdout }}"

Without stdout:
What is output of this?
Will this print same output as of manually execute openssl x509 -issuer -noout -in Certiticate1?
    - name: print variable
      debug:
        msg: "{{ chain_from_certificate1 }}"

My assumption is that the script set the value of stdout in chain_from_certificate1 and chain_from_certificate2 rather than the stdout: . Therefor stdout do not exist as attribute.
Or chain_from_certificate1 and chain_from_certificate2 contain another data structure.
The key is to print them out.


---

### Answer by username_2 on 2024-08-13. Score: 0

Q: "My question how to compare AnsibleUnsafeText?"
Just as usual via ==.
Q: "Is it necessary to convert them to string before comparison?"
No.
A minimal example playbook
---
- hosts: localhost
  become: false
  gather_facts: false

  tasks:

    - shell:
        cmd: |
          openssl x509 -issuer -noout -in "/etc/pki/ca-trust/source/anchors/DC=com,DC=example,CN=immediate_one.pem"
      register: cert1

    - shell:
        cmd: |
          openssl x509 -issuer -noout -in "/etc/pki/ca-trust/source/anchors/DC=com,DC=example,CN=immediate_two.pem"
      register: cert2

    - debug:
        msg:
          - "value: '{{ cert1.stdout }}' is of type: {{ cert1.stdout | type_debug }}"
          - "value: '{{ cert2.stdout }}' is of type: {{ cert2.stdout | type_debug }}"

    - assert:
        that:
          cert1.stdout == cert2.stdout
        fail_msg: "[ERR] Different issuer"
        success_msg: "[OK] Identic issuer"

will result into an output of
TASK [debug] **************************************************************************
ok: [localhost] =>
  msg:
  - 'value: ''issuer=DC = com, DC = example, CN = root'' is of type: AnsibleUnsafeText'
  - 'value: ''issuer=DC = com, DC = example, CN = root'' is of type: AnsibleUnsafeText'

TASK [assert] *************************************************************************
ok: [localhost] => changed=false
  msg: '[OK] Identic issuer'


---

