LoginSignup
9
12

More than 3 years have passed since last update.

CentOS8でAnsibleを動かしてみた。

Last updated at Posted at 2019-10-04

今更ながらAnsibleを触ってみた

最近、サーバをたくさん立てることが多くなってきたため、サーバを立てたら必ず実施することはAnsibleに任せることにしました。
なお、Ansibleサーバ・対象クライアントともに先日リリースされたばかりのCentOS 8で動かしています。

[arkey22@automation]$ cat /etc/redhat-release
CentOS Linux release 8.0.1905 (Core)

サーバを立てたら自動でやりたかったこと

  • dnf update
  • vimやwgetなど必要そうなツールのインストール
  • zabbix-agentのダウンロード・インストール・起動・自動起動の設定
  • zabbix_agentd.confのIPアドレス書き換え
  • zabbix用にfirewalldの穴あけ(TCP/10050)
  • bashrcに追記してプロンプトに色付け
  • sshのrootログイン禁止

Ansibleのインストールと実行(実施した内容をhistoryコマンドから抜粋)

# dnf install ansible
一致した引数がありません: ansible

# dnf install python3-pip
# pip3 install ansible --user
$ ansible --version
ansible 2.8.5

$ sudo mkdir /etc/ansible
$ sudo chown arkey22:arkey22 /etc/ansible/
$ mkdir /etc/ansible/inventory
$ vim /etc/ansible/inventory/hosts
$ vim /etc/ansible/tasks.yml
$ cd /etc/ansible
$ ansible-playbook -i inventory tasks.yml

※CentOS8をインストールしてdnf updateした状態から実施しています。
※いきあたりばったりでインストールしたので正規の手段ではない可能性があります。
※参考: https://www.tecmint.com/install-ansible-on-centos-rhel-8

yaml

tasks.yml
[arkey22@automation]$ cat tasks.yml
- hosts: automation_test
  remote_user: arkey22
  become: yes
  tasks:
    - name: dnf update
      yum: name=* state=latest

    - name: dnf install list of packages
      yum:
        name: "{{packages}}"
      vars:
        packages:
          - epel-release
          - vim
          - tmux
          - wget
          - git
          - python3
          - python3-pip

 # Zabbix Installation
    - name: Download zabbix-agent
      get_url:
       url: https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.4-1.el7.x86_64.rpm
       dest: /tmp/zabbix-agent-4.0.4-1.el7.x86_64.rpm

    - name: Install zabbix-agent
      yum:
        name: /tmp/zabbix-agent-4.0.4-1.el7.x86_64.rpm
        state: present

    - name: Delete zabbix-agent file
      file:
        path: /tmp/zabbix-agent-4.0.4-1.el7.x86_64.rpm
        state: absent

    - name: Edit zabbix.conf (Server)
      replace:
        path: /etc/zabbix/zabbix_agentd.conf
        regexp: 'Server=127.0.0.1'
        replace: 'Server=192.168.0.208'

    - name: Edit zabbix.conf (ServerActive)
      replace:
        path: /etc/zabbix/zabbix_agentd.conf
        regexp: 'ServerActive=127.0.0.1'
        replace: 'ServerActive=192.168.0.208'

    - name: Start and enable zabbix-agent.service
      systemd:
        name: zabbix-agent.service
        state: started
        enabled: yes

    - name: firewall for zabbix-agent
      command: firewall-cmd --add-port=10050/tcp --permanent

    - name: restart firewalld
      command: firewall-cmd --reload

# /root/.bashrc
    - lineinfile:
        path: /root/.bashrc
        line: 'PS1="[\[\e[1;31m\\]\u\[\e[0m\]@\[\e[1;34m\]\h\[\e[0m\]]\\$ "'

# /home/arkey22/.bashrc
    - lineinfile:
        path: /home/arkey22/.bashrc
        line: 'PS1="[\[\e[1;32m\\]\u\[\e[0m\]@\[\e[1;34m\]\h\[\e[0m\]]\\$ "'

# /etc/bashrc
    - lineinfile:
        path: /etc/bashrc
        line: 'HISTTIMEFORMAT="%y/%m/%d %H:%M:%S "'
    - lineinfile:
        path: /etc/bashrc
        line: 'HISTSIZE=100000'

# /etc/ssh/sshd_config no root login
    - name: no root login
      replace:
        path: /etc/ssh/sshd_config
        regexp: 'PermitRootLogin yes'
        replace: 'PermitRootLogin no'
    - name: Restart sshd.service
      systemd:
        name: sshd.service
        state: restarted

フォルダ構成

[arkey22@automation]$ pwd
/etc/ansible
[arkey22@automation]$
[arkey22@automation]$ ls
ansible.cfg  inventory  private.yml  tasks.yml
[arkey22@automation]$
[arkey22@automation]$ ls -l inventory
合計 4
-rw-rw-r--. 1 arkey22 arkey22 32 10月  5 11:11 hosts
[arkey22@automation]$
[arkey22@automation]$ cat inventory/hosts
[automation_test]
192.168.0.212
[arkey22@automation]$

反省点・振り返り

  • 初めてAnsibleを触ったので、まだまだスマートに書けてない気がします。より良い記法が見つかり次第、改善していきます。
  • Ansibleをインストールする際、CentOS8の場合、dnf install ansibleでインストール出来なかったので、pipで入れました。 なので、mkdir /etc/ansibleして、手作業でフォルダ構成を作りました。(レポジトリの問題かしら?)
9
12
0

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
9
12