Advertisement
Guest User

Untitled

a guest
Nov 20th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. ### Installation of MySQL Server and setting of default root password using Ansible
  2.  
  3. Running the first Ansible playbook below against an Ubuntu 14.04 (Trusty64) Vagrant box runs sucessfully and correctly sets the root password.
  4. With the same playbook against an Ubuntu 16.04 (Xenial64) Vagrant box, the install is successful but does not set the root password,
  5. hence a slightly different approach described below in the second playbook.
  6.  
  7. __Note:__ These are playbook excerpts only and you will still need to configure your Ansible hosts file etc. as per offical docs.
  8.  
  9.  
  10. **Tested with Ubuntu Server 14.04.5 LTS (Trusty64), MySQL 5.5.53, Ansible 2.1.2.0, Vagrant 1.8.6**
  11.  
  12. ```yml
  13. ---
  14. - hosts: devbox
  15. become: yes
  16.  
  17. vars:
  18. mysql_root_password: NewRootCleartextPassword
  19.  
  20. - name: Install MySQL Server
  21. apt: name={{item}} update_cache=yes cache_valid_time=86400 state=latest
  22. with_items:
  23. - python-mysqldb
  24. - mysql-server
  25. - mysql-client
  26.  
  27. - name: Start MySQL Server service
  28. service: name=mysql state=started
  29.  
  30. - name: configuring root user pass and priveliges
  31. mysql_user:
  32. name: root
  33. host: "{{ item }}"
  34. password: "{{ mysql_root_password }}"
  35. check_implicit_admin: yes
  36. login_user: root
  37. login_password: "{{ mysql_root_password }}"
  38. priv: "*.*:ALL,GRANT"
  39. with_items:
  40. - "{{ ansible_hostname }}"
  41. - 127.0.0.1
  42. - ::1
  43. - localhost
  44.  
  45. ```
  46.  
  47. **Tested with Ubuntu Server 16.04.1 LTS (Xenial64), MySQL 5.7.16, Ansible 2.1.2.0, Vagrant 1.8.6**
  48.  
  49. In this playbook the password is required to be in a hashed form, unlike the previous example that shows the password in clear-text.
  50. 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
  51. at the ```mysql>``` CLI prompt..
  52.  
  53. ```
  54. USE mysql;
  55. ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'NewRootCleartextPassword';
  56. ```
  57.  
  58. The password will automatically be hashed before saving to the database. ```Query OK, 0 rows affected (0.00 sec)```
  59.  
  60. ```
  61. SELECT host, user, authentication_string FROM users;
  62. ```
  63.  
  64. Copy and paste the resulting hashed password for your localhost/root shown in to the playbook. Example below..
  65.  
  66. ```
  67. +-----------+------------------+-------------------------------------------+
  68. | host | user | authentication_string |
  69. +-----------+------------------+-------------------------------------------+
  70. | localhost | root | *C70DC9782E5FD6CE6F613D33063A49C7E77CA068 |
  71. | localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
  72. | localhost | debian-sys-maint | *96426E2D8A5CB455FB40B7BE0FA2E20D3CCA420F |
  73. | 127.0.0.1 | root | *C70DC9782E5FD6CE6F613D33063A49C7E77CA068 |
  74. | ::1 | root | *C70DC9782E5FD6CE6F613D33063A49C7E77CA068 |
  75. +-----------+------------------+-------------------------------------------+
  76. ```
  77.  
  78. ```yml
  79. ---
  80. - hosts: devbox
  81. become: yes
  82.  
  83. vars:
  84. mysql_root_password: "*C70DC9782E5FD6CE6F613D33063A49C7E77CA068"
  85.  
  86. tasks:
  87. - name: Install MySQL Server
  88. apt: name={{item}} update_cache=yes cache_valid_time=86400 state=latest
  89. with_items:
  90. - python-mysqldb
  91. - mysql-server
  92. - mysql-client
  93.  
  94. - name: Update mysql root password for all host/root account pairs
  95. mysql_user: name=root host={{item}} password={{mysql_root_password}} encrypted=yes
  96. with_items:
  97. - 127.0.0.1
  98. - ::1
  99. - localhost
  100.  
  101. - name: Start MySQL Server service
  102. service: name=mysql state=started
  103.  
  104. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement