使用CloudFormation在EC2上配置Apache并使用Ansible进行设置和执行!

首先

为了在上一次使用Cloudformation构建的环境中使用Serverspec进行测试,使用Ansible来构建环境。
文章在此处↓
尝试按层级创建AWS CloudFormation模板并创建交叉堆栈引用。

朝向的目标

    • EC2(1a)にテスト実行サーバーとしてAnsibleを実装

 

    EC2(1c)にテスト対象サーバーとしてApacheを構築

结构图

スクリーンショット 2022-06-02 8.35.37.png

安装Ansible

从连接到Ansible服务器开始!首先在服务器上安装Ansible。

$ sudo amazon-linux-extras install ansible2 -y

如果在版本确认命令中显示以下内容,则表示安装已完成。

$ ansible --version
ansible 2.9.23
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/ec2-user/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.18 (default, Jun 10 2021, 00:11:02) [GCC 7.3.1 20180712 (Red Hat 7.3.1-13)]

复制密钥。

这个工作需要从服务器退出后,在本地终端(本次使用的是命令行)上执行。
由于Ansible需要通过SSH连接到目标服务器并执行操作,所以需要提供密钥。可以使用以下命令将在创建EC2实例时指定的密钥(或现有密钥)配置到Ansible服务器上。
注意,如果不使用scp命令进行复制,则Ansible服务器上默认没有私钥在~/.ssh目录下。
$ scp -i [ansibleサーバに接続するための秘密鍵] [ansibleサーバが構築対象に接続するための秘密鍵]  ec2-user@[ansibleサーバーのグローバルIPアドレス]:~/.ssh
# e.g.)
$ scp -i ~/.ssh/samplekey.pem ~/.ssh/samplekey.pem ec2-user@1.2.3.4:~/.ssh

创建一个名为”hosts”的文件来存储库存信息。

在 /var/www/ansible 目录下创建一个清单文件。

$ vi hosts

只需最少的配置,在下面的文件中添加新内容。

# hosts
[server]
11.22.333.44

[server:vars]
ansible_ssh_port=22
ansible_ssh_user=ec2-user
ansible_ssh_private_key_file=~/.ssh/秘密鍵.pem
__working_user=ec2-user

创建PLAYBOOK

在创建Playbook(site.yaml)中,描述了针对由主机清单(hosts)定义的Ansible服务器设备执行的操作内容。

# 対象の指定(インベントリファイルで指定した名前)
- hosts: server

  # 管理者権限で実行
  become: true

  # 以下に実際の操作を記述していく
  tasks:

    # パッケージの更新
    - name: updated yum
      yum:
        name: "*"
        state: latest
    
    # 言語設定の変更
    - name: set LANG=ja_JP.UTF-8 in /etc/locale.conf
      lineinfile:
        dest: /etc/locale.conf
        regexp: "^LANG="
        line: "LANG=ja_JP.UTF-8"
        backup: yes

    # httpdのインストール
    - name: installed httpd
      yum:
        name: httpd
        state: installed
      become: yes

    # httpdの起動・自動起動設定
    - name: httpd booted and auto boot conf
      service:
        name: httpd
        state: started
        enabled: yes
      become: yes

    # あらかじめ作っておいたindex.htmlファイルを対象に配置
    - name: deployed index.html
      template:
        src: ./index.html
        dest: /var/www/html/index.html
        mode: 0644

hosts
実行対象のグループやホスト名を指定。

tasks
セクションでは実行するタスクを実行順に記述。

name
実行するタスク名を指定する箇所、何を指定してもOK。

state
どのような状態にするかの指示。

enabled
運用稼働状態にするかの指示。

语法检查

$ ansible-playbook -i hosts site.yml --syntax-check

playbook: site.yaml
# 問題なければこのように表示される

执行ansible-playbook

$ ansible-playbook -i hosts site.yaml

错误

[WARNING]: Platform linux on host 11.22.333.44 is using the discovered Python interpreter at /usr/bin/python, but future
installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.

只需要在Ansible的配置文件(ansible.cfg)中添加以下设置即可解决。

[defaults]
interpreter_python=/usr/bin/python3

消除Ansible在执行时显示的警告,通过指定Python的版本来参考这篇文章。


错误

fatal: [example.com]: FAILED! => {"changed": false, "msg": "The Python 2 bindings for rpm are needed for this module. 
If you require Python 3 support use the `dnf` Ansible module instead.. 
The Python 2 yum module is needed for this module. 
If you require Python 3 support use the `dnf` Ansible module instead."}

[解决方案]
在hosts文件中添加Python的绝对路径,如下所示。

# ansible_python_interpreter=/usr/bin/python3
ansible_python_interpreter=/usr/bin/python

在没有安装dnf的环境中使用Ansible的yum模块。


顺利完成了!!

$ ansible-playbook -i hosts site.yaml

# 無事に全ての構築が完了!

PLAY [server] ********************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************
ok: [11.22.333.44]

TASK [updated yum] ***************************************************************************************************************
ok: [11.22.333.44]

TASK [set LANG=ja_JP.UTF-8 in /etc/locale.conf] **********************************************************************************
ok: [11.22.333.44]

TASK [installed httpd] ***********************************************************************************************************
ok: [11.22.333.44]

TASK [httpd booted and auto boot conf] *******************************************************************************************
ok: [11.22.333.44]

TASK [deployed index.html] *******************************************************************************************************
ok: [11.22.333.44]

PLAY RECAP ***********************************************************************************************************************
11.22.333.44           : ok=6    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

我参考了一篇文章。

    • EC2にAnsibleをインストールしてWebサーバを構築する手順を解説

 

    Ansibleで環境構築を自動化する!
bannerAds