如何在 Ubuntu 14.04 上使用 Ansible 部署多个 PHP 应用程序

介绍

本教程是关于在 Ubuntu 上使用 Ansible 部署 PHP 应用程序的系列中的第三部 14.04. 第一个教程涵盖了部署应用程序的基本步骤; 第二个教程涵盖了数据库,队列大师和任务计划员(crons)等更先进的主题。

在本教程中,我们将建立在我们在以前的教程中学到的内容上,将我们的单一应用程序Ansible演示本转化为支持在一个或多个服务器上部署多个PHP应用程序的演示本。

我们将使用一些简单的 Lumen应用程序作为我们的示例的一部分。然而,这些说明可以很容易地修改以支持其他框架和应用程序,如果你已经有自己的。

前提条件

要遵循本教程,您将需要:

  • 遵循本系列的[第一 (https://andsky.com/tech/tutorials/how-to-deploy-a-basic-php-application-using-ansible-on-ubuntu-14-04)和[第二 (https://andsky.com/tech/tutorials/how-to-deploy-an-advanced-php-application-using-ansible-on-ubuntu-14-04)教程设置了两个滴滴.
  • 一个新的(第三)Ubuntu 14.04 Droplet在 [first reductional] (https://andsky.com/tech/tutorials/how-to-deploy-a-basic-php-application-using-ansible-on-ubuntu-14-04 ) 中像原始的 PHP Droplet 一样设置,带有sudo 非root 用户和 SSH 密钥. 此 Droplet 将用来演示如何使用一个 Ansible 播放本向多个服务器部署多个应用程序 。 我们将把原始的PHP Droplet和这个新的PHP Droplet的IP地址分别称为你的_first_server_ip'和你的_second_server_ip'.
  • 联合国 在本地计算机上添加了以下行的 " /etc/hosts " 文件。 您可以在 [此教程的第 6 步( https://andsky.com/tech/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-14-04-lts# step-six-%E2%80%94-set-up-local-hosts-file-(optional ) 中更多地了解此文件 。 .
1[label Lines to add anywhere in /etc/hosts]
2your_first_server_ip laravel.example.com one.example.com two.example.com
3your_second_server_ip laravel.example2.com two.example2.com

我们将在本教程中使用的示例网站是)而不是。

第1步:设置Playbook变量

在此步骤中,我们将设置播放簿变量来定义我们的新应用程序。

在之前的教程中,我们硬编码了所有配置规格,这对于执行特定应用程序的特定任务的许多播放书来说是正常的。

正如我们之前所看到的,Ansible提供了可在任务定义和文件模板中使用的变量。我们还没有看到的是如何手动设置变量。在播放簿的顶部,除了主机任务参数,您可以定义一个vars参数,并在那里设置变量。

如果您尚未这样做,请从以前的教程中将目录更改为ansible-php

1cd ~/ansible-php/

打开我们的现有游戏图书来编辑。

1nano php.yml

文件的顶部应该是这样的:

1[label Top of original php.yml]
2---
3- hosts: php
4  sudo: yes
5
6  tasks:
7. . .

要定义变量,我们可以简单地在hosts,sudotasks旁边添加一个vars部分。

 1[label Updated vars in php.yml]
 2---
 3- hosts: php
 4  sudo: yes
 5
 6  vars:
 7    wwwuser: www-data
 8
 9  tasks:
10. . .

接下来,通过并用新的变量更新www-data用户的所有发生,这个格式应该熟悉,因为我们已经在外观和搜索中使用了它。

要使用 nano 查找和更换,请按 CTRL+\. 您将看到一个提示,表示 搜索(替换): . 键入** www-data** ,然后按 ENTER。 提示将更改为** 替换:** . 在这里,键入** {{ wwwuser }}** ,然后再次按 ENTER

注意 :请确保我们刚刚在顶部添加的变量声明也未被更改,应该有11个需要更换的www-data实例。

在我们走得更远之前,在变量方面,我们需要注意一些事情,通常我们可以在更长的行内添加这些变量:

1[label Example task in php.yml]
2- name: create /var/www/ directory
3  file: dest=/var/www/ state=directory owner={{ wwwuser }} group={{ wwwuser }} mode=0700

但是,如果变量是字符串中唯一的值,我们需要将其包装在引文中,以便YAML分析器能够正确理解它:

1[label Updated task in php.yml]
2- name: Run artisan migrate
3  shell: php /var/www/laravel/artisan migrate --force
4  sudo: yes
5  sudo_user: "{{ wwwuser }}"
6  when: dbpwd.changed

在你的播放簿中,这需要发生任何时候你有 sudo_user: {{ wwwuser }}. 你可以使用一个全球查找和更换的方式,取代 sudo_user: {{ wwwuser }} 用** sudo_user: "{{ wwwuser }}"** . 应该有四个行需要这个更改。

一旦您已更改所有发生,保存并运行播放簿:

1ansible-playbook php.yml --ask-sudo-pass

不会有更改的任务,这意味着我们的wwwuser变量正在正常工作。

步骤 2 — 定义复杂配置的嵌套变量

在本节中,我们将研究复杂配置选项的嵌入变量。

在之前的步骤中,我们设置了一个基本的变量,但是,还可以设置变量和定义变量列表,这提供了我们需要的功能来定义我们希望在我们的服务器上设置的网站列表。

首先,让我们看看我们在游戏中设置的现有 git 存储库:

1[label Existing git task in php.yml]
2- name: Clone git repository
3  git: >
4    dest=/var/www/laravel
5    repo=https://github.com/do-community/do-ansible-adv-php.git
6    update=yes
7    version=example

我们可以提取以下有用的信息:名称(目录),存储库,分支机构和域. 因为我们正在设置多个应用程序,我们还需要一个域名来响应它。

这导致了以下四个变量,我们可以为此应用定义:

1[label Application variables]
2name: laravel
3repository: https://github.com/do-community/do-ansible-adv-php.git
4branch: example
5domain: laravel.example.com

现在,打开你的剧本来编辑:

1nano php.yml

在顶部的vars部分,我们可以将我们的应用程序添加到新的应用程序列表中:

 1[label Updated applications variables in php.yml]
 2---
 3- hosts: php
 4  sudo: yes
 5
 6  vars:
 7    wwwuser: www-data
 8
 9    applications:
10      - name: laravel
11        domain: laravel.example.com
12        repository: https://github.com/do-community/do-ansible-adv-php.git
13        branch: example
14
15...

如果你现在运行你的播放簿(使用),没有什么会改变,因为我们还没有设置我们的任务,以使用我们的新的应用程序变量。

步骤 3 — 在任务中循环变量

在本节中,我们将学习如何在任务中循环通过变量列表。

如前所述,变量列表需要在我们想要使用它们的每个任务中环绕,正如我们在安装包任务中所看到的那样,我们需要定义一个项目循环,然后将任务应用到列表中的每个项目。

打开你的剧本来编辑:

1nano php.yml

我们将首先从一些简单的任务开始,在你玩本的中间,你应该找到这两个env任务:

1[label Existing env tasks in php.yml]
2- name: set APP_DEBUG=false
3  lineinfile: dest=/var/www/laravel/.env regexp='^APP_DEBUG=' line=APP_DEBUG=false
4
5- name: set APP_ENV=production
6  lineinfile: dest=/var/www/laravel/.env regexp='^APP_ENV=' line=APP_ENV=production

您会注意到它们目前是用laravel目录硬编码的。我们希望更新它以使用每个应用程序的名称属性。 为了做到这一点,我们在with_items选项中添加with_items选项,在我们的应用程序列表上循环。

它应该看起来像这样:

1[label Updated .env tasks in php.yml]
2- name: set APP_DEBUG=false
3  lineinfile: dest=/var/www/{{ item.name }}/.env regexp='^APP_DEBUG=' line=APP_DEBUG=false
4  with_items: applications
5
6- name: set APP_ENV=production
7  lineinfile: dest=/var/www/{{ item.name }}/.env regexp='^APP_ENV=' line=APP_ENV=production
8  with_items: applications

接下来,转到两个Laravel手工Cron任务,它们可以像我们刚刚在env任务中一样更新,我们还会将item.name添加到name参数中,因为Ansible使用这个字段来独特地识别每个Cron条目。

任务应该是这样的:

 1[label Updated cron tasks in php.yml]
 2- name: Laravel Scheduler
 3  cron: >
 4    job="run-one php /var/www/{{ item.name }}/artisan schedule:run 1>> /dev/null 2>&1"
 5    state=present
 6    user={{ wwwuser }}
 7    name="{{ item.name }} php artisan schedule:run"
 8  with_items: applications
 9
10- name: Laravel Queue Worker
11  cron: >
12    job="run-one php /var/www/{{ item.name }}/artisan queue:work --daemon --sleep=30 --delay=60 --tries=3 1>> /dev/null 2>&1"
13    state=present
14    user={{ wwwuser }}
15    name="{{ item.name }} Laravel Queue Worker"
16  with_items: applications

如果你现在保存并运行播放簿(使用),你只应该看到两个更新的cron任务作为更新。这是由于名称参数的变化。除此之外,没有任何变化,这意味着我们的应用程序列表正如预期的那样工作,我们还没有对我们的服务器进行任何更改,因为我们重塑了我们的播放簿。

步骤4 — 在模板中应用圆形变量

在本节中,我们将介绍如何在模板中使用圆形变量。

模板中的可循环变量非常容易使用,它们可以用完全相同的方式用于任务,就像所有其他变量一样。当你考虑文件路径以及变量时,复杂性会出现,因为在某些用途中,我们需要在文件名中计数,甚至由于新文件而运行其他命令。

在 Nginx 的情况下,我们需要为每个应用程序创建一个新的配置文件,并告诉 Nginx 应该启用它。

首先,打开你的剧本编辑:

1nano php.yml

查找),并像我们在其他任务中一样更新它:

1[label Updated nginx task in php.yml]
2- name: Configure nginx
3  template: src=nginx.conf dest=/etc/nginx/sites-available/{{ item.name }}
4  with_items: applications
5  notify:
6    - restart php5-fpm
7    - restart nginx

当我们在这里时,我们还会添加上面提到的两个任务,首先,我们会告诉 Nginx 我们的新网站配置文件,这是通过在 /var/nginx/ 中的 sites-availablesites-enabled 目录之间进行的 symlink。

将此任务添加到配置 nginx任务后:

1[label New symlink task in php.yml]
2- name: Configure nginx symlink
3  file: src=/etc/nginx/sites-available/{{ item.name }} dest=/etc/nginx/sites-enabled/{{ item.name }} state=link
4  with_items: applications
5  notify:
6    - restart php5-fpm
7    - restart nginx

接下来,我们要删除启用了默认的网站配置文件,以免导致我们新网站配置文件的问题。

1[label New file task php.yml]
2- name: Remove default nginx site
3  file: path=/etc/nginx/sites-enabled/default state=absent
4  notify:
5    - restart php5-fpm
6    - restart nginx

请注意,我们不需要循环应用程序,因为我们正在寻找单个文件。

您的播放簿中的 Nginx 块现在应该是这样的:

 1[label Updated nginx tasks in php.yml]
 2- name: Configure nginx
 3  template: src=nginx.conf dest=/etc/nginx/sites-available/{{ item.name }}
 4  with_items: applications
 5  notify:
 6    - restart php5-fpm
 7    - restart nginx
 8
 9- name: Configure nginx symlink
10  file: src=/etc/nginx/sites-available/{{ item.name }} dest=/etc/nginx/sites-enabled/{{ item.name }} state=link
11  with_items: applications
12  notify:
13    - restart php5-fpm
14    - restart nginx
15
16- name: Remove default nginx site
17  file: path=/etc/nginx/sites-enabled/default state=absent
18  notify:
19    - restart php5-fpm
20    - restart nginx

保存播放簿并打开nginx.conf文件进行编辑:

1nano nginx.conf

更新配置文件以使用我们的变量:

 1[label Updated nginx.conf]
 2server {
 3    listen 80 default_server;
 4    listen [::]:80 default_server ipv6only=on;
 5
 6    root /var/www/{{ item.name }}/public;
 7    index index.php index.html index.htm;
 8
 9    server_name {{ item.domain }};
10
11    location / {
12        try_files $uri $uri/ =404;
13    }
14
15    error_page 404 /404.html;
16    error_page 500 502 503 504 /50x.html;
17    location = /50x.html {
18        root /var/www/{{ item.name }}/public;
19    }
20
21    location ~ \.php$ {
22        try_files $uri =404;
23        fastcgi_split_path_info ^(.+\.php)(/.+)$;
24        fastcgi_pass unix:/var/run/php5-fpm.sock;
25        fastcgi_index index.php;
26        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
27        include fastcgi_params;
28    }
29}

但是,我们还没有完成,请注意在顶部的default_server吗?我们只想包含laravel应用程序的default_server,以使其成为默认。

它将看起来像这样:

1[label Updated nginx.conf with conditionals]
2server {
3    listen 80{% if item.name == "laravel" %} default_server{% endif %};
4    listen [::]:80{% if item.name == "laravel" %} default_server ipv6only=on{% endif %};

相应地更新您的「nginx.conf」并保存它。

现在是时候发行我们的剧本了:

1ansible-playbook php.yml --ask-sudo-pass

您应该注意到 Nginx 任务已被标记为更改. 当它完成运行时,请在您的浏览器中刷新网站,它应该显示与上一个教程结束时相同:

1[label http://laravel.example.com/]
2Queue: YES
3Cron: YES

步骤5 - 将多个变量循环在一起

在此步骤中,我们将将多个变量合并为任务。

现在是时候解决一个更复杂的循环示例,具体注册的变量。为了支持不同的状态,并防止任务不必要地运行,您将记住,我们在我们的 Clone git repository 任务中使用注册:克隆来注册变量克隆与任务的状态。

首先,打开你的剧本编辑:

1nano php.yml

下面就来看看 Clone git repository 任务:

 1[label Existing git task in php.yml]
 2- name: Clone git repository
 3  git: >
 4    dest=/var/www/laravel
 5    repo=https://github.com/do-community/do-ansible-adv-php.git
 6    update=yes
 7    version=example
 8  sudo: yes
 9  sudo_user: "{{ wwwuser }}"
10  register: cloned

当我们在这个任务中注册变量时,我们不需要做我们尚未做的任何事情:

 1[label Updated git task in php.yml]
 2- name: Clone git repository
 3  git: >
 4    dest=/var/www/{{ item.name }}
 5    repo={{ item.repository }}
 6    update=yes
 7    version={{ item.branch }}
 8  sudo: yes
 9  sudo_user: "{{ wwwuser }}"
10  with_items: applications
11  register: cloned

现在,向下移动播放簿,直到您找到创作者创建项目任务:

1[label Original composer task in php.yml]
2- name: composer create-project
3  composer: command=create-project working_dir=/var/www/laravel optimize_autoloader=no
4  sudo: yes
5  sudo_user: "{{ wwwuser }}"
6  when: cloned|changed

现在我们需要更新它以循环通过 两个变量 应用程序克隆 这是通过使用with_together选项,并通过应用程序克隆 随着with_together循环通过两个变量,访问项目是用item.# 完成的,其中 是定义变量的索引。

1with_together:
2- list_one
3- list_two

item.0 将指list_one,而 item.1 将指list_two

这意味着对于应用程序我们可以通过item.0.name访问属性,对于克隆我们需要通过cloned.results传输任务的结果,然后我们可以通过item.1.changed检查它是否被更改。

这意味着任务变成:

1[label Updated composer task in php.yml]
2- name: composer create-project
3  composer: command=create-project working_dir=/var/www/{{ item.0.name }} optimize_autoloader=no
4  sudo: yes
5  sudo_user: "{{ wwwuser }}"
6  when: item.1.changed
7  with_together:
8    - applications
9    - cloned.results

现在保存并运行您的播放簿:

1ansible-playbook php.yml --ask-sudo-pass

但是,我们现在有一个注册变量在循环中工作得很好。

步骤6 - 复杂的注册变量和循环

在本节中,我们将了解更复杂的注册变量和循环。

转换的最复杂的一部分是处理我们正在使用的注册变量来为我们的MySQL数据库创建密码. 也就是说,在这个步骤中我们没有处理的更多事情,我们只需要一次更新一些任务。

打开你的剧本来编辑:

1nano php.yml

找到 MySQL 任务,在我们的初始通道中,我们只会像我们在以前的任务中一样添加基本变量:

 1[label Updated MySQL tasks in php.yml]
 2- name: Create MySQL DB
 3  mysql_db: name={{ item.name }} state=present
 4  with_items: applications
 5
 6- name: Generate DB password
 7  shell: makepasswd --chars=32
 8  args:
 9    creates: /var/www/{{ item.name }}/.dbpw
10  with_items: applications
11  register: dbpwd
12
13- name: Create MySQL User
14  mysql_user: name={{ item.name }} password={{ dbpwd.stdout }} priv={{ item.name }}.*:ALL state=present
15  when: dbpwd.changed
16
17- name: set DB_DATABASE
18  lineinfile: dest=/var/www/{{ item.name }}/.env regexp='^DB_DATABASE=' line=DB_DATABASE={{ item.name }}
19  with_items: applications
20
21- name: set DB_USERNAME
22  lineinfile: dest=/var/www/{{ item.name }}/.env regexp='^DB_USERNAME=' line=DB_USERNAME={{ item.name }}
23  with_items: applications
24
25- name: set DB_PASSWORD
26  lineinfile: dest=/var/www/{{ item.name }}/.env regexp='^DB_PASSWORD=' line=DB_PASSWORD={{ dbpwd.stdout }}
27  when: dbpwd.changed
28
29- name: Save dbpw file
30  lineinfile: dest=/var/www/{{ item.name }}/.dbpw line="{{ dbpwd.stdout }}" create=yes state=present
31  sudo: yes
32  sudo_user: "{{ wwwuser }}"
33  when: dbpwd.changed
34
35- name: Run artisan migrate
36  shell: php /var/www/{{ item.name }}/artisan migrate --force
37  sudo: yes
38  sudo_user: "{{ wwwuser }}"
39  when: dbpwd.changed

接下来我们将添加with_together,以便我们可以使用我们的数据库密码. 对于我们的密码生成,我们需要循环dbpwd.results,并将能够从item.1.stdout访问密码,因为应用程序将通过item.0访问。

我们可以相应地更新我们的游戏簿:

 1[label Updated MySQL tasks in php.yml]
 2- name: Create MySQL DB
 3  mysql_db: name={{ item.name }} state=present
 4  with_items: applications
 5
 6- name: Generate DB password
 7  shell: makepasswd --chars=32
 8  args:
 9    creates: /var/www/{{ item.name }}/.dbpw
10  with_items: applications
11  register: dbpwd
12
13- name: Create MySQL User
14  mysql_user: name={{ item.0.name }} password={{ item.1.stdout }} priv={{ item.0.name }}.*:ALL state=present
15  when: item.1.changed
16  with_together:
17  - applications
18  - dbpwd.results
19
20- name: set DB_DATABASE
21  lineinfile: dest=/var/www/{{ item.name }}/.env regexp='^DB_DATABASE=' line=DB_DATABASE={{ item.name }}
22  with_items: applications
23
24- name: set DB_USERNAME
25  lineinfile: dest=/var/www/{{ item.name }}/.env regexp='^DB_USERNAME=' line=DB_USERNAME={{ item.name }}
26  with_items: applications
27
28- name: set DB_PASSWORD
29  lineinfile: dest=/var/www/{{ item.0.name }}/.env regexp='^DB_PASSWORD=' line=DB_PASSWORD={{ item.1.stdout }}
30  when: item.1.changed
31  with_together:
32  - applications
33  - dbpwd.results
34
35- name: Save dbpw file
36  lineinfile: dest=/var/www/{{ item.0.name }}/.dbpw line="{{ item.1.stdout }}" create=yes state=present
37  sudo: yes
38  sudo_user: "{{ wwwuser }}"
39  when: item.1.changed
40  with_together:
41  - applications
42  - dbpwd.results
43
44- name: Run artisan migrate
45  shell: php /var/www/{{ item.0.name }}/artisan migrate --force
46  sudo: yes
47  sudo_user: "{{ wwwuser }}"
48  when: item.1.changed
49  with_together:
50  - applications
51  - dbpwd.results

一旦你更新了播放簿,保存它并运行它:

1ansible-playbook php.yml --ask-sudo-pass

尽管我们对我们的播放簿进行了所有更改,但不会对数据库任务发生任何更改. 随着此步骤的更改,我们应该完成从单个应用程序播放簿到多个应用程序播放簿的转换。

第7步:添加更多应用程序

在此步骤中,我们将在我们的播放簿中配置另外两个应用程序。

现在我们已经重新设计了我们的演示本以使用变量来定义应用程序,将新应用程序添加到我们的服务器的过程非常简单,只需将它们添加到应用程序变量列表中。

打开你的剧本来编辑:

1nano php.yml

vars部分的顶部,找到applications块:

1[label Existing applications variable in php.yml]
2applications:
3  - name: laravel
4    domain: laravel.example.com
5    repository: https://github.com/do-community/do-ansible-adv-php.git
6    branch: example

现在再添加两个应用程序:

 1[label Updated applications variable in php.yml]
 2applications:
 3  - name: laravel
 4    domain: laravel.example.com
 5    repository: https://github.com/do-community/do-ansible-adv-php.git
 6    branch: example
 7
 8  - name: one
 9    domain: one.example.com
10    repository: https://github.com/do-community/do-ansible-php-example-one.git
11
12    branch: master
13
14  - name: two
15    domain: two.example.com
16    repository: https://github.com/do-community/do-ansible-php-example-two.git
17    branch: master

保存你的玩具书。

现在是时候开始玩你的游戏了:

1ansible-playbook php.yml --ask-sudo-pass

这个步骤可能需要一段时间,因为作曲家设置了新应用程序. 完成后,你会注意到一些任务被更改,如果你仔细观察,你会注意到每个链接的项目将被列出。

更重要的是,如果您在您的 Web 浏览器中访问您配置的网站的所有三个域,您应该注意到三个不同的网站。

第一个应该看起来很熟悉,另外两个应该显示:

1[label http://one.example.com/]
2This is example app one!

1[label http://two.example.com/]
2This is example app two!

因此,我们刚刚通过简单地更新我们的应用程序列表部署了两个新的Web应用程序。

第8步:使用主机变量

在此步骤中,我们将提取我们的变量来托管变量。

退一步,播放簿变量很好,但如果我们要在使用相同的播放簿使用不同的服务器上部署不同的应用程序呢?我们可以对每个任务进行条件检查,以确定哪个服务器正在运行该任务,或者我们可以使用 _host 变量。

主机变量可以定义在主机文件中,就像我们在ansible_ssh_user变量中所做的那样,也可以定义在host_vars目录中的每个主机的专用文件中。

首先,在我们的主机文件和我们的播放簿旁边创建一个新目录。

1mkdir host_vars

接下来我们需要为我们的主机创建一个文件。 Ansible 使用的惯例是让文件名匹配主机文件中的主机名称。

1[label Ansible hosts file]
2[php]
3your_first_server_ip ansible_ssh_user=sammy

然后你应该创建一个名为 host_vars/your_first_server_ip 的文件,让我们现在创建它:

1nano host_vars/your_first_server_ip

与我们的播放书一样,主机文件使用YAML进行格式化,这意味着我们可以将我们的应用程序列表复制到我们的新主机文件中,所以它看起来像这样:

 1[label New host_vars/your_first_server_ip file]
 2---
 3applications:
 4  - name: laravel
 5    domain: laravel.example.com
 6    repository: https://github.com/do-community/do-ansible-adv-php.git
 7    branch: example
 8
 9  - name: one
10    domain: one.example.com
11    repository: https://github.com/do-community/do-ansible-php-example-one.git
12    branch: master
13
14  - name: two
15    domain: two.example.com
16    repository: https://github.com/do-community/do-ansible-php-example-two.git
17    branch: master

保存您的新主机文件,并打开播放簿进行编辑:

1nano php.yml

更新顶部以删除整个应用程序部分:

 1[label Updated top of php.yml]
 2---
 3- hosts: php
 4  sudo: yes
 5
 6  vars:
 7    wwwuser: www-data
 8
 9  tasks:
10. . .

保存 playbook,然后运行它:

1ansible-playbook php.yml --ask-sudo-pass

即使我们已经将我们的变量从播放簿移动到主机文件中,输出应该完全相同,而且不应该有由Ansible报告的变化。

host_vars文件中定义的变量也可在管理服务器的所有播放簿中访问,这对于常见选项和设置是有用的。

第9步:在其他服务器上部署应用程序

在此步骤中,我们将利用我们的新主机文件并在第二台服务器上部署应用程序。

首先,我们需要与我们的新主机更新我们的主机文件。

1nano hosts

并添加到您的新主机:

1[label Ansible hosts file]
2[php]
3your_first_server_ip ansible_ssh_user=sammy
4your_second_server_ip ansible_ssh_user=sammy

保存并关闭文件。

接下来,我们需要创建一个新的主机文件,就像我们第一次一样。

1nano host_vars/your_second_server_ip

您可以选择一个或多个我们的示例应用程序并将其添加到您的主机文件中,例如,如果您想将我们的原始示例和示例2部署到新服务器上,您可以使用:

 1[label New host_vars/your_second_server_ip file]
 2---
 3applications:
 4  - name: laravel
 5    domain: laravel.example2.com
 6    repository: https://github.com/do-community/do-ansible-adv-php.git
 7    branch: example
 8
 9  - name: two
10    domain: two.example2.com
11    repository: https://github.com/do-community/do-ansible-php-example-two.git
12    branch: master

保存你的玩具书。

最后,我们可以运行我们的剧本:

1ansible-playbook php.yml --ask-sudo-pass

Ansible 将需要一段时间才能运行,因为它已经在你的第二个服务器上设置了所有内容。当它完成时,请在你的浏览器中打开所选应用程序(例如,我们使用了 laravel.example2.com’‘two.example2.com’),并确认它们已正确设置。

结论

本教程采用了一本功能齐全的单个应用程序演示本,并将其转换为支持多个服务器上的多个应用程序. 结合以前的教程中涵盖的主题,您应该拥有所需的一切,以撰写一个完整的演示本来部署您的应用程序。

你会注意到添加更多的应用程序和另一个服务器是多么简单,一旦我们已经完成了播放簿的结构,这就是Ansible的力量,这就是使其如此灵活和易于使用的。

Published At
Categories with 技术
Tagged with
comments powered by Disqus