ansible批量搞机部署及常用模块(一),ansible部署模块


什么是ansible?
• Ansible是2013年推出的一款IT自劢化和DevOps软
件,目前由Redhat已签署Ansible收购协议。其是基
于Python研发,糅合了很多老运维工具的优点实现
了批量操作系统配置,批量程序的部署,批量运行命
令等功能
• ansible可以让我们实现:
– 自动化部署APP
– 自动化管理配置项
– 自动化的持续交付
– 自动化的(AWS)于服务管理


为什么要选择ansible ?
• 选择一款配置管理软件总的来说,无外乎从以下几点来权衡利弊
– 活跃度(社区活跃度)
– 学习成本
– 使用成本
– 编码诧言
– 性能
– 使用是否广泛


主机定义与分组
• 安装好了 Ansible 之后就可以开始一些简单的任务了

• Ansible配置文件查找顺序
– 首先检测 ANSIBLE_CONFIG 变量定义的配置文件
– 其次检查当前目彔下的 ./ansible.cfg 文件
– 再次检查当前用户家目彔下 ~/ansible.cfg 文件
– 最后检查 /etc/ansible/ansible.cfg 文件

• /etc/ansible/ansible.cfg 默认配置文件路径


实验环境:
  1 ansible1  192.168.1.10   管理机器
  2 web1     192.168.1.11   托管机器
  3 web2     192.168.1.12   托管机器
  4 db1      192.168.1.21   托管机器
  5 db2      192.168.1.22   托管机器
  6 cache    192.168.1.33   托管机器

一、部署和配置
1. 在 ansible 管理机器上,配置源
yum install -y ansible

2.安装完成以后执行,没有报错,正确显示版本即可
ansible --version

ansible 的配置文件是 ansible.cfg 
ansible.cfg 的查找顺序是 
1)ANSIBLE_CONFIG 变量定义的配置文件
2 ) 当前目录下的 ./ansible.cfg 文件
3 ) 前用户家目录下 ~/ansible.cfg 文件
4) /etc/ansible/ansible.cfg 文件


3. ansible.cfg 中 inventony 指定主机分组文件的路径和地址,默认分组文件 hosts
 14 inventory      = /etc/ansible/hosts   //一般都和ansible.cfg在一个文件路径 

hosts 的配置
[web]
web[1:2]

[db]
db1
db2

[app:children]  #指定子组[web组和db组就是子组]
web
db

[app:vars]
ansible_ssh_user="root"
ansible_ssh_pass="123456"

[other]
cache        

3.1 也可以自定义与分组
[root@ansible1 ~]# mkdir ooxx & cd ooxx
[root@ansible1 ooxx]# vim ansible.cfg     //创建ansible.cfg文件 
[defaults]
inventory   = hosts
host_keys_checking = False

4. 动态主机
ansible 的inventony 文件可以是静态也可以是脚本(输出格式 json)
修改 ansible.cfg 
inventory      = urscript

一个shell 脚本样例
#!/bin/bash
echo '
{
    "web"   : ["web1", "web2"],
    "db"    : ["db1", "db2"],
    "other" : ["cache"]
}'

二、常用模块
1.语法格式 
ansible  命令基础
ansible  主机分组  -m 模块  -a '命令和参数'

ansible命令基础
• ansible <host-pattern> [options]
– host-pattern 主机戒定义的分组

-M 指定模块路径
-m 使用模块,默认 command 模块
-a or --args 模块参数
-i inventory 文件路径,戒可执行脚本
-k 使用交互式登彔密码
-e 定义变量
-v 详绅信息,-vvvv 开吭 debug 模式

2.ping模块(主要是检测托管主机ssh的连通性,与 ping(ICMP)无关)
a.先修改hosts文件这里以cache主机为例子
[root@ansible1 ~]# vim /etc/ansible/hosts
[other]
cache        ansible_ssh_user="root" ansible_ssh_pass="123456"  ansible_ssh_port=22

b.接下来使用ping模块
[root@ansible1 ~]# ansible  other -m ping    //这里因为other组只有cache,所以写组或者写主机都是可以的
cache | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

3.authorized_key模块(批量传递公钥)         //做这个可以简化后期的操作,不用像上面在主机后面加添加用户和密码了

a.创建密钥对 id_rsa 是私钥,  id_rsa.pub 是公钥
ssh-keygen -f /root/.ssh/id_rsa  -N ''

b.给所有主机部署密钥
ansible all -m authorized_key -a "user=root exclusive=true manage_dir=true key='$(< /root/.ssh/id_rsa.pub)'" -k


4.command | shell | raw 模块
command 是默认模块,没有启用 shell ,所有shell 相关特性命令无法使用,例如 < > | &  set
raw    模块,没有 chdir create remove 等参数,能执行大部分操作(一些古老的uliunx系统)
shell  模块,启动 /bin/sh 运行命令,可以执行所有操作

测试
ansible cache -m command -a 'chdir=/tmp touch f1'   创建成功
ansible cache -m shell -a 'chdir=/tmp touch f2'     创建成功
ansible cache -m raw -a 'chdir=/tmp touch f3'       文件可以创建,但无法切换目录,文件在用户家目录下生成


[root@ansible1 ~]# ansible cache  -a 'cat /etc/passwd | wc -l'    //执行失败
[root@ansible1 ~]# ansible cache  -m shell -a 'cat /etc/passwd | wc -l'   //执行成功


5.script 模块(支持shell脚本)
复杂操作怎么办,使用脚本来解决
[root@ansible1 ~]# cd /etc/ansible/

[root@ansible1 ansible]# vim uscript.sh
#!/bin/bash
id  zhang3
if [ $? != 0 ];then
    useradd li4
    echo 123456 |passwd --stdin li4
      chage  -d  0  li4       //要求li4在第一次登陆的时候必须修改密码                    
fi

ansible all -m script -a "uscript.sh"   //这里脚本是在当前路径,可以直接相对路径,如果不是在当前路径需要写绝对路径


6.copy lineinfile replace 模块
copy 把文件发布到远程其他主机上面
lineinfile  修改一个文件的一行,以行为基础,整行修改
replace    修改文件的某一部分,以正则表达式匹配为基础修改

a. 利用 copy 模块修改所有机器的 /etc/resolv.conf 为 
[root@ansible1 ~]# vim /etc/resolv.conf
nameserver 8.8.8.8

[root@ansible1 ~]# ansible all -m copy -a 'src=/etc/resolv.conf dest=/etc/resolv.conf'

b. 利用 lineinfile 修改 /etc/sysconfig/network-scriopts/ifcfg-eth0(整行替换) 
ONBOOT=yes|no
ansible cache -m lineinfile -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth0 regexp="^ONBOOT" line="ONBOOT=\"no\""

c. 利用 replace 修改 /etc/sysconfig/network-scriopts/ifcfg-eth0  ONBOOT=no|yes(匹配什么替换什么)
ansible cache -m replace -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth0 regexp="^(ONBOOT=).*" replace="\1\"yes\""'


7. yum 模块  installed 安装, removed 删除
ansible other -m yum -a 'name="lrzsz" state=removed' 
ansible other -m yum -a 'name="lftp"  state=removed'
ansible other -m yum -a 'name="lrzsz,lftp" state=installed'


8.service 模块  name 指定服务名称,enabled= yes|no 设置开机启动, state=stopped|started 启动关闭服务

设置 chronyd 服务开启启动,并启动服务
ansible other -m service -a 'name="chronyd" enabled="yes"  state="started"'

9.setup 模块,查看信息  filter 过滤指定的关键字


模块 
ansible-doc  查看帮助,必须掌握
ansible-doc -l  列出所有模块
ansible-doc  模块名   查看该模块的帮助信息


 

相关内容

    暂无相关文章