Ansibleを実際に導入してみた (1)
AnsibleサーバーとWordpressサーバーを用意する
vagrant box add ansible_srv ./centos.box
vagrant box add wdpress_srv ./centos.box
vagrant box add wdpress_srv ./centos.box
ここまでは通常のVM構築と変わりない
仮に以下のようにIPアドレスを割り振っておく
Ansibleサーバ:192.168.33.100
WordPressサーバ:192.168.33.200
AnsibleサーバーにAnsibleをインストールする
(EPELリポジトリを先に導入)
1 2 | yum localinstall -y http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm yum install -y ansible |
※Pythonが万が一インストールされていなくても自動インストールされる
◯確認◯
1 2 3 | ansible —version ansible 1.9.2 configured module search path = None |
Ansibleサーバのssh鍵をWordPressサーバに登録する
1 | scp ./id_rsa.pub root@192.168.33.200:~/.ssh/authorized_keys |
※予めroot権限でログイン出来るように”sudo passwd”でパスワードを作成しておく。
定義ファイル一覧 こちら参照
<<site.yml>> (構成定義ファイル)
1 2 3 4 5 6 7 8 9 10 11 12 13 | --- - name: Install WordPress, MySQL, Nginx, and PHP-FPM hosts: wordpress-server remote_user: root # remote_user: user # sudo: yes roles: - common - mysql - nginx - php-fpm - wordpress |
<<hosts>> (サーバー定義ファイル)
1 2 | [wordpress-server] 192.168.33.200 |
※ここにWordPress用に作った仮想サーバのIPを指定する (複数可能)
<<roles/mysql/tasks/main.yml>> (一例としてMySQLインストール定義を抜粋)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | --- - name: Install Mysql package yum: name={{ item }} state=present with_items: - mysql-server - MySQL-python - libselinux-python - libsemanage-python - name: Configure SELinux to start mysql on any port seboolean: name=mysql_connect_any state=true persistent=yes when: ansible_selinux.status == "enabled" - name: Create Mysql configuration file template: src=my.cnf.j2 dest=/etc/my.cnf notify: - restart mysql - name: Start Mysql Service service: name=mysqld state=started enabled=yes |
<<roles/mysql/templates/main.yml>> (MySQLのコピー元定義ファイル)
1 2 3 4 5 6 7 8 9 10 11 | [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 port={{ mysql_port }} [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid |
<<./group_vars>>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | --- # Which version of WordPress to deploy wp_version: 4.2.4 wp_sha256sum: 42ca594afc709cbef8528a6096f5a1efe96dcf3164e7ce321e87d57ae015cc82 # These are the WordPress database settings wp_db_name: wordpress wp_db_user: wordpress wp_db_password: secret # You shouldn't need to change this. mysql_port: 3306 # This is used for the nginx server configuration, but access to the # WordPress site is not restricted by a named host. server_hostname: www.example.com # Disable All Updates # By default automatic updates are enabled, set this value to true to disable all automatic updates auto_up_disable: false #Define Core Update Level #true = Development, minor, and major updates are all enabled #false = Development, minor, and major updates are all disabled #minor = Minor updates are enabled, development, and major updates are disabled core_update_level: true |
実際にplaybookを実行してみよう!
1 | ansible-playbook site.yml -i hosts --private-key=~/.ssh/id_rsa |
※ssh鍵を指定する
エラーが・・・
1 2 | fatal: [192.168.33.200] => SSH Error: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). while connecting to 192.168.33.200:22 |
怒られました・・・Vagrant独自のセキュリティロック仕様なので以下のように対処
Vagrantfileに config.ssh.insert_key = falseを指定
もう一度実行・・・!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | PLAY [Install WordPress, MySQL, Nginx, and PHP-FPM] *************************** GATHERING FACTS *************************************************************** Enter passphrase for key '/root/.ssh/id_rsa': ok: [192.168.33.200] TASK: [common | Install libselinux-python] ************************************ ok: [192.168.33.200] TASK: [common | Copy the EPEL repository definition] ************************** ok: [192.168.33.200] TASK: [common | Create the GPG key for EPEL] ********************************** ok: [192.168.33.200] TASK: [common | Set up iptables rules] **************************************** ok: [192.168.33.200] TASK: [mysql | Install Mysql package] ***************************************** ok: [192.168.33.200] => (item=mysql-server,MySQL-python,libselinux-python,libsemanage-python) TASK: [mysql | Configure SELinux to start mysql on any port] ****************** changed: [192.168.33.200] TASK: [mysql | Create Mysql configuration file] ******************************* changed: [192.168.33.200] TASK: [mysql | Start Mysql Service] ******************************************* changed: [192.168.33.200] TASK: [nginx | Install nginx] ************************************************* changed: [192.168.33.200] TASK: [nginx | Copy nginx configuration for wordpress] ************************ changed: [192.168.33.200] TASK: [php-fpm | Install php-fpm and deps] ************************************ changed: [192.168.33.200] => (item=php,php-fpm,php-enchant,php-IDNA_Convert,php-mbstring,php-mysql,php-PHPMailer,php-process,php-simplepie,php-xml) TASK: [php-fpm | Disable default pool] **************************************** changed: [192.168.33.200] TASK: [php-fpm | Copy php-fpm configuration] ********************************** changed: [192.168.33.200] TASK: [wordpress | Download WordPress] **************************************** changed: [192.168.33.200] TASK: [wordpress | Extract archive] ******************************************* changed: [192.168.33.200] TASK: [wordpress | Add group "wordpress"] ************************************* changed: [192.168.33.200] TASK: [wordpress | Add user "wordpress"] ************************************** changed: [192.168.33.200] TASK: [wordpress | Fetch random salts for WordPress config] ******************* changed: [192.168.33.200 -> 127.0.0.1] TASK: [wordpress | Create WordPress database] ********************************* changed: [192.168.33.200] TASK: [wordpress | Create WordPress database user] **************************** changed: [192.168.33.200] TASK: [wordpress | Copy WordPress config file] ******************************** changed: [192.168.33.200] TASK: [wordpress | Change ownership of WordPress installation] **************** changed: [192.168.33.200] NOTIFIED: [mysql | restart mysql] ********************************************* changed: [192.168.33.200] NOTIFIED: [nginx | restart nginx] ********************************************* changed: [192.168.33.200] NOTIFIED: [php-fpm | restart php-fpm] ***************************************** changed: [192.168.33.200] PLAY RECAP ******************************************************************** 192.168.33.200 : ok=26 changed=20 unreachable=0 failed=0 |
無事成功しました。SELinuxを無効にしてOK.
ただし実ファイルが見つからず
Wordpressインストール画面の表示には至らず。。。
画面表示と並行して複数サーバへのデプロイを検証してみようと思います。
Author Profile
スターフィールド編集部
SHARE