ansible:集中管理平台

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

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

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

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

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

  • name: configure vsftpd
    hosts: all
    tasks:

    • name: install vsftpd
      yum:
      name: vsftpd
      state: latest

    • name: start vsftpd
      service:
      name: vsftpd
      state: started
      enabled: true
      9、语法检查
      [root@room8pc16 ansi]# ansible-playbook --syntax-check a.yml
      10、执行playbook
      [root@room8pc16 ansi]# ansible-playbook a.yml -k
      11、查看ansible模块列表
      [root@room8pc16 ansi]# ansible-doc -l
      12、查看yum模块使用方法
      [root@room8pc16 ansi]# ansible-doc yum
      13、不允许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
      14、使用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

15、使用循环
[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
        16、ansible中文站点 http://www.ansible.com.cn
        17、常用模块:yum/service/lineinfile/copy/file/stat/debug/firewalld/command/shell
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章