Ansible是一款集配置管理、應用部署、任務執行、多節點管理的工具。本人通過Ansible官方文檔及GitHub上的實例學習Ansible,並簡單實現LAMP環境的自動部署(全程Yum安裝)。

操作在VPS上進行,系統選擇CentOS Linux release 7.0.1406 (Core)最小化安裝,IP地址爲23.105.199.121,SSH端口爲27454

此處不具體描述代碼作用及實現,只是本人初步接觸Ansible後的一個小試驗品。本文借鑑了大量實例,會在文末貼出相關GitHub地址。

Tree Structure

代碼使用Ansible中的roles來架構,目錄樹形結構如下

[flying@lemp ansible playbook]$ pwd
/home/flying/Desktop/ansible playbook
[flying@lemp ansible playbook]$ tree
.
├── hosts
├── hosts.bak
├── playbook.yml
└── roles
    ├── httpd24
    │   ├── defaults
    │   ├── files
    │   ├── handlers
    │   │   └── main.yml
    │   ├── tasks
    │   │   ├── configuration.yml
    │   │   ├── firewallSecure.yml
    │   │   ├── installation.yml
    │   │   └── main.yml
    │   └── templates
    ├── init
    │   ├── defaults
    │   ├── files
    │   │   ├── CentOS6-Base.repo
    │   │   ├── CentOS7-Base.repo
    │   │   ├── iptables-config
    │   │   └── vimrc
    │   ├── handlers
    │   │   └── main.yml
    │   ├── tasks
    │   │   ├── configuration.yml
    │   │   ├── firewallSecure.yml
    │   │   ├── installation.yml
    │   │   ├── main.yml
    │   │   └── repo.yml
    │   ├── templates
    │   └── vars
    ├── mariadb
    │   ├── defaults
    │   │   └── main.yml
    │   ├── files
    │   │   ├── MariaDB10.0.repo
    │   │   └── MariaDB10.1.repo
    │   ├── handlers
    │   │   └── main.yml
    │   ├── tasks
    │   │   ├── configuration.yml
    │   │   ├── firewallSecure.yml
    │   │   ├── installation.yml
    │   │   └── main.yml
    │   ├── templates
    │   │   └── my.cnf.j2
    │   └── vars
    ├── mysql
    ├── nginx
    └── php70
        ├── defaults
        │   └── main.yml
        ├── files
        │   ├── remi-php56.repo
        │   ├── remi-php70.repo
        │   └── RPM-GPG-KEY-remi
        ├── handlers
        │   └── main.yml
        ├── tasks
        │   ├── configuration.yml
        │   ├── installation.yml
        │   └── main.yml
        ├── templates
        └── vars

30 directories, 35 files
[flying@lemp ansible playbook]$

Ansible FACTS

[flying@lemp ansible playbook]$ ansible-playbook playbook.yml -i hosts

PLAY [init] *******************************************************************

GATHERING FACTS ***************************************************************
ok: [23.105.199.121]

TASK: [init | Install libselinux-python which used to control selinux] ********
changed: [23.105.199.121]

TASK: [init | Test /etc/yum.repos.d/bak exists or not] ************************
changed: [23.105.199.121]

TASK: [init | Create Directory /etc/yum.repos.d/bak if not exists] ************
changed: [23.105.199.121]

TASK: [init | Backup Repo] ****************************************************
changed: [23.105.199.121]

TASK: [init | Add USTC Yum Repo] **********************************************
changed: [23.105.199.121]

TASK: [init | Install EPEL Repo] **********************************************
changed: [23.105.199.121]

TASK: [init | Install vim, wget, tree .etc Packages] **************************
changed: [23.105.199.121] => (item=vim-enhanced,wget,tree)

TASK: [init | Set TimeZone Asia/Shanghai] *************************************
changed: [23.105.199.121]

TASK: [init | Vim Configuration] **********************************************
changed: [23.105.199.121]

TASK: [httpd24 | Install Apache web server] ***********************************
changed: [23.105.199.121] => (item=httpd)

TASK: [httpd24 | Check backup httpd24 conf or not] ****************************
changed: [23.105.199.121]

TASK: [httpd24 | backup httpd24 conf] *****************************************
changed: [23.105.199.121]

TASK: [httpd24 | Set global ServerName to localhost] **************************
changed: [23.105.199.121]

TASK: [httpd24 | Add PidFile] *************************************************
changed: [23.105.199.121]

TASK: [httpd24 | Add Listen Port 8080] ****************************************
changed: [23.105.199.121]

TASK: [httpd24 | Add index.php into DirectoryIndex] ***************************
changed: [23.105.199.121]

TASK: [httpd24 | Start httpd service] *****************************************
changed: [23.105.199.121]

TASK: [mariadb | Check MariaDB.repo if exists or not] *************************
changed: [23.105.199.121]

TASK: [mariadb | Create MariaDB.repo] *****************************************
changed: [23.105.199.121]

TASK: [mariadb | Insall MariaDB-server MariaDB-client] ************************
changed: [23.105.199.121] => (item=MariaDB-server,MariaDB-client,MySQL-python)

TASK: [mariadb | Start MariaDB Service] ***************************************
changed: [23.105.199.121]

TASK: [mariadb | Disallow root login remotely] ********************************
ok: [23.105.199.121] => (item=DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1'))

TASK: [mariadb | Get list of hosts for the root user.] ************************
ok: [23.105.199.121]

TASK: [mariadb | Update MySQL root password for localhost root account.] ******
changed: [23.105.199.121] => (item=127.0.0.1)
changed: [23.105.199.121] => (item=::1)
changed: [23.105.199.121] => (item=localhost)

TASK: [mariadb | Copy .my.cnf file with root pasword credentials] *************
changed: [23.105.199.121]

TASK: [mariadb | Remove the test database] ************************************
changed: [23.105.199.121]

TASK: [mariadb | Remove anonymous users] **************************************
ok: [23.105.199.121]

TASK: [mariadb | Check MariaDB Configuration File exists or not] **************
changed: [23.105.199.121]

TASK: [mariadb | Backup MariaDB Configuration File] ***************************
skipping: [23.105.199.121]

TASK: [mariadb | Create MariaDB Configuration file] ***************************
changed: [23.105.199.121]

TASK: [mariadb | Create MariaDB log file] *************************************
changed: [23.105.199.121]

TASK: [mariadb | Create MariaDB PID directory] ********************************
changed: [23.105.199.121]

TASK: [mariadb | Start MariaDB Service] ***************************************
ok: [23.105.199.121]

TASK: [mariadb | reload privilege tables] *************************************
changed: [23.105.199.121] => (item=FLUSH PRIVILEGES)

TASK: [mariadb | Create Application Database] *********************************
changed: [23.105.199.121]

TASK: [mariadb | Create Application DB User] **********************************
changed: [23.105.199.121]

TASK: [mariadb | Restart MariaDB Service] *************************************
changed: [23.105.199.121]

TASK: [php70 | Create Remi Repo] **********************************************
changed: [23.105.199.121]

TASK: [php70 | Create the GPG key for Remi] ***********************************
changed: [23.105.199.121]

TASK: [php70 | Install Relevent Packages] *************************************
changed: [23.105.199.121] => (item=bzip2-devel,libxml2-devel,libxml2)

TASK: [php70 | Install php & php-fpm] *****************************************
changed: [23.105.199.121] => (item=php,php-fpm)

TASK: [php70 | Install php modules] *******************************************
changed: [23.105.199.121] => (item=php-cli,php-devel,php-opcache,php-soap,php-pdo,php-pecl-xdebug,php-xml,php-mysql,php-enchant,php-process,php-bcmath,php-ctype,php-libxml,php-xmlreader,php-xmlwriter,php-session,php-mbstring,php-gettext,php-ldap,php-mysqlnd)

TASK: [php70 | Restart php-fpm] ***********************************************
changed: [23.105.199.121]

NOTIFIED: [php70 | Restart Apache httpd Service] ******************************
changed: [23.105.199.121]

PLAY [lamp56] *****************************************************************
skipping: no hosts matched

PLAY [lamp70] *****************************************************************
skipping: no hosts matched

PLAY [lemp56] *****************************************************************
skipping: no hosts matched

PLAY [lemp70] *****************************************************************
skipping: no hosts matched

PLAY RECAP ********************************************************************
23.105.199.121             : ok=44   changed=39   unreachable=0    failed=0   

[flying@lemp ansible playbook]$

Errors Occurrences

在測試過程中,遇到很多報錯,仍有少部分未及時解決。

Failed to start NTP client/server

[root@localhost ~]# systemctl status chronyd.service -l
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Fri 2016-03-04 21:00:22 CST; 3s ago
  Process: 7420 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=1/FAILURE)

Mar 04 21:00:22 localhost.localdomain systemd[1]: Starting NTP client/server...
Mar 04 21:00:22 localhost.localdomain chronyd[7422]: chronyd version 2.1.1 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +DEBUG +ASYNCDNS +IPV6 +SECHASH)
Mar 04 21:00:22 localhost.localdomain chronyd[7422]: Fatal error : adjtimex() failed
Mar 04 21:00:22 localhost.localdomain chronyd[7420]: adjtimex() failed
Mar 04 21:00:22 localhost.localdomain systemd[1]: chronyd.service: control process exited, code=exited status=1
Mar 04 21:00:22 localhost.localdomain systemd[1]: Failed to start NTP client/server.
Mar 04 21:00:22 localhost.localdomain systemd[1]: Unit chronyd.service entered failed state.
Mar 04 21:00:22 localhost.localdomain systemd[1]: chronyd.service failed.
[root@localhost ~]#

MySQL-python

在安裝MariaDB是一定要安裝MySQL-python,否則會報錯

msg: the python mysqldb module is required

ERROR 2002 (HY000)

MariaDB安裝後,沒有立刻啓動服務,Ansible在嘗試連接數據庫時報錯

stderr: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'

Yaml Syntax Error

提示如下報錯

# We could be wrong, but this one looks like it might be an issue with
# missing quotes.  Always quote template expression brackets when they
# start a value. For instance:
#
#     with_items:
#       - {{ foo }}
#
# Should be written as:
#
#     with_items:
#       - "{{ foo }}"

在進行大量排錯後,發現是格式不對,多了一個空格。

- name: Insall MariaDB-server MariaDB-client
  yum: name={{ item }} state=latest enablerepo=mariadb
  with_items:
      - MariaDB-server
      - MariaDB-client
      - MySQL-python

如上,name, yum, with_items必須在同一豎線上。

php-mcrypt & php-gd

缺少依賴包

msg: Error: Package: php-mcrypt-7.0.4-1.el7.remi.x86_64 (remi-php70)
           Requires: libmcrypt.so.4()(64bit)
Error: Package: php-gd-7.0.4-1.el7.remi.x86_64 (remi-php70)
           Requires: libgd.so.3()(64bit)
Error: Package: php-gd-7.0.4-1.el7.remi.x86_64 (remi-php70)
           Requires: gd-last(x86-64) >= 2.1.1

代碼地址 OneDriveGoogle Drive


References in GitHub


Change Log

  • 2016.03.04 21:40 Fri Aisa/Beijing
    • 初稿完成

  • Note Time: 2016.03.04 21:40 Fri
  • Note Location: Asia/Beijing
  • Writer: lempstacker