安装ansible
[root@longma ~]# yum install ansible -y
[root@longma ansible]# ansible --version
ansible 2.3.1.0
config file = /etc/ansible/ansible.cfg
常用配置
#inventory      = /etc/ansible/hosts  #仓库,linux下的hosts,ansible自己的hosts
#library        = /usr/share/my_modules/ #放第三方插件
#module_utils   = /usr/share/my_module_utils/ 
#remote_tmp     = ~/.ansible/tmp # 在远程生成的临时目录
#local_tmp      = ~/.ansible/tmp #本地生成的临时目录
#forks          = 5 #线程数,如果远程机器比较多,可以多开几个
#remote_port    = 22
#timeout = 10
#roles_path    = /etc/ansible/roles #剧本路径
#log_path = /var/log/ansible.log 
#executable = /bin/sh
#action_plugins     = /usr/share/ansible/plugins/action
#filter_plugins     = /usr/share/ansible/plugins/filter
host_key_checking = False 这项要开启,不然做了认证每次都提示输入yes很烦人
配置/etc/ansible/hosts
[web]
192.168.1.116
192.168.1.117
192.168.1.118
配置密钥认证
ssh-keygen
ssh-copy-id root@192.168.1.116 给ansible自己做认证
ssh-copy-id root@192.168.1.117
ssh-copy-id root@192.168.1.118
测试:
[root@longma ansible]# ansible web -m shell -a "date"
192.168.1.118 | SUCCESS | rc=0 >>
Mon Jul 17 00:43:33 CST 2017

192.168.1.117 | SUCCESS | rc=0 >>
Mon Jul 17 00:43:17 CST 2017

192.168.1.116 | SUCCESS | rc=0 >>
Mon Jul 17 01:11:25 CST 2017
ansible option:
  • -C:–check假执行,不实际执行
  • –list-hosts 显示有哪些主机
    ansible websrv --list-hosts #会显示该组所有的主机
  • -m Modules 指定模块名
ansible工作原理

ansible运维自动化之二---基础模块使用-LMLPHP
ansible运维自动化之二---基础模块使用-LMLPHP

ansible palybooks : ansible剧本
inventory: ansible管理主机的清单
modules: 模块
plugins:插件
API: 供第三方程序调用的应用程序编程接口
图中的组合inventory API Modules Plugins可以理解为是ansible命令工具,其为核心执行工具

在各个主机~/.ansible/tmp自创建目录执行命令,执行完后自清除

ansible使用
ansible-doc -l显示支持的所有模块
ansible-doc command  显示该模块的用法
ansible-doc copy
添加用户:

note:

  • command命令不支持管道 | > <
  • shell命令支持 | > < 但是对aws的 awk “{print $1}” 等一些符号支持不太好,我们
    将负杂的awk命令写成脚本传到远程服务器去执行
普通用户使用ansible命令
su -stanley
ssh-keygen生成密钥 普通用户执行生成密钥
ssh-coyp-id stanley@192.168.1.117 将普通用户生成的密钥传递到相应家目录
开启各主机该普通用户免密码使用sudo权限.默认普通用户sudo ls 使用sudo命令需要输入密码
root@longma~]#visudo
%wheel        ALL=(ALL)       NOPASSWD: ALL #启用wheel组免密码登录
root@longma~]#useradd -G wheel stanly  #把stanly加入wheel组,没创建用户的时候
root@longma~]# usermod -a -G wheel stanly # append到wheel组里面
root@longma~]# usermod -G  wheel stanly #表示该wheel组只有stanly用户
执行相应的ansible命令
ansible web -m ping -u stanley -b --become-user root #-u表示使用远程哪个普通用户,-b表示要变成哪个用户去执行命令
root用户直接执行相应ansible命令即可,都是有权限的
command和shell模块
ansible web -m ping
ansible web -m command -a "useradd stanley"
ansible web -m shell -a 'echo password |passwd --stdin stanley'
#注意不要利用command执行添加密码,因为command不支持 < > | ;& 此类符号
copy模块
ansible-doc copy
ansible web -m copy -a "src=/etc/fstab dest=/tmp/  backup=yes owner=stanley mode=644 group=stanley"
#注意只有目标服务器有fstab这个同名文件,且文件不一致时才会备份目标主机的文件,然后覆盖该同名文件
fetch模块
copy是推送文件到其它服务器,fetch是从其它服务器将文件拉取到本地以主机名方式存储,且不能拉取文件夹,fetch的src是目标主机的地址,dest要存到hosts主机设置的哪个位置,
1.建议使用单个主机,就是ansible 192.168.1.113 xxxx,
2.如果是文件夹就先打包
3.ansible web 也挺好直接全部复制到所有主机上了
4.打压缩包的时候不跟路径,默认存放到家目录
ansible web -m shell -a "tar -zcvf /tmp/fstab.tar.gz /tmp/fstab"
ansible web -m fetch -a "src=/tmp/fstab.tar.gz  dest=/tmp"
#这里不知道压缩路径,默认放在家目录下面
[root@localhost tmp]# pwd
/tmp/192.168.1.113/tmp
[root@localhost tmp]# ls
fstab  fstab.tar.gz

file模块使用
创建软连接
ansible web -m file -a "src=/tmp/fstab dest=/tmp/fstab.link state=link"
设置属主属组及权限
ansible web -m file -a "path=/tmp/fstab mode=400 owner=root group=root"
创建目录
ansible web -m file -a "path=/tmp/stanleydir state=directory mode=0755"
group模块使用
   ansible web -m group -a "name=natash gid=330 state=present system=yes"
   ansible web -m shell  -a "tail -2 /etc/group"
   ansible web -m group -a "name=natash state=absent"
   ansible web -m shell  -a "tail -2 /etc/group"
hostname模块使用设置主机名
vim /etc/ansible/hosts
[web]
192.168.1.113 hostname=longma-node13
192.168.1.117 hostname=longma-node17
192.168.1.118 hostname=longma-node18

ansible web -m hostname -a "name={{ hostname }}"
ansible web -m shell  -a "hostname"

#如果批量更改主机名呢,上面跟在ip后面是定义单个主机的变量,我们还可以定制组变
#量,但是如果主机变量设置了,主机变量优先级大于组变量


vim /etc/ansible/hosts
    [web:vars]  #这里定义组变量
    http_port=808

    [web]
    192.168.1.113 hostname=longma-node13
    192.168.1.117 hostname=longma-node17 http_port=80 #这里是自己变量
    192.168.1.118 hostname=longma-node18

ansible web -m hostname -a "name={{ hostname }}-{{ http_port }}"
[root@localhost tmp]# ansible web -m shell  -a "hostname"
192.168.1.118 | SUCCESS | rc=0 >>
longma-node18-808

192.168.1.117 | SUCCESS | rc=0 >>
longma-node17-80

192.168.1.113 | SUCCESS | rc=0 >>
longma-node13-808

在/etc/ansible/hosts不仅可以使用ip,还可以使用hosts主机名

vim /etc/ansible/hosts
[web]
node[1:3]

vim /etc/hosts
192.168.1.113 node1
192.168.1.117 node2
192.168.1.118 node3

ansible web -m "ping"
yum模块使用
install: present installed latest  remove: absent
ansible web -m shell -a "rpm -q nginx"
ansible web -m yum   -a "name=nginx state=present"
service模块使用
ansible web -m service -a "name=nginx state=started"
ansible web -m service -a "name=nginx state=stopped"
User模块
ansible-doc user
ansible web -m user -a 'name=gaomei uid=10088 group=root'
ansible web -m user -a 'name=gaomei state=absent'
ansible web -m shell -a "tail -2 /etc/passwd"
Script模块

脚本在远程服务器执行

touch /tmp/echo.sh
#!/bin/bash
touch /tmp/echolog.txt

ansible web -m script -a '/tmp/echo.sh'
Cron模块
ansible-doc cron
ansible web -m cron -a 'name="lbcron" minute="0" hour="3" weekday="0" job="ls /tmp >/tmp/lbcron.log" '
crontab -l
ansible web -m cron -a 'name="lbcron" state=absent '
01-30 09:22