我在CentOS8上尝试运行Ansible

我尝试了一下Ansible,虽然现在有点晚。

最近,由于频繁地设置许多服务器,我们决定将服务器的设置任务交由Ansible来完成。
需要注意的是,我们在最近刚发布的CentOS 8上运行了Ansible服务器和目标客户端。

[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ログイン禁止

从history命令中提取了安装Ansible并运行的内容。

# 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

请注意,以下是一种可能的中文翻译:

※此操作是在安装了CentOS 8并进行了dnf update的情况下进行的。
※由于是采用了试错方式进行安装,所以可能不是正式的安装方法。
※参考链接:https://www.tecmint.com/install-ansible-on-centos-rhel-8

YAML:又称为YAML Ain’t Markup Language,是一种轻量级的数据序列化格式。

[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して、手作業でフォルダ構成を作りました。(レポジトリの問題かしら?)
广告
将在 10 秒后关闭
bannerAds