I have been using Ansible for very basic playbooks. I now want to have a simply playbook that connected to my asterisk PBX, and lookup a value in the my SQL table. I have tried many various combinations to get this working, but keep getting errors. Can someone help with this simply playbook? I dont know if the issue is bad YAML formatting, or something else. Thank you in advance!
ERROR! couldn’t resolve module/action ‘community.mysql.mysql_query’. This often indicates a misspelling, missing collection, or incorrect module path.
The error appears to be in ‘/etc/ansible/playbooks/get_trunk_name.yml’: line 8, column 5, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
tasks:
name: Select trunk names
^ here
The database is NOT running on the ansible server. The ansible server is not running on my local PC , but rather a server at a colo at a different IP. So I can’t use the mysql client from that server.
SO this is what happens when I check if the module exists when logged in as a normal user:
[WARNING]: module community.mysql.mysql_query not found in:
/home/normaluser/.ansible/plugins/modules:/usr/share/ansible/plugins/modules:/usr/lib/python3.6/site-
packages/ansible/modules
If I run that same check when logged in as root, it appears like I get the appropriate output with mysql information.
But I still get the error when running the syntax check:
ERROR! ‘community.mysql.mysql_query’ is not a valid attribute for a Play
The error appears to be in ‘/etc/ansible/playbooks/get_trunk_name.yml’: line 12, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
ERROR! ‘community.mysql.mysql_query’ is not a valid attribute for a Play
The error appears to be in ‘/etc/ansible/playbooks/get_trunk_name.yml’: line 12, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
SO this is what happens when I check if the module exists when logged in as a normal user:
[WARNING]: module community.mysql.mysql_query not found in:
/home/normaluser/.ansible/plugins/modules:/usr/share/ansible/plugins/modules:/usr/lib/python3.6/site-
packages/ansible/modules
If I run that same check when logged in as root, it appears like I get the appropriate output with mysql information.
SO, you installed the collection as root, probably into /root/.ansible/plugins/modules
unprivileged normaluser does not have /root/.ansible/plugins/modules in the
$ANSIBLE_COLLECTIONS_PATHS
one solution would be: install it again as normaluser
But I still get the error when running the syntax check:
ERROR! 'community.mysql.mysql_query' is not a valid attribute for a Play
The error appears to be in '/etc/ansible/playbooks/get_trunk_name.yml': line 12, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: get trunk names
^ here
SO, syntax went wrong? Wrong indentation?
- name: get trunk names
community.mysql.mysql_query:
it should be (note the spaces)
- name: get trunk names
community.mysql.mysql_query:
OK, thank you. I think I am getting closer. I installed as the normal user. Now I am just getting another error. This is the file:
name: Select trunk name from pbx
hosts: demoserver
become: true
become_user: root
name: get trunk names
community.mysql.mysql_query:
login_db: asterisk
login_user: root
login_password:
query: SELECT * FROM trunks
…
This is the error now:
ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: Expecting value: line 1 column 1 (char 0)
Syntax Error while loading YAML.
did not find expected ‘-’ indicator
The error appears to be in ‘/etc/ansible/playbooks/get_trunk_name.yml’: line 9, column 1, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
name: get trunk names
community.mysql.mysql_query:
^ here
Wrong indentation.
Take a close look at the example and make sure you get the indentation right.
I appreciate all the help so far. I just simply do not understand what I am doing wrong with the spacing, indentation, etc. It all looks the exact same to me in the examples that are posted to me here. I don’t get it. I am looking at other examples of working play books that I have, and they work perfectly. Would it be possible for someone to explain in detail where specifically the problem is? Or provide a working example with proper spacing/indentation? (currently banging head against keyboard)
Hello Adam,
1 - I think the module isn’t loaded on the control node. I think you need to do load it locally because its complaining it cannot find the module.
2 - Ensure you have a mysql connector on the control node (most common is PyMySQL)
In summary: after ansible-galaxy collection install community.mysql
you should copy the modules into an adhoc directory (check which ones by running ansible --version)
cp -p ~/.ansible/collections/ansible_collections/community/mysql/plugins/modules/* /home/steve/.ansible/plugins/modules/
if it works, you should be able to check that with ansible localhost -m mysql_query for example or even ansible-doc mysql_query
2.9 is before the collection split, so the available version of the module is called simply mysql_query. Unless you have a specific reason to use the collection (and have correctly installed the collection locally), you should not use community.mysql.mysql_query with 2.9.x.
As a followup, to learn how to do some proper formatting, I would like to format my output and export it to a file. For example, I successfully get the following output below. In an ideal world, I could filter this output and put it into a file. I would like something like this: