Ansible简介版

发布于:2024-05-07 ⋅ 阅读:(31) ⋅ 点赞:(0)

目录

架构

环境部署

一、Ansible安装部署

1.yum安装Ansible

2.修改主机清单文件

3.配置密钥对验证

4.ansible-doc

5.看被控主机

二、常用模块

1.Command模块

2.Shell模块

3.Cron模块

1.添加

2.删除

4.User模块

5.Group模块

1.创建组 

​编辑

​编辑

​编辑

2.删除组 

​编辑

6.cp模块

​编辑

7.File模块

8.Hostname模块

9.ping模块

10.Yum_repository模块

1.安装httpd 

2.删除 

3.安装nginx

11.Service模块

1.开启nginx

12.Script模块

13.setup模块


架构

    1. 核心:ansible
    2. 主机清单(Host Inventory):被管理主机的列表的文件;Ansible 可以根据这个清单文件来执行针对不同主机组的任务
    3. 剧本(playbook):ansible的任务配置文件,将多个任务定义在剧本中,由ansible自动去执行
  4. 核心模块(Core Modules):是ansible自带的模块
  5. 自定义模块模块(Custom Modules):在核心模块功能不足时,可使用自定义模块
  6. 连接插件(Connaction Plugins):使用ssh,来连接每一个被控制的主机
  7. 插件(Plugins):记录日志
  
  #######################################################################################
  
  1. 用户请求过来之后,给到我们核心Ansible
  
  2. Ansible通过主机清单去处理用户请求
    处理的两种方式:
  	1)使用剧本playbook,命令[yum  install...]操作处理
  	2)使用模块完成
  		1)核心模块【Core  Modules】直接完成,在没有核心模块时,使用自定义模块
  		2)自定义模块【Custom  Modules】
  		
  3. 通过连接插件【Connaction  Plugins】使用ssh,来连接每一个被控制的主机
  
  4. 最后由插件【Plugins】来记录日志

环境部署

主机 服务
192.168.91.102 Ansible
192.168.91.103 ————
192.168.91.104 ————

一、Ansible安装部署

1.yum安装Ansible

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# 
[root@localhost ~]# yum install epel-release -y
[root@localhost ~]# yum install ansible -y

2.修改主机清单文件

[root@localhost ~]# vim /etc/ansible/hosts

 45 [web]
 46 192.168.91.103
 47 192.168.91.104
 48 
 49 [all]
 50 192.168.91.[102:110]

3.配置密钥对验证

ssh-keygen -t rsa		#一路回车,使用免密登录
sshpass -p 'abc1234' ssh-copy-id root@192.168.91.103
sshpass -p 'abc1234' ssh-copy-id root@192.168.91.104

同样方法登录一下192.168.91.104

4.ansible-doc

[root@ansible ~]# ansible-doc -l
#查看所有支持的模块

5.看被控主机

ansible all --list

二、常用模块

1.Command模块

[root@localhost ~]# ansible web -a 'hostname'

[root@localhost ~]# ansible web -a 'touch /opt/test' 

[root@localhost ~]# ansible web -a 'ls /opt/test'

或者直接去7-3和7-4opt目录看一下,有没有test这个文件

ansible 192.168.91.104 -a "chdir=/opt ls ./"

2.Shell模块

功能:和command相似,用shell执行命令,支持各种符号,比如:*,$, >

注意:此模块不具有幂等

[root@localhost ~]# ansible web -m shell -a "echo hello > /opt/hello.txt"

[root@localhost ~]# ansible web -m shell -a "cat /opt/hello.txt"

ansible web -m shell -a 'echo $(ifconfig ens33 | awk "NR==2 {print $2}") | cut -d " " -f2'

3.Cron模块

功能:计划任务

支持时间:minute,hour,day,month,weekday

关键字:

name  会生成一行注释,显示标题如下显示
job   执行的命令

1.添加

[root@localhost mnt]# ansible 192.168.91.102 -m cron -a 'hour=*/2 minute=30 weekday=1-5 name="backup mysql" job=/root/mysql_backup.sh'

看结果

2.删除

[root@localhost mnt]# ansible 192.168.91.102 -m cron -a 'name="backup mysql" state=absent'

结果

4.User模块

###管理用户

comment         用户的描述信息
createhome      是否创建家目录
force           在使用state=absent时, 行为与userdel –force一致.
group           指定基本组
groups          指定附加组,如果指定为(groups=)表示删除所有组
home            指定用户家目录
move_home       如果设置为home=时, 试图将用户主目录移动到指定的目录
name            指定用户名
non_unique      该选项允许改变非唯一的用户ID值
password        指定用户密码,使用 SHA512 hash
remove          在使用state=absent时, 行为是与userdel –remove一致
shell           指定默认shell
state           设置帐号状态,不指定为创建,指定值为absent表示删除
system          当创建一个用户,设置这个用户是系统用户。这个设置不能更改现有用户
uid             指定用户的uid
update_ password 
  always      如果password参数设置的值与用户当前的加密过的密码字符串不一致,则直接更新用户的密码,默认值即为always
  on_create   如果password参数设置的值与用户当前的加密过的密码字符串不一致,则不会更新用户的密码字符串,保持之前的密码设定

ansible web -m user -a 'name="test1"'

看结果

删除用户

ansible web -m user -a 'name="test1" state=absent remove=yes'

看结果

5.Group模块

###管理组

1.创建组 

ansible 192.168.91.103 -m group -a 'name=test gid=88 system=yes'

查看创建结果

创建用户导入到组中

ansible 192.168.91.103 -m user -a 'name=test2 uid=504 system=yes group=test'

查看结果

id test2

2.删除组 

[root@ansible ~]# ansible 192.168.91.103 -m user -a 'name="test2" state=absent remove=yes'  ########先删除组中的用户

ansible 192.168.91.103 -m group -a 'name=test state=absent'

查看结果

6.cp模块

功能:解包解压缩

实现有两种用法:

1、将ansible主机上的压缩包传到远程主机后解压缩至特定目录,设置copy=yes,此为默认值,可省略

2、将远程主机上的某个压缩包解压缩到指定路径下,设置copy=no

常见参数:

copy:默认为yes,当copy=yes,拷贝的文件是从ansible主机复制到远程主机上,如果设置为copy=no,会在远程主机上寻找src源文件
remote_src:和copy功能一样且互斥,yes表示在远程主机,不在ansible主机,no表示文件在ansible主机上
src:源路径,可以是ansible主机上的路径,也可以是远程主机(被管理端或者第三方主机)上的路径,如果是远程主机上的路径,则需要设置copy=no
dest:远程主机上的目标路径
mode:设置解压缩后的文件权限

ansible 192.168.91.103 -m copy -a 'src=/etc/fstab dest=/opt/fstab_bak owner=root mode=640'

看结果

写入文件内容

ansible 192.168.91.103 -m copy -a 'content="xzq" dest=/opt/xzq.txt'

查看结果

7.File模块

功能:设置文件属性,创建软链接等

path       指定文件路径
state      文件状态 有:新建(touch) 删除(absent) 文件夹(directory)  连接文件(link)等
src        源文件
mode       权限
owner      属主
group      属组
recurse    递归

[root@localhost mnt]# ansible web -m file -a "path=/data/666 state=touch mode=644 owner=lisi group=lisi"

创建软连接

ansible web -m file -a 'path=/opt/fstab.link src=/opt/fstab.bak state=link'

查看设置结果

8.Hostname模块

###修改被管理主机的主机名

ansible 192.168.91.103 -m hostname -a 'name=node3 '
#一般不使用此模块,主机名会一致

9.ping模块

[root@localhost ~]# ansible web -m ping

Web是我们之前在主机清单文件中写的名字

10.Yum_repository模块

功能:建立yum仓库模块

关键字:

name参数:            必须参数,用于指定要操作的唯一的仓库ID,也就是”.repo”配置文件中每个仓库对应的”中括号”内的仓库ID。
baseurl参数:        此参数用于设置 yum 仓库的 baseurl。
description参数:    此参数用于设置仓库的注释信息,也就是”.repo”配置文件中每个仓库对应的”name字段”对应的内容。
file参数:            此参数用于设置仓库的配置文件名称,即设置”.repo”配置文件的文件名前缀,在不使用此参数的情况下,默认以 name 参数的仓库ID作                      为”.repo”配置文件的文件名前缀,同一个”.repo” 配置文件中 可以存在多个 yum 源。
enabled参数:        此参数用于设置是否激活对应的 yum 源,此参数默认值为 yes,表示启用对应的 yum 源,设置为 no 表示不启用对应的 yum 源。
gpgcheck参数:        此参数用于设置是否开启 rpm 包验证功能,默认值为 no,表示不启用包验证,设置为 yes 表示开启包验证功能。
gpgcakey参数:        当 gpgcheck 参数设置为 yes 时,需要使用此参数指定验证包所需的公钥。
state参数:        默认值为 present,当值设置为 absent 时,表示删除对应的 yum 源。

###yum安装

1.安装httpd 

[root@localhost mnt]# ansible web -m yum -a 'name=httpd state=present'

###state=present可以不加

结果

2.删除 

删除

[root@localhost mnt]# ansible web -m yum -a 'name=httpd state=present state=absent'

结果

3.安装nginx

ansible web -m yum_repository -a 'name=epel description=epel  baseurl=https://download.fedoraproject.org/pub/epel/$releasever/$basearch/ gpgcheck=no file=epel'

[root@localhost mnt]# ansible web -m yum -a 'name=nginx state=present'

看结果

11.Service模块

功能:管理服务

关键字:

name参数:        此参数用于指定需要操作的服务名称,比如 nginx。
state参数:    此参数用于指定服务的状态,比如,我们想要启动远程主机中的 nginx,则可以将 state 的值设置为 started;如果想要停止远程主机中的服               务,则可以将 state 的值设置为 stopped。此参数的可用值有 started、stopped、restarted、reloaded。
enabled参数:    此参数用于指定是否将服务设置为开机 启动项,设置为 yes 表示将对应服务设置为开机启动,设置为 no 表示不会开机启动。

1.开启nginx

[root@localhost mnt]# ansible web -m service -a 'name=nginx state=started enabled=yes'

curl 192.168.91.102
###看结果###

12.Script模块

功能:在远程主机上运行ansible服务器上的脚本(无需执行权限)

注意:此模块不具有幂等性

在Ansible服务器上,创建test.sh脚本

[root@ansible opt]# vim test.sh
[root@ansible opt]# chmod +x test.sh 
[root@ansible opt]# ansible web -m script -a '/opt/test.sh'

查看结果

13.setup模块

功能: setup 模块来收集主机的系统信息,这些 facts 信息可以直接以变量的形式使用,但是如果主机较多,会影响执行速度

可以使用 gather_facts: no 来禁止 Ansible 收集 facts 信

ansible web -m setup -a "filter=ansible_hostname"

ansible all -m setup
ansible all -m setup -a "filter=ansible_nodename"
ansible all -m setup -a "filter=ansible_hostname"
ansible all -m setup -a "filter=ansible_domain"
ansible all -m setup -a "filter=ansible_memtotal_mb"
ansible all -m setup -a "filter=ansible_memory_mb"
ansible all -m setup -a "filter=ansible_memfree_mb"
ansible all -m setup -a "filter=ansible_os_family"
ansible all -m setup -a "filter=ansible_distribution_major_version"
ansible all -m setup -a "filter=ansible_distribution_version"
ansible all -m setup -a "filter=ansible_processor_vcpus"
ansible all -m setup -a "filter=ansible_all_ipv4_addresses"
ansible all -m setup -a "filter=ansible_architecture"
ansible all -m setup -a "filter=ansible_uptime_seconds"
ansible all -m setup -a "filter=ansible_processor*"
ansible all -m setup -a 'filter=ansible_env'

 ansible web -m setup -a 'filter=*ipv4'