hc6538 发表于 2018-7-29 06:56:14

ansible:集中管理平台

  无服务、无agent、采用ssh管理远程主机、多线程
  1、配置文件/etc/ansible/ansible.cfg
  2、管理方式
  (1)ad-hoc 临时命令
  (2)playbook剧本
  远程管理
  1、新建一个目录
  # mkdir ansi
  # cd ansi
  2、创建配置文件
  # vim ansible.cfg
  
  inventory = inventory      # 定义被管理主机到哪个文件中查找
  remote_user = root         # ssh到远程主机的用户
  3、创建主机清单
  # vim inventory
         # 定义主机组名
  192.168.4.1       # 定义组成员主机
  
  192.168.4.2
  192.168.4.3
  4、列出主机命令,虽然all没有定义,但是它是保留字,表示所有主机
  # ansible all --list-hosts
  # ansible dbservers --list-hosts
  # ansible webservers --list-hosts
  5、测试到远程主机的通信
  # ansible all -m ping -k
  6、在所有的主机上执行任意命令
  # ansible all -a 'touch /opt/abc.txt' -k
  以下命令是远程开机命令,与ansible无关
  # ether-wake -i enp2s0 xx:xx:xx:xx:xx:xx
7、yaml
  (1)用空格缩进,tab键不允许
  (2)注释采用#
  (3)列表成员使用- ,多项之间用逗号分开
  (4)键值对采用冒号分隔
  (5)字符串通常使用引号
  为了方便输入,可以设置vim
  # vim ~/.vimrc
  autocmd FileType yaml setlocal sw=2 ts=2 et ai
  8、使用playbook
  (1)在所有主机上安装vsftpd
  # 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、语法检查
  # ansible-playbook --syntax-check a.yml
  10、执行playbook
  # ansible-playbook a.yml -k
  11、查看ansible模块列表
  # ansible-doc -l
  12、查看yum模块使用方法
  # ansible-doc yum
  13、不允许mysql服务器上出现apache
  # vim a.yml 追加以下内容

[*]name: remove httpd  hosts: dbservers
  tasks:

[*]name: remove apache web server
  yum:
  name: httpd
  state: absent
  # ansible-playbook a.yml -k
  14、使用lineinfile模块
  # vim b.yml

[*]name: configure file  hosts: all
  tasks:

[*]name: configure hosts file  lineinfile:
  path: /etc/hosts
  line: "192.168.4.254host.tedu.cnhost"
[*]name: configure selinux file  lineinfile:
  path: /etc/selinux/config
  regexp: '^SELINUX='
  line: 'SELINUX=permissive'
  # ansible-playbook b.yml -k

15、使用循环
  # 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


页: [1]
查看完整版本: ansible:集中管理平台