I need to write an ansible module using bash (since I do not know python). This is the shell module I used in my playbook that worked:
- name: Finding out what web server it uses
shell: "lsof -i :80 | grep LISTEN | cut -d ' ' -f 1"
register: result
- name: output the result of what web server it uses
debug: msg="{{ result.stdout_lines|first }}"
And this is the wbsrv.sh bash script located where ansible.cfg wants it.
#!/bin/bash
lsof -i :80 | grep LISTEN | cut -d ' ' -f 1
if [ $? == 0 ]; then
printf '{"changed": true, "rc": 0}'
else
printf '{"failed": true, "msg": "Something went wrong", "rc": 1}'
fi
And so I changed my playbook to this
- name: Finding out what web server it uses
wbsrv:
register: result
- name: output the result of what web server it uses
debug: msg="{{ result.stdout_lines|first }}"
When I run the playbook the error happens at "TASK [output the result of what web server it uses]":
fatal: [vm2]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout_lines'\n\nThe error appears to have been in '/home/ansible/wbsrvtest.yml': line 19, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n register: result\n - name: output the result of what web server it uses\n ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'dict object' has no attribute 'stdout_lines'"}
I tried removing the conditionals in the bash script. I tried putting echo instead of printf. I also tried setting RESULT variable to equal the line of bash code in the script and then echoing that in the if statement but all of that returned a similar error.
resultvariable to see what is stored inside it. Because you are handling it as it was the output of theshellmodule, but it's not. It's saving the output of yourwbsrvmodule. I think that is the problem.lsof -i :80 | grep LISTEN | cut -d ' ' -f 1inside a variable, then include this variable inmsg:key of the dict youprintf, so that you register a correctly syntaxed result."debug: msg={{ result.stdout_lines|first }}"- thus you can replace it withdebug: msg="{{ result }}"and walk from there.