ansible基础知识

什么是ansible:

批量部署操作系统,程序,以及批量运行命令等功能,开源,基于python研发,自动化部署APP,自动化持续交付,自动化云服务管理使用成本低性能高使用广泛,社区活跃度高。

ansible优点:只需要ssh和python即可使用无客户端功能强大,模块丰富
上手容易门槛低做二次开发容易使用公司多,社区活跃

ansible特性:模块化设计
一:安装ansible
准备环境
在这里插入图片描述
1.真机共享Yum源:

[student@room9pc01~]$ systemctl restart vsftpd
[student@room9pc01~]$ mkdir /var/ftp/ansible
[student@room9pc01 ~]$ cd /linux-soft/04
[student@room9pc01 ansible]$ ls
ansible-2.4.2.0-2.el7.noarch.rpm         python-paramiko-2.1.1-4.el7.noarch.rpm
python2-jmespath-0.9.0-3.el7.noarch.rpm  python-passlib-1.6.5-2.el7.noarch.rpm
python-httplib2-0.9.2-1.el7.noarch.rpm   sshpass-1.06-2.el7.x86_64.rpm
[student@room9pc01 ansible]$ cp * /var/ftp/ansible/
[student@room9pc01 ansible]$ createrepo /var/ftp/ansible/

2.配置ip和Yum仓库,这里以ansible主机为例子(其他机器也需要配置)

[root@ansible ~]# vim /etc/yum.repos.d/local.repo 
[local_repo]
name=CentOS-$releasever - Base
baseurl="ftp://192.168.1.254/system"
enabled=1
gpgcheck=1
[local]
name=local
baseurl="ftp://192.168.1.254/ansible"
enabled=1
gpgcheck=0
[root@ansible ~]# yum -y install ansible
[root@ansible ~]# ansible --version
ansible 2.4.2.0        //显示版本说明安装成功

二 :主机管理
添加主机名映射

[root@ansible ~]#vim /etc/hosts
192.168.4.40 ansible
192.168.4.41 web1
192.168.4.42 web2
192.168.4.43 db1
192.168.4.44 db2
192.168.4.45 cache

修改ansible配置文件

[root@ansible ~]#vim /ect/ansible/ansible.cfg	//配置文件
inventory      = /etc/ansible/hosts		//去掉注释	指定配置文件写入远程主机的地址。
host_key_checking = False			//去掉注释如果为False,ssh时候不需要输入yes

[root@ansible ~]# vim  /etc/ansible/hosts  //修改主机分组配置文件
[web]  #组名
web1 #组成员
web2 #组成员

[db]
db[1:2]  #这也代表两个组成员

[other]
cache

部署免密登录

[root@ansible ~]# ssh-keygen	生成密钥对
[root@ansible ~]#for i in ansible web1 web2 db1 db2 cache
>>> do
>>> ssh-copy-id root@$i
>>> done

三:验证环境是否搭建成功
语法格式: ansible 组名称(all表示全部,多个组中间逗号间隔) --list-hosts

[root@ansible ~]# ansible all --list-hosts   列出全部组成员
  hosts (5):
    web1
    web2
    cache
    db1
    db2
[root@ansible ~]# ansible web --list-hosts
  hosts (2):
    web1
    web2

语法格式: ansible 组名称 -m 模块 -c 参数

[root@ansible ~]# ansible all -m ping    //检测组成员是否正常,ping模块没有参数
web2 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
web1 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
db1 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
cache | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
db2 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

inventory扩展参数(了解即可,本环境无需配置)

[root@ansible ~]#vim /etc/ansible/host
[db:vars]		//db(组名)	:vars是(固定字符)表示给组定义参数
ansible_ssh_user="root"		//ansible连接db组所有主机时候默认用root用户连接

[db]
db1	
db2	 ansible_ssh_port=222	//给单台机器定义参数,默认连接22,需要指定新的端口


[app:children]	//用children声明以下是组名并非主机名
db		//db组
web		//web组

自定义主机分组文件
若多人想分不同的组可以自定义配置文件,利用ansible配置文件查找顺序
ansible会优先查看当前目录的ansible.cfg的配置文件

[root@ansible ~]# mkdir myansible
[root@ansible ~]# cd myansible/
[root@ansible myansible]# vim ansible.cfg
[defaults]
inventory=myhost
host_key_checking = False

[root@ansible myansible]# vim myhost
[pc]
web1
db1
cache

[pc2]
web2
db2

[root@ansible myansible]# ansible pc --list-hosts  //管理其他机器时必须在此目录下
  hosts (3):
    web1
    db1
    cache
[root@ansible myansible]# cd 
[root@ansible ~]# ansible pc --list-hosts  //退出myansible目录后就找不到pc组
 [WARNING]: Could not match supplied host pattern, ignoring: pc

 [WARNING]: No hosts matched, nothing to do

  hosts (0):

模块

查看某个模块的帮助信息 ansible-doc 模块名
ansbile-doc -l //列出所有模块
command模块(默认模块)

[root@ansible ~]# ansible web -m command -a 'uptime'   查看web组所有主机的CPU
web2 | SUCCESS | rc=0 >>
 15:00:43 up  3:34,  1 user,  load average: 0.00, 0.01, 0.05 (1分钟,5分钟,15分钟)的系统负载

web1 | SUCCESS | rc=0 >>
 15:00:43 up  3:34,  1 user,  load average: 0.00, 0.01, 0.05
 root@ansible ~]# ansible web -a 'df -h /'  
web2 | SUCCESS | rc=0 >>
文件系统        容量  已用  可用 已用% 挂载点
/dev/vda1        30G  1.3G   29G    5% /

web1 | SUCCESS | rc=0 >>
文件系统        容量  已用  可用 已用% 挂载点
/dev/vda1        30G  1.3G   29G    5% /

command模块注意事项:
- 不能执行如下字符
- “<”,">",’ | ',"&"
- command 模块不能解析系统变量

shell模块
几乎所有命令都是可以用shell模块执行,所以可以不用command模块

[root@ansible ~]# ansible web -m shell -a'ps -ef | grep ssh' 列出ssh的进程ID
web2 | SUCCESS | rc=0 >>
root       790     1  0 11:28 ?        00:00:00 /usr/sbin/sshd -D
root     25745   790  0 15:13 ?        00:00:00 sshd: root@pts/0
root     25834 25833  0 15:13 pts/0    00:00:00 /bin/sh -c ps -ef | grep ssh
root     25836 25834  0 15:13 pts/0    00:00:00 grep ssh

web1 | SUCCESS | rc=0 >>
root       790     1  0 11:28 ?        00:00:00 /usr/sbin/sshd -D
root     26230   790  0 15:13 ?        00:00:00 sshd: root@pts/0
root     26319 26318  0 15:13 pts/0    00:00:00 /bin/sh -c ps -ef | grep ssh
root     26321 26319  0 15:13 pts/0    00:00:00 grep ssh

练习1 创建用户

- 给web1 db2 添加用户nb
- 设置nb的密码为123
[root@ansible ~]# ansible web1,db2 -m shell -a'useradd nb'
[root@ansible ~]# ansible web1,db2 -m shell -a'echo 123 | passwd --stdin root'
web1 | SUCCESS | rc=0 >>
更改用户 root 的密码 。
passwd:所有的身份验证令牌已经成功更新。

db2 | SUCCESS | rc=0 >>
更改用户 root 的密码 。
passwd:所有的身份验证令牌已经成功更新。

练习2 添加用户

  • 给所有web主机添加用户wk
  • 要求nb用户与wk用户不能存在同一台服务器
  • 设置wk用户的密码是456

script模块

[root@ansible ~]# vim user.sh
#!/bin/bash
id nb
if [ $? -ne 0 ];then
	useradd wk || echo 456 | passwd --stdin wk
fi
[root@ansible ~]# ansible web -m script -a'user.sh'
[root@ansible ~]# ansible web -m shell -a'id wk'   查看web1上是否有用户wk

yum模块和service模块

[root@ansible ~]# ansible db -m yum -a'name="mariadb-server" state=installed'
[root@ansible ~]# ansible db -m service -a'name="mariadb" state="started" enabled="yes"'
[root@ansible ~]# ansible db -m shell -a'ss -ntulp | grep 3306'
db1 | SUCCESS | rc=0 >>
tcp    LISTEN     0      50        *:3306                  *:*                   users:(("mysqld",pid=27087,fd=14))

db2 | SUCCESS | rc=0 >>
tcp    LISTEN     0      50        *:3306                  *:*                   users:(("mysqld",pid=27153,fd=14))

copy模块

同步目录
[root@ansible ~]# ansible all -m copy -a'src=/etc/yum.repos.d/ dest=/etc/yum.repos.d/'
同步文件
[root@ansible ~]# ansible all -m copy -a'src=/etc/hosts dest=/etc/hosts'

lineinfile模块
参数:

  • path 要修改的文件
  • regexp 要修改的行(使用正则表达式)
  • line 修改后的样子
[root@ansible ~]# ansible db -m shell -a'cat /etc/my.cnf | head -5'
db1 | SUCCESS | rc=0 >>
[mysqld]
server_id = 43
binlog-format = "row"
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
[root@ansible ~]# ansible db -m lineinfile -a'path="/etc/my.cnf" regexp="^binlog" lininfile="binlog-format = mixed"'
[root@ansible ~]# ansible db -m shell -a'cat /etc/my.cnf | head -5'  再次查看
db2 | SUCCESS | rc=0 >>
[mysqld]
server_id = 43
binlog-format = mixed
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

replace模块
参数:

  • path 要修改的文件
  • regexp 要修改的字符(使用正则表达式)
  • replace 修改后字符的样子
[root@ansible ~]# ansible db -m replace -a'path="/etc/my.cnf" regexp="mixed$" replace="row"'
db2 | SUCCESS => {
    "changed": true, 
    "msg": "1 replacements made"
}
db1 | SUCCESS => {
    "changed": true, 
    "msg": "1 replacements made"
}
[root@ansible ~]# ansible db -m shell -a'cat /etc/my.cnf | head -5'
db1 | SUCCESS | rc=0 >>
[mysqld]
server_id = 43
binlog-format = row
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

db2 | SUCCESS | rc=0 >>
[mysqld]
server_id = 43
binlog-format = row
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值