Last active
May 4, 2020 17:55
-
-
Save zeitounator/25e3dfc2a660f984f05712ec2e912ccc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cat /tmp/test.yml | |
--- | |
- hosts: localhost | |
gather_facts: false | |
vars: | |
with_test: |- | |
echo username cisco privilege 15 secret 5 \$1\$.. | |
echo username test privilege 15 secret 5 \$1\$.. | |
without_test: |- | |
echo username cisco privilege 15 secret 5 \$1\$.. | |
echo username toto privilege 15 secret 5 \$1\$.. | |
vars_prompt: | |
- name: test_user | |
prompt: "test user (yes/no): " | |
defaut: yes | |
private: no | |
tasks: | |
- shell: "{{ test_user | bool | ternary(with_test, without_test) }}" | |
register: username_contents | |
changed_when: false | |
- name: debug registered vars we will use | |
debug: | |
var: "{{ item }}" | |
loop: | |
- username_contents.stdout_lines | |
- username_contents.stdout | |
- name: Check that 'test' is in stdout_lines (wrong) | |
debug: | |
msg: Found 'test' in stdout_lines (but that's probably a lie) | |
when: ('test') in username_contents.stdout_lines | |
- name: Check that 'test' is not in stdout_line (wrong) | |
debug: | |
msg: Did not find 'test' in stdout_lines (but that's probably a lie) | |
when: ('test') not in username_contents.stdout_lines | |
- name: Check that 'test' is in output (good) | |
debug: | |
msg: Found 'test' in entire output | |
when: ('test') in username_contents.stdout | |
- name: Check that 'test' is not in output (good) | |
debug: | |
msg: Did not find 'test' in entire output | |
when: ('test') not in username_contents.stdout | |
$ ansible-playbook /tmp/test.yml | |
test user (yes/no): : yes | |
PLAY [localhost] ************************************************************************************************************************************************************************************************** | |
TASK [shell] ****************************************************************************************************************************************************************************************************** | |
ok: [localhost] | |
TASK [debug registered vars we will use] ************************************************************************************************************************************************************************** | |
ok: [localhost] => (item=username_contents.stdout_lines) => { | |
"ansible_loop_var": "item", | |
"item": "username_contents.stdout_lines", | |
"username_contents.stdout_lines": [ | |
"username cisco privilege 15 secret 5 $1$..", | |
"username test privilege 15 secret 5 $1$.." | |
] | |
} | |
ok: [localhost] => (item=username_contents.stdout) => { | |
"ansible_loop_var": "item", | |
"item": "username_contents.stdout", | |
"username_contents.stdout": "username cisco privilege 15 secret 5 $1$..\nusername test privilege 15 secret 5 $1$.." | |
} | |
TASK [Check that 'test' is in stdout_lines (wrong)] *************************************************************************************************************************************************************** | |
skipping: [localhost] | |
TASK [Check that 'test' is not in stdout_line (wrong)] ************************************************************************************************************************************************************ | |
ok: [localhost] => { | |
"msg": "Did not find 'test' in stdout_lines (but that's probably a lie)" | |
} | |
TASK [Check that 'test' is in output (good)] ********************************************************************************************************************************************************************** | |
ok: [localhost] => { | |
"msg": "Found 'test' in entire output" | |
} | |
TASK [Check that 'test' is not in output (good)] ****************************************************************************************************************************************************************** | |
skipping: [localhost] | |
PLAY RECAP ******************************************************************************************************************************************************************************************************** | |
localhost : ok=4 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0 | |
$ ansible-playbook /tmp/test.yml | |
test user (yes/no): : no | |
PLAY [localhost] ************************************************************************************************************************************************************************************************** | |
TASK [shell] ****************************************************************************************************************************************************************************************************** | |
ok: [localhost] | |
TASK [debug registered vars we will use] ************************************************************************************************************************************************************************** | |
ok: [localhost] => (item=username_contents.stdout_lines) => { | |
"ansible_loop_var": "item", | |
"item": "username_contents.stdout_lines", | |
"username_contents.stdout_lines": [ | |
"username cisco privilege 15 secret 5 $1$..", | |
"username toto privilege 15 secret 5 $1$.." | |
] | |
} | |
ok: [localhost] => (item=username_contents.stdout) => { | |
"ansible_loop_var": "item", | |
"item": "username_contents.stdout", | |
"username_contents.stdout": "username cisco privilege 15 secret 5 $1$..\nusername toto privilege 15 secret 5 $1$.." | |
} | |
TASK [Check that 'test' is in stdout_lines (wrong)] *************************************************************************************************************************************************************** | |
skipping: [localhost] | |
TASK [Check that 'test' is not in stdout_line (wrong)] ************************************************************************************************************************************************************ | |
ok: [localhost] => { | |
"msg": "Did not find 'test' in stdout_lines (but that's probably a lie)" | |
} | |
TASK [Check that 'test' is in output (good)] ********************************************************************************************************************************************************************** | |
skipping: [localhost] | |
TASK [Check that 'test' is not in output (good)] ****************************************************************************************************************************************************************** | |
ok: [localhost] => { | |
"msg": "Did not find 'test' in entire output" | |
} | |
PLAY RECAP ******************************************************************************************************************************************************************************************************** | |
localhost : ok=4 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I really appreciate your help, thanks! So this one works:
when: username_contents.stdout is search('test')
.I may found a issue. When ansible generates output, it creates a list. That's why this doesn't work in my case too:
username_contents.stdout.find('test') != -1
Error:
fatal: [r5]: FAILED! => {"msg": "The conditional check 'username_contents.stdout.find(usr) != -1' failed. The error was: error while evaluating conditional (username_contents.stdout.find(usr) != -1): 'list object' has no attribute 'find'\n\nThe error appears to be in '/var/production/Cisco/configure_ospf/g_facts_copy.yml': line 23, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: check if user exists\n ^ here\n"}
Even though I've tried using python interpretation for finding string in list/dictionary
when: usr in username_contents['stdout']
but no luck so I'm guessing it's Ansible bug.