在EC2系统管理器上执行Ansible Playbook

由于东京地区现在也可以使用 AWS-RunAnsiblePlaybook,我们可以参考以下的方法,在EC2系统管理器中尝试执行Ansible Playbook。

我将执行以下操作:
– 为特定实例安装Ansible。
– 使用Playbook将安装Apache并启动。

这个内容适合初学者。

前提条件 tí

:beginner:

将Ansible安装到指定的实例。

在EC2的系统管理服务中,选择“执行命令”,然后选择“AWS-RunShellScript”。

2017-06-27-13-36-59.png

选择目标实例。尝试选择多个实例。
在”Commands”中填写以下内容,并选择”RUN”。

请将以下内容以中文给出,只需要一个选项:
对于Amazon Linux,请使用yum命令安装Ansible。
对于CentOS或Ubuntu,请使用yum或apt命令安装Ansible。

/usr/bin/pip install ansible
ansible --version
2017-06-27-13-50-31.png
2017-06-27-13-52-24.png
2017-06-27-13-57-29.png
2017-06-27-14-02-44.png

此外,有关pip版本的错误出现在输出结果中,但是已成功安装了Ansible。

----------ERROR-------
You are using pip version 6.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
2017-06-27-14-11-11.png

如果你希望同时更新pip,只需在以下设置下执行命令即可。

pip install --upgrade pip
pip install ansible
ansible --version
2017-06-27-14-16-19.png

执行 Ansible Playbook

在EC2的系统管理服务中,选择”执行命令”,然后选择”AWS-RunAnsiblePlaybook”。

2017-06-27-16-51-00.png

如果playbook可以在通过HTTP获取的位置找到,可以将URL输入到”playbookurl”中。

- hosts: all
  become: true

  tasks:
    - name: gather ec2 facts
      action: ec2_facts

    - name: install apache on redhat or centos instances
      yum: name=httpd state=present
      when: ansible_os_family == "RedHat"

    - name: install apache on debian or ubuntu instances
      apt: name=apache2 state=present
      when: ansible_os_family == "Debian"

    - name: enable apache on startup and start service for redhat or centos
      service: name=httpd enabled=yes state=started
      when: ansible_os_family == "RedHat"

    - name: enable apache on startup and start service for debian or ubuntu
      service: name=apache2 enabled=yes state=started
      when: ansible_os_family == "Debian"
2017-06-28-14-20-01.png

看到输出结果时,确认如下结果显示,可以知道Ansible已经成功执行。

2017-06-28-14-38-43.png

请确认执行结果。

我能够通过SSH登录到实例并确认日志和网页被正确显示。

Jun 28 05:20:20 ip-10-0-13-43 ansible-setup: Invoked with filter=* gather_subset=['all'] fact_path=/etc/ansible/facts.d gather_timeout=10
Jun 28 05:20:21 ip-10-0-13-43 ansible-ec2_facts: Invoked with url_password=NOT_LOGGING_PARAMETER force=False use_proxy=True url=None force_basic_auth=False http_agent=ansible-httpget url_username=None validate_certs=True
Jun 28 05:20:22 ip-10-0-13-43 ansible-yum: Invoked with name=['httpd'] list=None install_repoquery=True conf_file=None disable_gpg_check=False state=present disablerepo=None update_cache=False enablerepo=None exclude=None validate_certs=True installroot=/ skip_broken=False
Jun 28 05:20:25 ip-10-0-13-43 yum[23264]: Installed: apr-1.5.1-1.12.amzn1.x86_64
Jun 28 05:20:25 ip-10-0-13-43 yum[23264]: Installed: apr-util-1.4.1-4.17.amzn1.x86_64
Jun 28 05:20:25 ip-10-0-13-43 yum[23264]: Installed: httpd-tools-2.2.32-1.9.amzn1.x86_64
Jun 28 05:20:25 ip-10-0-13-43 yum[23264]: Installed: apr-util-ldap-1.4.1-4.17.amzn1.x86_64
Jun 28 05:20:25 ip-10-0-13-43 yum[23264]: Installed: httpd-2.2.32-1.9.amzn1.x86_64
Jun 28 05:20:27 ip-10-0-13-43 ansible-service: Invoked with name=httpd pattern=None enabled=True state=started sleep=None arguments= runlevel=default

由于在这次的情况下我已经创建了一个在私有子网中的实例,所以我希望通过使用lynx进行确认。

2017-06-28-15-56-06.png

暂时,我确认了Ansible可以运行,所以下次我可能尝试设置awslogs之类的东西。