Skip to content

Instantly share code, notes, and snippets.

@naavveenn
Created June 27, 2018 14:58
Show Gist options
  • Save naavveenn/1e4fb8b5837ce9c1a189d7e88ee8a493 to your computer and use it in GitHub Desktop.
Save naavveenn/1e4fb8b5837ce9c1a189d7e88ee8a493 to your computer and use it in GitHub Desktop.
This playbook checks the city_name in the table and if the city_name doesn't exist then it will insert the city_name into the table.
---
- hosts: local
connection: local
tasks:
- name: check city in DB
mysql_query:
login_host: localhost
login_user: naveen
login_password: abc
db: test_db
query: "select * from city where name like '{{ city_name }}' "
ignore_errors: True
register: query_output
- debug:
msg: "The city name is {{ query_output.result.0.name }} and the country code is {{ query_output.result.0.country_code }}"
ignore_errors: True
- name: insert city
mysql_query:
login_host: localhost
login_user: naveen
login_password: abc
db: test_db
query: insert into `city`(id, country_code, country_name, lat, lng, name, region_id, state_id, enabled) values("1","US","United States", "45.78", "-59.654", "{{ city_name }}", "1", "1", "1");
when: query_output.result.0 is not defined
register: out
- debug:
var: out
@naavveenn
Copy link
Author

ansible-playbook query.yml --extra-vars 'city_name=flint'
###Make sure to add python-mysql module in the library## Please refer the below link for mysql
https://github.com/teemops/ansible-modules

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment