使用 Anshible 在 Amazon EC2 上使用 yum 安装指定的包

首先

在Amazon EC2实例(Amazon Linux)上安装Ansible,并使用Ansible在Amazon EC2实例上通过yum安装指定的软件包的步骤。

环境:

我們使用了以下的AMI在本次操作中創建了Amazon EC2實例(Amazon Linux)。

亚马逊 Linux AMI 2016.03.3 (HVM), SSD卷类型 – ami-374db956
amzn-ami-hvm-2016.03.3.x86_64-gp2 (ami-374db956)

在Amazon EC2实例上安装的Ansible版本如下所示。

ansible-playbook 2.1.1.0的版本

请参考以下资料

非常感谢您提供的参考资料。

yum – 使用yum软件包管理器来管理软件包
https://docs.ansible.com/ansible/yum_module.html

检查模式(“干跑”)
https://docs.ansible.com/ansible/playbooks_checkmode.html

关于Ansible构建管理工具

http://apatheia.info/blog/2013/04/06/about-ansible/

(Note: The provided information is the paraphrased translation of the original text.)

使用 yum 在 AmazonLinux 上安装 Ansible 但无法运行。

在Amazon EC2上安装Ansible的步骤

(1) 登录AWS管理控制台,创建一个Amazon EC2实例(Amazon Linux)以安装Ansible。

在已创建的Amazon EC2实例上,使用ec2-user进行ssh登录。

ssh ec2-user@EC2インスタンスのIPアドレス

更新 Amazon EC2 实例上的 yum 软件包。

$ sudo su - 
# yum -y update

(4) 重新启动Amazon EC2实例。

在这个例子中,我们将改变Amazon EC2实例的主机名如下。

# vi /etc/sysconfig/network
 (中略)
HOSTNAME=example-ansible-server
 (中略)

重新启动Amazon EC2实例。

# reboot

当Amazon EC2实例启动后,您将再次使用ec2-user进行SSH登录。

(5) 确认 Amazon EC2 实例中的 Python 版本。

运行Anshible的机器必须安装Python。请确认Python已安装在Amazon EC2实例上。
(自2016年9月25日起,EC2实例默认已安装以下版本的Python。)

[ec2-user@example-ansible-server ~]$ python --version
Python 2.7.12
[ec2-user@example-ansible-server ~]$
[ec2-user@example-ansible-server ~]$ uname -a
Linux example-ansible-server 4.4.19-29.55.amzn1.x86_64 #1 SMP Mon Aug 29 23:29:40 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
[ec2-user@example-ansible-server ~]$

在Amazon EC2实例上安装pip。

[ec2-user@example-ansible-server ~]$ sudo easy_install pip
Searching for pip
Best match: pip 6.1.1
Adding pip 6.1.1 to easy-install.pth file
Installing pip script to /usr/local/bin
Installing pip2.7 script to /usr/local/bin
Installing pip2 script to /usr/local/bin

Using /usr/lib/python2.7/dist-packages
Processing dependencies for pip
Finished processing dependencies for pip
[ec2-user@example-ansible-server ~]$

(7)在Amazon EC2实例上安装Ansible。

安装Ansible。

[ec2-user@example-ansible-server ~]$ sudo pip install ansible
You are using pip version 6.1.1, however version 8.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting ansible
  Downloading ansible-2.1.1.0.tar.gz (1.9MB)
    100% |????????????????????????????????| 1.9MB 249kB/s
Requirement already satisfied (use --upgrade to upgrade): paramiko in /usr/lib/python2.7/dist-packages (from ansible)
Requirement already satisfied (use --upgrade to upgrade): jinja2 in /usr/lib/python2.7/dist-packages (from ansible)
Requirement already satisfied (use --upgrade to upgrade): PyYAML in /usr/lib64/python2.7/dist-packages (from ansible)
Requirement already satisfied (use --upgrade to upgrade): setuptools in /usr/lib/python2.7/dist-packages (from ansible)
Requirement already satisfied (use --upgrade to upgrade): pycrypto>=2.6 in /usr/lib64/python2.7/dist-packages (from ansible)
Requirement already satisfied (use --upgrade to upgrade): ecdsa>=0.11 in /usr/lib/python2.7/dist-packages (from paramiko->ansible)
Requirement already satisfied (use --upgrade to upgrade): markupsafe in /usr/lib64/python2.7/dist-packages (from jinja2->ansible)
Installing collected packages: ansible
  Running setup.py install for ansible
Successfully installed ansible-2.1.1.0
[ec2-user@example-ansible-server ~]$

确认安装了 Aansible。

[ec2-user@example-ansible-server ~]$ which ansible
/usr/local/bin/ansible
[ec2-user@example-ansible-server ~]$
[ec2-user@example-ansible-server ~]$ ansible --version
ansible 2.1.1.0
  config file =
  configured module search path = Default w/o overrides
[ec2-user@example-ansible-server ~]$

Ansible配置的设置

(8) 创建 Ansible 操作目标机器的 IP 地址列表。

在安装了Ansible的Amazon EC2实例上,创建Ansible操作目标机器的IP地址列表。

[ec2-user@example-ansible-server ~]$ sudo mkdir /etc/ansible
[ec2-user@example-ansible-server ~]$
[ec2-user@example-ansible-server ~]$ sudo vi /etc/ansible/hosts
[local_node]
127.0.0.1

(9) 创建一个Ansible的playbook。

使用Aansible在Amazon EC2实例上安装指定的 yum 包。

在这个例子中,我们将使用yum命令在EC2实例上安装jq、sysstat(sar命令)、git和gcc。

创建Ansible的Playbook文件。在Playbook文件中写入所要安装的yum包。

[ec2-user@example-ansible-server ~]$ vi /home/ec2-user/example_playbook.yml
---
- hosts:
    - localhost

  connection: local

  tasks:
    - name: update all packages
      yum: name=* state=latest
      become: yes

    - name: install the latest version of gcc
      yum: name=gcc state=latest
      become: yes

    - name: install the latest version of git
      yum: name=git state=latest
      become: yes

    - name: install the latest version of jq
      yum: name=jq state=latest
      become: yes

    - name: install the latest version of sysstat
      yum: name=sysstat state=latest
      become: yes
[ec2-user@example-ansible-server ~]$ cat /home/ec2-user/example_playbook.yml
---
- hosts:
    - localhost

  connection: local

  tasks:
    - name: update all packages
      yum: name=* state=latest
      become: yes

    - name: install the latest version of gcc
      yum: name=gcc state=latest
      become: yes

    - name: install the latest version of git
      yum: name=git state=latest
      become: yes

    - name: install the latest version of jq
      yum: name=jq state=latest
      become: yes

    - name: install the latest version of sysstat
      yum: name=sysstat state=latest
      become: yes
[ec2-user@example-ansible-server ~]$

(10) 检查 Ansible playbook 的语法。

检查所创建的playbook文件的语法,并确认没有问题。

[ec2-user@example-ansible-server ~]$ ansible-playbook --syntax-check /home/ec2-user/example_playbook.yml

playbook: /home/ec2-user/example_playbook.yml
[ec2-user@example-ansible-server ~]$

(11) 以测试模式运行Aansible的playbook。

将使用Ansible playbook以测试模式运行。在测试模式下,实际上不会安装yum软件包。

确认所指定的安装包名称会显示出来。

[ec2-user@example-ansible-server ~]$ ansible-playbook --check /home/ec2-user/example_playbook.yml

PLAY [localhost] ***************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [update all packages] *****************************************************
ok: [localhost]

TASK [install the latest version of gcc] ***************************************
changed: [localhost]

TASK [install the latest version of git] ***************************************
changed: [localhost]

TASK [install the latest version of jq] ****************************************
changed: [localhost]

TASK [install the latest version of sysstat] ***********************************
changed: [localhost]

PLAY RECAP *********************************************************************
localhost                  : ok=6    changed=4    unreachable=0    failed=0

[ec2-user@example-ansible-server ~]$

使用Ansible在Amazon EC2实例上通过yum安装指定的软件包。

(12)执行 Ansible 的 playbook,在 Amazon EC2 实例上使用 yum 安装软件包。

运行Ansible的playbook,在EC2实例上使用yum安装jq、sysstat、git和gcc。

首先,我们会检查安装前的状态。

[ec2-user@example-ansible-server ~]$ rpm -qa | grep jq
[ec2-user@example-ansible-server ~]$
[ec2-user@example-ansible-server ~]$ rpm -qa | grep sysstat
[ec2-user@example-ansible-server ~]$
[ec2-user@example-ansible-server ~]$ rpm -qa | grep git
[ec2-user@example-ansible-server ~]$
[ec2-user@example-ansible-server ~]$ rpm -qa | grep gcc
libgcc48-4.8.3-9.109.amzn1.x86_64
[ec2-user@example-ansible-server ~]$

执行 Ansible playbook,在 Amazon EC2 实例上使用 yum 安装软件包。

[ec2-user@example-ansible-server ~]$ ansible-playbook /home/ec2-user/example_playbook.yml

PLAY [localhost] ***************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [update all packages] *****************************************************
ok: [localhost]

TASK [install the latest version of gcc] ***************************************
changed: [localhost]

TASK [install the latest version of git] ***************************************
changed: [localhost]

TASK [install the latest version of jq] ****************************************
changed: [localhost]

TASK [install the latest version of sysstat] ***********************************
changed: [localhost]

PLAY RECAP *********************************************************************
localhost                  : ok=6    changed=4    unreachable=0    failed=0

[ec2-user@example-ansible-server ~]$

(13) 确认 Amazon EC2 实例上已安装 yum 包。

通过上述Ansible的playbook执行,确认在Amazon EC2实例上安装了yum包。

再次执行Ansible的playbook,并确认在Amazon EC2实例上已安装yum软件包。

您可以再次执行Ansible的playbook,并确认Amazon EC2实例安装了yum软件包。

由于Ansible具有幂等性,因此即使再次运行Playbook,如果yum包已经安装,安装过程也将被跳过。

在这个例子中,我们确认命令执行结果会显示“ok=6”。

[ec2-user@example-ansible-server ~]$ ansible-playbook /home/ec2-user/example_playbook.yml

PLAY [localhost] ***************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [update all packages] *****************************************************
ok: [localhost]

TASK [install the latest version of gcc] ***************************************
ok: [localhost]

TASK [install the latest version of git] ***************************************
ok: [localhost]

TASK [install the latest version of jq] ****************************************
ok: [localhost]

TASK [install the latest version of sysstat] ***********************************
ok: [localhost]

PLAY RECAP *********************************************************************
localhost                  : ok=6    changed=0    unreachable=0    failed=0

[ec2-user@example-ansible-server ~]$

使用手动方式执行命令,确认在Amazon EC2实例上已安装了yum包。(13-2)

不使用Ansible的playbook,手动确认Amazon EC2实例上是否已安装了yum包也是可能的。

可以执行以下命令来确认每个包已经通过yum安装了。

[ec2-user@example-ansible-server ~]$ rpm -qa | grep jq
jq-libs-1.5-1.2.amzn1.x86_64
jq-1.5-1.2.amzn1.x86_64
[ec2-user@example-ansible-server ~]$

[ec2-user@example-ansible-server ~]$ rpm -qa | grep git
git-2.7.4-1.47.amzn1.x86_64
[ec2-user@example-ansible-server ~]$

[ec2-user@example-ansible-server ~]$ rpm -qa | grep sysstat
sysstat-9.0.4-27.10.amzn1.x86_64
[ec2-user@example-ansible-server ~]$

[ec2-user@example-ansible-server ~]$ rpm -qa | grep gcc
libgcc48-4.8.3-9.109.amzn1.x86_64
gcc48-4.8.3-9.109.amzn1.x86_64
gcc-4.8.3-3.20.amzn1.noarch
[ec2-user@example-ansible-server ~]$
[ec2-user@example-ansible-server ~]$ which jq
/usr/bin/jq
[ec2-user@example-ansible-server ~]$

[ec2-user@example-ansible-server ~]$ which git
/usr/bin/git
[ec2-user@example-ansible-server ~]$

[ec2-user@example-ansible-server ~]$ which sar
/usr/bin/sar
[ec2-user@example-ansible-server ~]$

[ec2-user@example-ansible-server ~]$ which gcc
/usr/bin/gcc
[ec2-user@example-ansible-server ~]$
[ec2-user@example-ansible-server ~]$ jq --version
jq-1.5
[ec2-user@example-ansible-server ~]$

[ec2-user@example-ansible-server ~]$ git --version
git version 2.7.4
[ec2-user@example-ansible-server ~]$

[ec2-user@example-ansible-server ~]$ sar -v
Linux 4.4.19-29.55.amzn1.x86_64 (example-ansible-server)        09/25/2016      _x86_64_        (1 CPU)

[ec2-user@example-ansible-server ~]$ gcc --version
gcc (GCC) 4.8.3 20140911 (Red Hat 4.8.3-9)
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[ec2-user@example-ansible-server ~]$

以上就是。

广告
将在 10 秒后关闭
bannerAds