Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### Installation of MySQL Server and setting of default root password using Ansible
- Running the first Ansible playbook below against an Ubuntu 14.04 (Trusty64) Vagrant box runs sucessfully and correctly sets the root password.
- With the same playbook against an Ubuntu 16.04 (Xenial64) Vagrant box, the install is successful but does not set the root password,
- hence a slightly different approach described below in the second playbook.
- __Note:__ These are playbook excerpts only and you will still need to configure your Ansible hosts file etc. as per offical docs.
- **Tested with Ubuntu Server 14.04.5 LTS (Trusty64), MySQL 5.5.53, Ansible 2.1.2.0, Vagrant 1.8.6**
- ```yml
- ---
- - hosts: devbox
- become: yes
- vars:
- mysql_root_password: NewRootCleartextPassword
- - name: Install MySQL Server
- apt: name={{item}} update_cache=yes cache_valid_time=86400 state=latest
- with_items:
- - python-mysqldb
- - mysql-server
- - mysql-client
- - name: Start MySQL Server service
- service: name=mysql state=started
- - name: configuring root user pass and priveliges
- mysql_user:
- name: root
- host: "{{ item }}"
- password: "{{ mysql_root_password }}"
- check_implicit_admin: yes
- login_user: root
- login_password: "{{ mysql_root_password }}"
- priv: "*.*:ALL,GRANT"
- with_items:
- - "{{ ansible_hostname }}"
- - 127.0.0.1
- - ::1
- - localhost
- ```
- **Tested with Ubuntu Server 16.04.1 LTS (Xenial64), MySQL 5.7.16, Ansible 2.1.2.0, Vagrant 1.8.6**
- In this playbook the password is required to be in a hashed form, unlike the previous example that shows the password in clear-text.
- To obtain the hashed password for future use in the playbook, you will need to initially install an instance of MySQL Server and run the following commands
- at the ```mysql>``` CLI prompt..
- ```
- USE mysql;
- ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'NewRootCleartextPassword';
- ```
- The password will automatically be hashed before saving to the database. ```Query OK, 0 rows affected (0.00 sec)```
- ```
- SELECT host, user, authentication_string FROM users;
- ```
- Copy and paste the resulting hashed password for your localhost/root shown in to the playbook. Example below..
- ```
- +-----------+------------------+-------------------------------------------+
- | host | user | authentication_string |
- +-----------+------------------+-------------------------------------------+
- | localhost | root | *C70DC9782E5FD6CE6F613D33063A49C7E77CA068 |
- | localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
- | localhost | debian-sys-maint | *96426E2D8A5CB455FB40B7BE0FA2E20D3CCA420F |
- | 127.0.0.1 | root | *C70DC9782E5FD6CE6F613D33063A49C7E77CA068 |
- | ::1 | root | *C70DC9782E5FD6CE6F613D33063A49C7E77CA068 |
- +-----------+------------------+-------------------------------------------+
- ```
- ```yml
- ---
- - hosts: devbox
- become: yes
- vars:
- mysql_root_password: "*C70DC9782E5FD6CE6F613D33063A49C7E77CA068"
- tasks:
- - name: Install MySQL Server
- apt: name={{item}} update_cache=yes cache_valid_time=86400 state=latest
- with_items:
- - python-mysqldb
- - mysql-server
- - mysql-client
- - name: Update mysql root password for all host/root account pairs
- mysql_user: name=root host={{item}} password={{mysql_root_password}} encrypted=yes
- with_items:
- - 127.0.0.1
- - ::1
- - localhost
- - name: Start MySQL Server service
- service: name=mysql state=started
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement