ansible到底咋用?

ansible是一个自动化工具。它能配置系统,部署软件。编排更复杂的IT任务。用python编写:
工作原理:
在这里插入图片描述

在ansible 管理体系中,存在“管理节点”和“被管理节点” inventory(称为资产) 在管理节点上,Ansible将 AdHoc 或 PlayBook 转换为Python脚本。在资源inventory中调取资源(ip)将python程序在被管理主机中运行(通过ssh)然后返回结果给管理节点。

如何安装:
在这里插入图片描述
yum安装:yum -y install epel-release
yum -y install ansible absible --version(查看版本)

管理节点和被管理节点之间建立SSH信任关系
ssh-keygen -t rsa
ssh-copy-id root@IP(被管理的主机的。例如:192.168.179.149)
场景:管理节点:192.168.179.148
被管理节点(资产):192.168.179.149 10.9.29.151
且管理节点和被管理节点之间的节点已经打通SSH信任关系

hosts(资产)(自定义的)
cd /etc/ansible
在这里插入图片描述
vim hosts
在这里插入图片描述

ansible all -i hosts -m ping
在这里插入图片描述
模块类型:
command & shell模块

shell 模块可以执行SHELL 的内置命令和 特性(比如管道符)。
command 模块无法执行SHELL 的内置命令和特性

在这里插入图片描述在这里插入图片描述
script模块

将管理节点上的脚本传递到被管理节点(远程服务器)上进行执行。

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

copy模块:

参数:src 指定拷贝文件的源地址 dest 指定拷贝文件的目标地址 backup 拷贝文件前,若原目标文件发生了变化,则对目标文件进行备份 owner 指定新拷贝文件的所有者 group 指定新拷贝文件的所有组 mode 指定新拷贝文件的权限

#cat nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/ r e l e a s e v e r / releasever/ releasever/basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/ r e l e a s e v e r / releasever/ releasever/basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
#ansible webservers -i hosts -m copy -a “src=./nginx.repo ”dest=/etc/yum.repos.d/nginx.repo”

copy前,在被管理节点上原文件备份

ansible all -i hosts -m copy -a “src=./nginx.repo dest=/etc/yum.repos.d/nginx.repo backup=yes”

copy 文件的同时对文件进行用户及用户组设置

ansible all -i hosts -m copy -a “src=./nginx.repo dest=/etc/yum.repos.d/nginx.repo owner=nobody group=nobody”

copy 文件的同时对文件进行权限设置

ansible all -i hosts -m copy -a “src=./nginx.repo dest=/etc/yum.repos.d/nginx.repo mode=0755”

yum模块:

参数:name 要安装的软件包名, 多个软件包以英文逗号(,) 隔开 state 对当前指定的软件安装、移除操作(present installed latest absent removed)
支持的参数
- present 确认已经安装,但不升级
- installed 确认已经安装
- latest 确保安装,且升级为最新
- absent 和 removed 确认已移除

ansible webservers -i hosts -m yum -a “name=nginx state=installed”

在这里插入图片描述
安装一个软件包

#ansible webservers -i hosts -m yum -a “name=nginx state=present”
#ansible webservers -i hosts -m yum -a “name=nginx state=latest”
#ansible webservers -i hosts -m yum -a “name=nginx state=installed”

移除一个软件包

#ansible webservers -i hosts -m yum -a “name=nginx state=absent”
#ansible webservers -i hosts -m yum -a “name=nginx state=removed”

安装一个软件包组

#ansible webservers -i hosts -m yum -a “name=’@Development tools’ state=present”

systemd模块
重新加载 systemd

#ansible webservers -i hosts -m systemd -a “daemon_reload=yes”

启动 Nginx 服务

#ansible webservers -i hosts -m systemd -a “name=nginx state=started”

关闭 Nginx 服务

#ansible webservers -i hosts -m systemd -a “name=nginx state=restarted”

重新加载 Nginx 服务

#ansible webservers -i hosts -m systemd -a “name=nginx state=reloaded”

将 Nginx 服务设置开机自启动

#ansible webservers -i hosts -m systemd -a “name=nginx enabled=yes”

group模块
创建普通组 db_admin

#ansible dbservers -i hosts -m group -a “name=db_admin”

user模块

常用参数:
name 必须的参数, 指定用户名
password 设置用户的密码,这里接受的是一个加密的值,因为会直接存到 shadow, 默认不设置密码
create_home 在创建用户时,是否创建其家目录。默认创建,假如不创建,设置为 no。2.5版本之前使用 createhome
group 设置用户的主组
groups 将用户加入到多个其他组中,多个用逗号隔开。默认会把用户从其他已经加入的组中删除。
append yes|no 和 groups 配合使用,yes 时,不会把用户从其他已经加入的组中删除
state 删除或添加用户, present 为添加,absent 为删除;默认值 present
remove 当与 state=absent 一起使用,删除一个用户及关联的目录,比如家目录,邮箱目录。可选的值为: yes/no

创建用户并设置密码 先生成加密密码

[root@qfedu.com ~]# pass=$(echo “123456” | openssl passwd -1 -stdin)

执行 ansible 命令 创建用户 foo 并设置密码

#ansible all -i hosts -m user -a “name=foo password=${pass}”

创建用 tom, 并且加入到组 db_admin 中, 不改变用户原有加入的组。

#ansible dbservers -i hosts -m user -a “name=foo gorup=db_admin”

date模块

// 计算 3 小时之后是几点几分
#date +%T -d ‘3 hours’
// 任意日期的前 N 天,后 N 天的具体日期
#date +%F -d “20190910 1 day”
#date +%F -d “20190910 -1 day”
计算两个日期相差天数, 比如计算生日距离现在还有多少天
#d1= ( d a t e + d 2 = (date +%s -d 20180728) d2= (date+d2=(date +%s -d 20180726)
echo $(((d1-d2)/86400))

file模块

// 创建一个文件
#ansible all -i hosts -m file -a “path=/tmp/foo.conf state=touch”
// 改变文件所有者及权限
ansible all -i hosts -m file -a “path=/tmp/foo.conf owner=nobody group=nobody mode=0644”
// 创建一个软连接
ansible all -i hosts -m file -a “src=/tmp/foo.conf dest=/tmp/link.conf state=link”
// 创建一个目录
ansible all -i hosts -m file -a “path=/tmp/testdir state=directory”
// 取消一个连接
ansible all -i hosts -m file -a “path=/tmp/link.conf state=absent”
// 删除一个文件
ansible all -i hosts -m file -a “path=/tmp/foo.conf state=absent”

cron模块

// 新建一个 CRON JOB 任务
#ansible all -i hosts -m cron -a “name=‘create new job’ minute=‘0’ job=‘ls -alh > /dev/null’”
// 删除一个 CRON JOB 任务,删除时,一定要正确指定job 的name参数,以免误删除。
#ansible all -i hosts -m cron -a “name=‘create new job’ state=absent”

登录任何一台管理机验证cron

#crontab -l
#Ansible: create new job
0 * * * * ls -alh > /dev/null

debug模块
这里是打印出每台主机在 资产清单中的名称,可能是 IP,也可能是 主机名,清单中写什么这里就会显示什么。

#ansible all -i hosts -m debug -a “var=inventory_hostname”
#ansible all -i hosts -m debug -a “msg='role is {{inventory_hostname}} '”

lineinfile模块
在被管理节点上,用正则匹配的方式对目标文件的一行内容修改删除等操作。

如果是在一个文件中把所有匹配到的多行都进行统一处理,请参考replace 模块。

如果想对一个文件进行一次性添加/更新/删除连续多行内容等操作,参考blockinfile模块
删除被控节点文件中的某一条内容

#ansible dbservers -i hosts -m lineinfile -a “path=/etc/sudoers regexp=’^%wheel’ state=absent”

替换某一行

#ansible dbservers -i hosts -m lineinfile -a “path=/etc/selinux/config regexp=’^SELINUX=’ line=‘SELINUX=disabled’ state=present”

ini_file模块
常用参数

  • path 目标文件路径
  • block 文件中被操作的块内容
  • state 块内容如何处理,absent 删除, present 添加/更新(默认值)

ansible all -i hosts -m ini_file -a “path=/root/a.conf section=mysqld option=port value=3306”

[root@ela2 ~]# cat a.conf
[mysqld]
port = 3306

假如存在,则修改

ansible all -i hosts -m ini_file -a “path=/root/a.conf section=mysqld option=port value=3307”

验证

[root@ela2 ~]# cat a.conf

[mysqld]
port = 3307

再次添加一个

ansible all -i hosts -m ini_file -a “path=/root/a.conf section=mysqld option=enable value=‘1’”

验证

[root@ela2 ~]# cat a.conf
[mysqld]
port = 3307
enable = 1

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值