LoginSignup
0

More than 3 years have passed since last update.

AmazonLinux2にAnsibleでNewRelicのエージェントを入れる方法

Last updated at Posted at 2020-12-08

はじめに

新しく立てたlinux2のインスタンスにansibleでnewRelicのagentを入れようとしたら少しハマったのでやり方を書いておきます。

目次

  1. Ansible Roleの取得
  2. agentのインストール
  3. agentのアンインストール
  4. 結果
  5. 参考文献

Ansible Roleの取得

ansibleとはplaybookと呼ばれる上演ドキュメントに処理を書くとサーバにsshしてその処理を実行してくれる便利な構成管理ツールです。
Roleはそのplaybookの処理の一部を切り出したモジュールみたいなもので、インスコすれば誰でも使えます。

そこで、まずはnewRelicのインスコや起動処理などをまとめたRoleをダウンロードします。

リポジトリはこちら

導入はこの記事の通りやればおk
Ansible RoleでNew Relic INFRASTRUCTUREのインストールを自動化

agentのインストール

上記の記事はOSの指定がlinuxAMI向けになっているため、実行すると下記のエラーが出ます。
"msg": "The conditional check 'ansible_distribution|lower == 'amazon'' failed.

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

これで実行するとうまくインストールされるはずです。

agentのアンインストール

nrinfragent_stateabsentを指定するとアンインストールが実行されます。

  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 : install agent]と出てくるので不安になります。

Roleの中で実行されているタスクの中身をみてみると、ansibleのpackageモジュールのなかのstateに対して{{ nrinfragent_state }}で追加した値(absent)が展開されているだけなので、これで問題なくアンインストールができています。

roles/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がインストールされました。

NewRelic > Infrastructure > hosts

スクリーンショット 2020-12-08 21.11.51.png

余談
(version1.13.2をみて、1.1.3?..えらい古いのが入ったな。と一度勘違いしました)

参考文献

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0