用Ansible在Amazon Linux 2上安装NewRelic代理的方法
首先
我在新创建的linux2实例上使用ansible安装newRelic代理时遇到了一些问题,所以我会写下具体的步骤。
索引
-
- 获取Ansible角色
-
- 安装agent
-
- 卸载agent
-
- 结果
- 参考资料
获取Ansible角色
Ansible是一种方便的配置管理工具,您可以在称为playbook的演练文档中编写处理,并通过ssh连接到服务器来执行该处理。角色(Role)类似于将playbook的处理部分划分为模块,一旦安装,任何人都可以使用它们。
因此,首先我们需要下载包括newRelic安装和启动过程在内的Role。
仓库在这里
只需要一个选择,用中文将以下内容进行同义改写:
按照这篇文章的指示进行操作就可以了
使用Ansible Role来自动化安装New Relic INFRASTRUCTURE
安装代理
由于上述文章中的操作系统指定为适用于LinuxAMI,因此执行时会出现以下错误。”msg”:条件检查 ‘ansible_distribution|lower == ‘amazon” 失败。
TASK [nrinfragent : setup agent repo reference] *******************************************************
fatal: [stg-linux2.h-navi.jp]: FAILED! => {"msg": "The conditional check 'ansible_distribution|lower == 'amazon'' failed. The error was: error while evaluating conditional (ansible_distribution|lower == 'amazon'): 'ansible_distribution' is undefined\n\nThe error appears to be in '/Users/hiroshi.asakawa/Documents/h-navi-dev/lo/infrastructure/ansible/roles/nrinfragent/tasks/install_dist_pkgs.yml': line 44, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: setup agent repo reference\n ^ here\n"}
我会指定一些发行版等内容,以便ansible能够识别linux2。
roles:
- role: nrinfragent
nrinfragent_license_key: "NOTICE: ライセンスキーを入力してください"
nrinfragent_os_name: redhat
nrinfragent_os_version: 7
ansible_distribution: Amazon
ansible_distribution_version: Candidate
ansible_service_mgr: systemd
只要执行这个指令,应该能成功地安装。
代理人的卸载
如果将nrinfragent_state设置为absent,则会执行卸载操作。
roles:
- role: nrinfragent
nrinfragent_license_key: "NOTICE: ライセンスキーを入力してください"
nrinfragent_os_name: redhat
nrinfragent_os_version: 7
ansible_distribution: Amazon
ansible_distribution_version: Candidate
ansible_service_mgr: systemd
nrinfragent_state: absent # 追加
我对Ansible的执行结果出现TASK [nrinfragent: 安装代理]感到不安。
当查看Role中运行的任务时,可以看到ansible的package模块中的state参数只展开了{{ nrinfragent_state }}添加的值(absent),因此可以顺利卸载,没有问题。
角色/nrinfragent/tasks/install_dist_pkgs.yml的安装
- name: install agent
package:
name: "newrelic-infra"
state: "{{ nrinfragent_state }}"
when: nrinfragent_os_name|lower != 'windows'
公式文件ansible package模块
最終结果
已安装最新的agent。
新的遗产 > 基础设施 > 主机

看到版本1.13.2时,我一度误以为是1.1.3版本…好像装了一个非常旧的版本。
文献引用
-
- Ansible RoleでNew Relic INFRASTRUCTUREのインストールを自動化
-
- Configure the infrastructure agent using Ansible
- 公式ドキュメント ansible packageモジュール