违法℃玩家 发表于 2018-7-30 11:16:16

LH01_ansible及其常见模块01

  ansible及其常见模块01
  目录:
  1)ansible的特性
  2)ansible安装
  3)ansible配置添加管理主机
  4)配置基于ssh密钥管理
  5)如何查看模块帮助
  6)ansible命令应用基础
  7)ansible常见模块
  Ansible核心组件:
  ansible core
  host iventory
  Core Moduies
  Custom Modules
  Playbooks(yaml,jinjia2)
  Connection Plugins
  1)ansible的特性:
  基于Python语言实现,由Paramiko,PyYAML和Jinja2三个关键模块
  部署简单,agentless
  默认使用SSH协议;
  (1)基于密钥认证;
  (2)在inventory文件中指定账号和密码;
  主从模式:
  master:ansible,ssh client
  slave:ssh server
  支持自定义模块:支持各种编程语言
  支持Playbook
  基于“模块”完成各种“任务”
  2)ansible安装
  安装依赖于epel源:
yum install epel-release  
yum info ansible
  
yum install ansible -y
  配置文件:/etc/ansible/ansible.cfg
  Inventory:/etc/ansible/hosts
  3)ansible配置添加管理主机
  1.配置被管理的主机
# cd /etc/ansible/  
# ls
  
ansible.cfghostsroles
  
# cp hosts{,.bak}
  
# vi hosts
  

  
192.168.31.212
  
192.168.31.213
  

  
192.168.31.214
  4)配置基于ssh密钥管理
  (1)在管理主机上执行如下:
ssh-keygen -t rsa  
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.31.212
  
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.31.213
  
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.31.214
  (2)测试所有主机同步时间:
# ssh root@192.168.31.214 'date'  
# ssh root@192.168.31.212 'ntpdate pool.ntp.org'
  
# ssh root@192.168.31.213 'ntpdate pool.ntp.org'
  
# ssh root@192.168.31.214 'ntpdate pool.ntp.org'
  5)如何查看模块帮助
  ansible-doc -l
  ansible-doc -s MODULE_NAME
  6)ansible命令应用基础
  语法: ansible <host-pattern> [-f forks]
  [-m module_name] [-a args]
  -f forks:启动的并发线程数;
  -m module_name:要使用的模块
  -a args:模块特有的参数;
  7)ansible常见模块
  ## command : 命令模块,默认模块,用于在远程执行命令;
  如:ansible all -a 'date'
# ansible websrvs -m command -a 'date'  
# ansible dbsrvs -m command -a 'date'
  
# ansible all -m command -a 'date'##运用所有主机
  ## cron : 添加移动计划任务
  state:
  present:安装
  absent:移除
添加:  
# ansible websrvs -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron"'
  
移除:
  
# ansible websrvs -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron" state=absent'
  ## user :添加移除用户
  name=:指明创建的用户名
添加用户  
# ansible all -m user -a 'name="user1"'
  
移除用户
  
# ansible all -m user -a 'name="user1" state=absent'
  ## group :添加移动组
# ansible websrvs -m group -  
a 'name=mysql gid=306 system=yes'
  
# ansible websrvs -m user -a 'name=mysql uid=306 system=yes group=mysql'
  ## copy : 复制文件
  src=:定义本地源文件路径
  dest=:定义远程目标绝对路径
  content=:取代src=,表示直接用此处指定的信息生成为目标文件的内容;
# ansible all -m copy -a 'src=/etc/fstab dest=/tmp/fstab.ansible owner=root mode=640'  
# ansible all -m copy -a 'content="hello Ansible\nHi magedu" dest=/tmp/test.ansible'
  ## file :设定文件属性
  path=:指定文件路径,可以使用name或dest来替换;
  创建文件的符号链接:
  src=:指明源文件
  path=:指定符号链接文件路径
# ansible all -m file -a 'owner=mysql group=mysql mode=644 path=/tmp/fstab.ansible'  
创建符号链接:
  
# ansible all -m file -a 'path=/tmp/fstab.link src=/tmp/fstab.ansible state=link'
  ## ping : 测试指定主机是否能连接;
# ansible all -m ping  ## service:指定运行状态;
  enabled=:是否开机自动启动,取值为true或者false;
  name=:服务名称
  state=:状态,取值有started,stopped,restarted;
# ansible websrvs -m service -a 'enabled=true name=httpd state=started'  
# ansible websrvs -a 'service httpd status'
  
# ansible websrvs -a 'chkconfig --list httpd'
  ## shell : 在远程主机上运行命令
  尤其是用到管道等功能的复杂命令;
# ansible all -m user -a 'name=user1'  
# ansible all -m shell -a 'echo mageedu | passwd --stdin user1'
  ## script: 将本地脚本复制到远程主机并运行之;
  注意:要使用相对路径指定脚本
# cat /tmp/test.sh  
#!/bin/bash
  
echo "hello ansible from script" > /tmp/script.ansible
  
useradd user2
  
# ansible all -m script -a "/tmp/test.sh"
  ## yum :安装程序包
  name=:指明要安装的程序包,可以带上版本号;
  state=:present,latest表示安装,absent表示卸载;
# ansible all -m yum -a "name=zsh"  
# ansible all -m yum -a "name=zsh state=absent"
  ## setup : 收集远程主机的facts
  每个被管理节点在接收并运行管理命令之前,会将自己主机相关信息,如操作系统版本、IP地址等报告给远程的ansible主机;
# ansible all -m setup
页: [1]
查看完整版本: LH01_ansible及其常见模块01