ansible:集中管理平台

无服务、无agent、采用ssh管理远程主机、多线程
一、配置文件/etc/ansible/ansible.cfg
二、管理方式
(1)ad-hoc 临时命令
(2)playbook剧本php

远程管理
1、新建一个目录
[root@room8pc16 nsd1709]# mkdir ansi
[root@room8pc16 nsd1709]# cd ansi
二、建立配置文件
[root@room8pc16 ansi]# vim ansible.cfg
[defaults]
inventory = inventory # 定义被管理主机到哪一个文件中查找
remote_user = root # ssh到远程主机的用户
三、建立主机清单
[root@room8pc16 ansi]# vim inventory
[dbservers] # 定义主机组名
192.168.4.1 # 定义组成员主机mysql

[webservers]
192.168.4.2
192.168.4.3
四、列出主机命令,虽然all没有定义,可是它是保留字,表示全部主机
[root@room8pc16 ansi]# ansible all --list-hosts
[root@room8pc16 ansi]# ansible dbservers --list-hosts
[root@room8pc16 ansi]# ansible webservers --list-hosts
五、测试到远程主机的通讯
[root@room8pc16 ansi]# ansible all -m ping -k
六、在全部的主机上执行任意命令
[root@room8pc16 ansi]# ansible all -a 'touch /opt/abc.txt' -klinux

如下命令是远程开机命令,与ansible无关
[root@room8pc16 ansi]# ether-wake -i enp2s0 xx:xx:xx:xx:xx:xxweb

七、yaml
(1)用空格缩进,tab键不容许
(2)注释采用#
(3)列表成员使用- ,多项之间用逗号分开
(4)键值对采用冒号分隔
(5)字符串一般使用引号
为了方便输入,能够设置vim
[root@room8pc16 ansi]# vim ~/.vimrc
autocmd FileType yaml setlocal sw=2 ts=2 et ai
八、使用playbook
(1)在全部主机上安装vsftpd
[root@room8pc16 ansi]# vim a.yml

  • name: configure vsftpd
    hosts: all
    tasks:sql

    • name: install vsftpd
      yum:
      name: vsftpd
      state: latestshell

    • name: start vsftpd
      service:
      name: vsftpd
      state: started
      enabled: true
      九、语法检查
      [root@room8pc16 ansi]# ansible-playbook --syntax-check a.yml
      十、执行playbook
      [root@room8pc16 ansi]# ansible-playbook a.yml -k
      十一、查看ansible模块列表
      [root@room8pc16 ansi]# ansible-doc -l
      十二、查看yum模块使用方法
      [root@room8pc16 ansi]# ansible-doc yum
      1三、不容许mysql服务器上出现apache
      [root@room8pc16 ansi]# vim a.yml 追加如下内容
  • name: remove httpd
    hosts: dbservers
    tasks:
    • name: remove apache web server
      yum:
      name: httpd
      state: absent
      [root@room8pc16 ansi]# ansible-playbook a.yml -k
      1四、使用lineinfile模块
      [root@room8pc16 ansi]# vim b.yml

  • name: configure file
    hosts: all
    tasks:
    • name: configure hosts file
      lineinfile:
      path: /etc/hosts
      line: "192.168.4.254 host.tedu.cn host"
    • name: configure selinux file
      lineinfile:
      path: /etc/selinux/config
      regexp: '^SELINUX='
      line: 'SELINUX=permissive'
      [root@room8pc16 ansi]# ansible-playbook b.yml -k

1五、使用循环
[root@room8pc16 ansi]# vim lamp.yml

  • name: configure services
    hosts: dbservers
    tasks:
    • name: install services
      yum:
      name: "{{ item }}"
      state: latest
      with_items:
      • httpd
      • php
      • php-mysql
      • mod_ssl
      • mariadb-server
        1六、ansible中文站点 http://www.ansible.com.cn1七、经常使用模块:yum/service/lineinfile/copy/file/stat/debug/firewalld/command/shell
相关文章
相关标签/搜索