Ansible Yum Module is not working correctly - mysql

I am creating an ansible script to automate an LDAP configuration. However, when I do a test run on the script, I always get:
ERROR: yum is not a legal parameter of an Ansible Play
I am a bit rusty with using ansible, but I am pretty sure I got this correct (syntactically):
---
#Kicks off the installation of Tomcat and MySQL
- name: Connecting to Anssible_centos
hosts: ansible_centos
remote_user: root
- name: Retreiving MySQL RPM and Installing
yum: name=http://dev.sql.com/get/mysql157-community-release-e16-7.noarch.rpm state=present
- debug: var=outputmySql
- name: Disabling MySql57-Community
yum: disablerepo=mysql57-community
- debug: var=outputDisable
- name: Enabling Mysql56-Community
yum: enablerepo=mysql56-community
- debug: var=outputEnable
- name: Installing mySql 5.6
yum: name="mysql-community-server" state=present
- debug: var=install56
- name: Starting MySql 5.6
service: name=mysqld state=started
- debug: var=serviceStart
- name: Update MySql root password
mysql_user: name=root host=127.0.0.1 password=codiscope
- debug: var=rootmysql
Any ideas?

Your syntax is actually a bit off. Your playbook should look more like this:
- name: Connecting to Anssible_centos
hosts: ansible_centos
remote_user: root
tasks:
- name: Retreiving MySQL RPM and Installing
yum: name=http://dev.sql.com/get/mysql157-community-release-e16-7.noarch.rpm state=present
And if you want to view the results of each task then you want do do something more akin to this:
- name: Retreiving MySQL RPM and Installing
yum: name=http://dev.sql.com/get/mysql157-community-release-e16-7.noarch.rpm state=present
register: outputmySql
- debug: var=outputmySql

Related

Github action yml file and mysql

I'm having issues using mysql as the database for my tests that are running in a Github action. I'm using this as a guide.
I'm getting the following error:
SQLSTATE[HY000] [1045] Access denied for user 'root'#'172.18.0.2' (using password: NO) (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')
here is my yaml file:
name: LaravelTest
on:
push:
branches: [ test ]
jobs:
laravel_tests:
runs-on: ubuntu-latest
container:
image: kirschbaumdevelopment/laravel-test-runner:8.1
services:
testdb:
image: mysql:5.7
env:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: test
MYSQL_ALLOW_EMPTY_PASSWORD: 1
ports:
- 33306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
steps:
- uses: actions/checkout#main
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress
- name: Generate key
run: php artisan key:generate
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Install dependencies
run: npm install
- name: Compile assets
run: npm run dev
- name: Execute tests (Unit and Feature tests) via PHPUnit
run: vendor/bin/phpunit
forge_deploy:
runs-on: ubuntu-latest
needs: laravel_tests
steps:
- name: Make Get Request
uses: satak/webrequest-action#master
with:
url: ${{ secrets.MOMENTUM_TEST_DEPLOY_URL }}
method: GET
UPDATE
I removed this line from my phpunit.xml file:
<env name="DB_HOST" value="testdb"/>
and now I'm getting a different error:
SQLSTATE[HY000] [2002] Connection refused (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')
After doing some more googling, I found this post which made me go back and pull out the docker container and try to do it using the mysql service that's already available on ubuntu.
Once I was able to successfully start the service and create a database, I then realized that the steps in my action were copying the .env file, and if it wasn't there, it was copying the .env.example file, which I overlooked, and was getting odd results. Once I realized that it was looking in that file for database connection values, I was able to override them with the env option in the yaml file. So, I finally got it working, and this is my working yaml file for anyone that may run into this at some point:
name: LaravelTest
on:
push:
branches: [ test ]
jobs:
laravel_tests:
runs-on: ubuntu-20.04
env:
DB_CONNECTION: mysql
DB_HOST: localhost
DB_PORT: 3306
DB_DATABASE: testdb
DB_USERNAME: root
DB_PASSWORD: root
steps:
- name: Set up MySQL
run: |
sudo systemctl start mysql
mysql -e 'CREATE DATABASE testdb;' -uroot -proot
mysql -e 'SHOW DATABASES;' -uroot -proot
- uses: actions/checkout#main
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress
- name: Generate key
run: php artisan key:generate
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Install dependencies
run: npm install
- name: Compile assets
run: npm run dev
- name: Execute tests (Unit and Feature tests) via PHPUnit
run: vendor/bin/phpunit
forge_deploy:
runs-on: ubuntu-20.04
needs: laravel_tests
steps:
- name: Make Get Request
uses: satak/webrequest-action#master
with:
url: ${{ secrets.MOMENTUM_TEST_DEPLOY_URL }}
method: GET

Ansible playbook to install and configure MySQL on Ubuntu

I am trying to set up MySQL reasonably secured,
on Ubuntu 22.04, using Ansible. This is my playbook (from a post by Lorin Hochstein) See Ansible idempotent MySQL installation Playbook This is my playbook (converted for apt and Ubuntu)
- hosts: carme.hcs
become: yes
gather_facts: false
vars:
new_mysql_root_password: <redacted>
mysqlsoftware:
- python3-pymysql
- mysql-client
- mysql-server
tasks:
- name: Install MySQL
action: apt install {{ item }}
with_items: "{{ mysqlsoftware }}"
- name: Start the MySQL service
action: service name=mysql state=started
# 'localhost' needs to be the last item for idempotency, see
# http://ansible.cc/docs/modules.html#mysql-user
- name: update mysql root password for all root accounts
mysql_user:
check_implicit_admin: true
login_user: root
name: root
priv: '*.*:ALL,GRANT'
host: "{{ item }}"
password: "{{ new_mysql_root_password }}"
with_items:
- 127.0.0.1
- ::1
- localhost
- name: copy .my.cnf file with root password credentials
template: src=./shared/my.cnf.j2 dest=/root/.my.cnf owner=root mode=0600
- name: delete anonymous MySQL server user for $server_hostname
action: mysql_user user="" host="{{ server_hostname }}" state="absent"
- name: delete anonymous MySQL server user for localhost
action: mysql_user user="" state="absent"
- name: remove the MySQL test database
action: mysql_db db=test state=absent`
Steps 1 and 2 work just fine.
Step 3 always fails with
TASK [update mysql root password for all root accounts] ********************************************************************************************************* failed: [carme.hcs] (item=127.0.0.1) => {"ansible_loop_var": "item", "changed": false, "item": "127.0.0.1", "msg": "unable to connect to database, check login_user and login_password are correct or /root/.my.cnf has the credentials. Exception message: (1698, \"Access denied for user 'root'#'localhost'\")"} failed: [carme.hcs] (item=::1) => {"ansible_loop_var": "item", "changed": false, "item": "::1", "msg": "unable to connect to database, check login_user and login_password are correct or /root/.my.cnf has the credentials. Exception message: (1698, \"Access denied for user 'root'#'localhost'\")"} failed: [carme.hcs] (item=localhost) => {"ansible_loop_var": "item", "changed": false, "item": "localhost", "msg": "unable to connect to database, check login_user and login_password are correct or /root/.my.cnf has the credentials. Exception message: (1698, \"Access denied for user 'root'#'localhost'\")"}
I have checked on carme.hcs, and /root/.my.cnf does not exist.
I can log in to mysql with "sudo mysql" but not with
"mysql -u root" nor with "mysql -u root -p". Is this not
the default set up of a fresh MySQL install, that I wish to change?
I also ran 'select user, host, plugin, authentication_string from user where user = "root";' on carme, and the result is garbled beyond belief.
In short, plugin = "mysql_native_password" and authentication_string is blank for root#localhost.
I do not understand what is going wrong. Some enlightenment please!
There are so many things wrong with the playbook I'm embarrassed.
I misunderstood about installing pip and what should be installed with apt and what with pip.
apt will hang, and needs a -y param.
It leaves /root/.my.cnf so root can still log in with no password.
When that is fixed, when it is run twice it will fail the second time because it can't log in with no password.
127.0.0.1 and ::1 do not exist in mysql.user table, so they do not need to have their root access removed.
OK, Lets post the updated playbook.
- hosts: carme.hcs
become: yes
gather_facts: false
vars:
new_mysql_root_password: redacted
mysqlsoftware:
- mysql-server
- mysql-client
tasks:
- name: install python, pip etc
shell: apt-get -y install "{{ item }}"
with_items:
- pip
- python3-dev
- default-libmysqlclient-dev
- build-essential
- name: Install MySQL server
shell: apt-get -y install mysql-server
- name: Install MySQL client
shell: apt-get -y install mysql-client
- name: pip install mysqlclient
shell: pip install mysqlclient
- name: Start the MySQL service
action: service name=mysql state=started
- name: copy .my.cnf file with root password credentials
template: src=/home/ian/Ansible/playbooks/shared/my.cnf.j2 dest=/root/.my.cnf owner=root mode=0600
- name: update mysql root password for all root accounts
mysql_user:
name: root
host: localhost
password: "{{ new_mysql_root_password }}"
- name: delete anonymous MySQL server user for localhost
action: mysql_user user="" state="absent"
- name: remove the MySQL test database
action: mysql_db db=test state=absent
- name: Remove /root/.my.cnf
ansible.builtin.file:
path: /root/.my.cnf
state: absent

Ansible access denied for 'root#localhost' error

I am trying to create a mysql database through Ansible playbook. After reading several threads & questions over here, I have created my playbook like this -
---
- name: "Install Packages"
hosts: localhost
connection: local
become: yes
vars:
mysql_root_password: Test#1123
tasks:
- name: Install basic packages
apt:
name:
- vim
- curl
- python3-pip
- mysql-server
- libmysqlclient-dev
- python-pymysql
state: latest
cache_valid_time: 3600
- name: start & enable mysql Server
service:
name=mysql
state=started
enabled=yes
- name: checking RabbitMQ & mysql Server status
command: systemctl status "{{ item }}"
with_items:
- mysql
register: result
ignore_errors: yes
- name: showing mysql Server status
debug:
var: result
- name: Install pip modules
pip:
name:
- pymysql
- virtualenv
state: present
executable: /usr/bin/pip3
- name: create /root/.my.cnf with password credentials
blockinfile:
path: /root/.my.cnf
block: |
[client]
user=root
password={{ mysql_root_password }}
[mysql]
user=root
password={{ mysql_root_password }}
[mysqldump]
user=root
password={{ mysql_root_password }}
[mysqldiff]
user=root
password={{ mysql_root_password }}
create: yes
- name: "Mysql Configuration - Resetting RootPassword"
mysql_user:
login_user: root
login_password: ''
name: root
host_all: yes
password: "{{mysql_root_password}}"
- name: Create a new database with name 'test'
mysql_db:
login_user: root
login_password: "{{mysql_root_password}}"
login_unix_socket: /run/mysqld/mysqld.sock
name: test
state: present
- name: Create database user with name 'test' and password 'Test#1123' with all database privileges
mysql_user:
name: test
password: Test#1123
priv: '*.test:ALL'
state: present
While running this playbook, I am getting below error -
TASK [Mysql Configuration - Resetting RootPassword] *****************************************************************************************************************************************
[WARNING]: Module did not set no_log for update_password
fatal: [localhost]: FAILED! => {"changed": false, "msg": "unable to connect to database, check login_user and login_password are correct or /root/.my.cnf has the credentials. Exception message: (1698, u\"Access denied for user 'root'#'localhost'\")"}
I have checked /root/.my.cnf file has been updated with correct username & password.
I tried to run mysql -u root -p. This has been successful without any password. But while trying to run mysql -u 'root'#'localhost' -p, it is not allowing to get into mysql with or without password.
I know this can be fixed several ways manually. But how to fix this issue with Ansible. May be I am doing some silly mistake. But unable to figure it out. I have also tried without /root/.my.cnf file and without socket file also. But facing same issue.
UPDATE:
I fixed the issue by following this
please take a look at the https://docs.ansible.com/ansible/latest/modules/mysql_user_module.html#examples mysql_user_module documentation.
Here's probably what you want to achieve:
- name: "Mysql Configuration - Resetting RootPassword"
mysql_user:
login_user: root
login_password: actual_root_mysql_password
host: ''
name: root
password: "{{mysql_root_password}}"

docker-compose MySQL container for automated testing with GithubActions

I have GitHub repo and I want to use GithubActions to automatically execute unit Tests with every pull request.
I already set up a workflow file:
name: CI
on:
pull_request:
branches: [ master ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#v1
- name: Install
run: npm ci
- name: Linter
run: npm run lint
- name: Build
run: npm run build
- name: Docker
run: docker-compose up -d
- name: Wait / Sleep
uses: jakejarvis/wait-action#v0.1.0
with:
time: '10s'
- run: |
docker ps
cat ./dumps/backup.sql | docker exec -i mysql-development_1 /usr/bin/mysql -u root --password=password
- name: Test
run: npm test
As I need to insert the tables first, I want to insert a dump which does work on my machine with the exact same command used here.
However, the Action fails with this error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
read unix #->/var/run/docker.sock: read: connection reset by peer
cat: write error: Broken pipe
##[error]Process completed with exit code 1.
How can I access the database within GithubActions?
docker-compose.yml:
version: '3'
services:
mysql-development:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: test_db
ports:
- "3308:3306"
To anyone who might run into the same problem:
https://github.blog/changelog/2020-02-21-github-actions-breaking-change-ubuntu-virtual-environments-will-no-longer-start-the-mysql-service-automatically/
You just need to start the mysql-service manually and perhaps wait for a couple of seconds

run mysql:5.5 docker image from ansible

How to run mysql: 5.5 from ansible? If I run it directly:
docker run -e MYSQL_ROOT_PASSWORD=pass mysql:5.5
it's work ok. But if I run from ansible:
- name: run database
local_action:
module: docker
image: mysql:5.5
state: running
it's start and immediately stop. Also post in case 1 is 3306/tcp, but in case 2 there is no port.
You need to specify mysql root password as environment variable for container. For example:
- hosts: ansible_host
gather_facts: False
sudo: yes
pre_tasks:
- name: install pip pkg.
yum:
name: python-pip
state: present
- name: install boto pkg.
pip:
name: docker-py
state: present
- name: docker
docker:
image: "mysql:5.5"
state: running
env: "MYSQL_ROOT_PASSWORD=my-secret-pw"