conditional on a result using a fact as the condition

Hi All,

Ansible 2.8

Trying to do something like this:

  • name: task
    shell: blah blah
    register: shell_result

  • name: do if hostname is in result
    shell: blah blah
    when: shell_result.stdout.find(inventory_hostname)

so obviously the line " when: shell_result.stdout.find(inventory_hostname)" is the problem. I have tried a few things but dont know what is the best way to achieve this (and without getting jinja templating warnings when running)

btw “inventory_hostname” is an ansible fact with the hostname

Thanks for any help

Hi Ansible

Hi All,

Ansible 2.8

Trying to do something like this:

- name: task
  shell: blah blah
  register: shell_result

- name: do if hostname is in result
  shell: blah blah
  when: shell_result.stdout.find(inventory_hostname)

so obviously the line " when: shell_result.stdout.find(inventory_hostname)" is the problem. I have tried a few things but dont know what is the best way to achieve this (and without getting jinja templating warnings when running)

You provided ambiguous and/or incomplete information.
Instead of guessing what you mean, please tell clearly:

* what you are trying to achieve
* how you are doing this
* what errors/problems you encounter
* what the relevant tasks/playbooks/code/variables look like

Thx

Hi, apologies for the lack of clarity. Nonetheless i figured out my problem… here is the working sample of the concept I was struggling with (in this case matching a variable to the shell result).

  • name: something that outputs stuff
    shell: hostname
    register: shell_result

  • debug:
    var: shell_result.stdout

  • debug:
    var: ansible_hostname

  • name: if hostname is in output then do something
    debug:
    msg: HELLO
    when: ansible_hostname in shell_result.stdout
    # I couldnt get this conditional to work with .find “when: shell_result.stdout.find(ansible_hostname)”

PLAY [localhost] **************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************************************************************************
Wednesday 31 July 2019 18:58:46 +0200 (0:00:00.029) 0:00:00.029 ********
ok: [localhost]

TASK [something that outputs stuff] *******************************************************************************************************************************************************************************************************************************************
Wednesday 31 July 2019 18:58:48 +0200 (0:00:02.079) 0:00:02.109 ********
changed: [localhost]

TASK [debug] ******************************************************************************************************************************************************************************************************************************************************************
Wednesday 31 July 2019 18:58:49 +0200 (0:00:00.392) 0:00:02.502 ********
ok: [localhost] => {
“shell_result.stdout”: “test-box”
}

TASK [debug] ******************************************************************************************************************************************************************************************************************************************************************
Wednesday 31 July 2019 18:58:49 +0200 (0:00:00.067) 0:00:02.570 ********
ok: [localhost] => {
“ansible_hostname”: “test-box”
}

TASK [if hostname is in output then do something] *****************************************************************************************************************************************************************************************************************************
Wednesday 31 July 2019 18:58:49 +0200 (0:00:00.043) 0:00:02.614 ********
ok: [localhost] => {
“msg”: “HELLO”
}