ansible自动化模块的学习

参考网站

关于缓存导致bug的文章,https://serverfault.com/questions/630253/ansible-stuck-on-gathering-facts

清单配置的相关网站
https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#connecting-to-hosts-behavioral-inventory-parameters

https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#inventory-basics-formats-hosts-and-groups

ansible官网
https://docs.ansible.com/ansible/latest/

ping测试连通性
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/ping_module.html#ansible-collections-ansible-builtin-ping-module

command 简单命令模块
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/command_module.html#ansible-collections-ansible-builtin-command-module

shell模块(万能模块)
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/shell_module.html#ansible-collections-ansible-builtin-shell-module

file文件操作模块
https://docs.ansible.com/ansible/latest/modules/file_module.html#file-module

script脚本模块
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/script_module.html#ansible-collections-ansible-builtin-script-module

cron定时任务模块
https://docs.ansible.com/ansible/latest/modules/cron_module.html#cron-module

group模块
https://docs.ansible.com/ansible/latest/modules/group_module.html#group-

user用户模块
https://docs.ansible.com/ansible/latest/modules/user_module.html#user-module

yum安装软件模块
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/yum_module.html#examples

mount挂载模块
https://docs.ansible.com/ansible/latest/collections/ansible/posix/mount_module.html#mount-

archive压缩模块
https://docs.ansible.com/ansible/latest/collections/community/general/archive_module.html

unarchive解压缩模块
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/unarchive_module.html#examples

anisble命令语法

ansible批量管理命令主要涉及6部分

  1. ansible主命令
  2. 指定ansible管理的主机信息,可以是主机组名、主机ip地址、或是all
  3. 调用ansible的模块参数 -m
  4. 指定用哪一个功能模块,模块的名字,如shell模块
  5. 调用对应模块的功能参数,-a
  6. 执行对应模块中的哪些功能,如hostname

ansible安装部署

在管理机61上部署ansible
1、下载ansible
yum install epel-release ansible libselinux-python -y
2、查看版本
[root@master-61 ~]#ansible --version
ansible 2.9.27
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]

清单配置

[root@master-61 ~]#tail -14 /etc/ansible/hosts 

[all:vars]
ansible_port=22999
ansible_password='222222'

[web]
172.16.1.[7:9]

[nfs]
172.16.1.31

[rsync]
172.16.1.41

清单配置参数

参数 参数类型 参数说明
ansible_host 主机地址 远程主机ip
ansible_port 主机端口 设置SSH连接端口,默认22
ansible_user 主机用户 默认SSH远程连接的用户身份
ansible_password 用户密码 指定SSH远程主机密码

ansible批量管理的两种方式

1、密钥对认证

2、密码认证

ansible命令执行方式

ad-hoc模式

Ansible的ad-hoc模式也就是ansible的命令行模式,该模式通常用来临时处理一些任务。例如

  • 临时批量查看所有被管控机器的内存、负载、磁盘
  • 临时批量分发某个特定文件
Playbook模式

Ansible的playbook模式就是针对特定的具体较大的任务,事先写好执行剧本,然后在其他机器上批量执行相同的任务,属于定制化的批量执行任务,例如

  • 一键安装Rsync
  • 一键搭建LNMP集群等

ansible-doc命令

当前的ansible支持3387个模块
[root@master-61 ~]#ansible-doc -l |wc -l
3387

产看某个模块的具体用法
[root@master-61 ~]#ansible-doc -s shell

[root@master-61 ~]#ansible-doc -s ping

ansible执行命令结果(状态颜色)

绿色:命令以用户期望的执行了,但是状态没有发生改变;

黄色:命令以用户期望的执行了,并且状态发生了改变;

紫色:警告信息,说明ansible提示你有更合适的用法;

红色:命令错误,执行失败;

蓝色: 详细的执行过程;

ping测试连通性

语法

ansible 主机组 -m 模块名  [模块参数]

查看模块解释

[root@master-61 ~]#ansible-doc -s ping
[root@master-61 ~]#ansible-doc -s ping
- name: Try to connect to host, verify a usable python and return `pong' on success
  ping:
      data:                  # Data to return for the `ping' return value. If this parameter is set to `crash', the module will cause an exception.

执行

command 简单命令模块

语法

ansible 主机组 -m command -a "需要批量执行的命令"

command模块是ansible命令基本模块

使用command模块执行远程命令,命令不得用变量($HOME)
不得出现特殊符号< 、>、|、;、&,否则无法识别,需要则使用shell模块实现
也就是无法使用复杂的linux命令

执行

查看主机名

远程查看集群负载

command的专用命令
选项参数 选项说明
chdir 在执行命令执行,通过cd命令进入指定目录
creates 定义一个文件是否存在,若不存在,则运行相应命令;存在则跳过
free_form(必须) 参数信息中可以输入任何系统命令,实现远程管理
removes 定义一个文件是否存在,如果存在,则运行相应命令;如果不存在则跳过

chdir命令

creates命令

如果目标目录已存在,就别创建了

removes命令

如果目标目录文件夹不存在则不备份,存在此文件夹,才执行命令

shell模块(万能模块)

查看模块解释

[root@master-61 ~]#ansible-doc -s shell
- name: Execute shell commands on targets
  shell:
      chdir:                 # Change into this directory before running the command.
      cmd:                   # The command to run followed by optional arguments.
      creates:               # A filename, when it already exists, this step will *not* be run.
      executable:            # Change the shell used to execute the command. This expects an absolute path to the executable.
      free_form:             # The shell module takes a free form command to run, as a string. There is no actual parameter named 'free form'. See the
                               examples on how to use this module.
      removes:               # A filename, when it does not exist, this step will *not* be run.
      stdin:                 # Set the stdin of the command directly to the specified value.
      stdin_add_newline:     # Whether to append a newline to stdin data.
      warn:                  # Whether to enable task warnings.

远程过滤ssh进程信息

使用重定向符号,创建文件

copy拷贝文件

copy模块是远程推送数据模块,只能把数据推送给远程主机节点,无法拉取数据到本地。

语法

ansible 主机组 -m copy -a "参数"
简单发送文件

发送文件且指定文件属性

创建用户

远程拷贝,修改权限

远程查看

远程拷贝并做好备份

指定数据写入到远程文件中

像这样的覆盖操作,还是添加备份参数更合适

ansible backup -m copy -a "content='rsync_backup:chaoge666' dest=/etc/rsync.passwd mode=600 backup=yes" 
复制文件夹,注意结尾斜杠
远程拷贝/etc/下的所有内容
[root@master-61 ~]#ansible web -m copy -a  "src=/etc/ dest=/opt/"


远程拷贝/etc整个目录到目标机器
[root@master-61 ~]#ansible web -m copy -a  "src=/etc dest=/opt/"

file文件操作模块

file模块作用是创建、以及设置文件目录属性。

path参数 :必须参数,用于指定要操作的文件或目录,在之前版本的ansible中,使用dest参数或者name参数指定要操作的文件或目录,为了兼容之前的版本,使用dest或name也可以。

state参数 :此参数非常灵活,此参数对应的值需要根据情况设定,比如,当我们需要在远程主机中创建一个目录的时候,我们需要使用path参数指定对应的目录路径,假设,我想要在远程主机上创建/testdir/a/b目录,那么我则需要设置path=/testdir/a/b,但是,我们无法从"/testdir/a/b"这个路径看出b是一个文件还是一个目录,ansible也同样无法单单从一个字符串就知道你要创建文件还是目录,所以,我们需要通过state参数进行说明,当我们想要创建的/testdir/a/b是一个目录时,需要将state的值设置为directory,"directory"为目录之意,当它与path结合,ansible就能知道我们要操作的目标是一个目录,同理,当我们想要操作的/testdir/a/b是一个文件时,则需要将state的值设置为touch,当我们想要创建软链接文件时,需将state设置为link,想要创建硬链接文件时,需要将state设置为hard,当我们想要删除一个文件时(删除时不用区分目标是文件、目录、还是链接),则需要将state的值设置为absent,"absent"为缺席之意,当我们想让操作的目标"缺席"时,就表示我们想要删除目标。

src参数 :当state设置为link或者hard时,表示我们想要创建一个软链或者硬链,所以,我们必须指明软链或硬链链接的哪个文件,通过src参数即可指定链接源。

force参数  :  当state=link的时候,可配合此参数强制创建链接文件,当force=yes时,表示强制创建链接文件,不过强制创建链接文件分为两种情况,情况一:当你要创建的链接文件指向的源文件并不存在时,使用此参数,可以先强制创建出链接文件。情况二:当你要创建链接文件的目录中已经存在与链接文件同名的文件时,将force设置为yes,回将同名文件覆盖为链接文件,相当于删除同名文件,创建链接文件。情况三:当你要创建链接文件的目录中已经存在与链接文件同名的文件,并且链接文件指向的源文件也不存在,这时会强制替换同名文件为链接文件。

owner参数 :用于指定被操作文件的属主,属主对应的用户必须在远程主机中存在,否则会报错。

group参数 :用于指定被操作文件的属组,属组对应的组必须在远程主机中存在,否则会报错。

mode参数:用于指定被操作文件的权限,比如,如果想要将文件权限设置为"rw-r-x---",则可以使用mode=650进行设置,或者使用mode=0650,效果也是相同的,如果你想要设置特殊权限,比如为二进制文件设置suid,则可以使用mode=4700,很方便吧。

recurse参数:当要操作的文件为目录,将recurse设置为yes,可以递归的修改目录中文件的属性。
查看命令帮助
ansible-doc -s file
远程创建文件

远程创建文件夹

远程创建文件且设定权限

远程创建软连接文件

远程修改已存在文件/文件夹的属性

script脚本模块

模块功能:把本地脚本传输到远程节点上并运行脚本

比起shell模块,script模块功能更强大,本地有一份脚本,就可以在所有机器上运行。
功能参数
选项参数 选项说明
creates 定义一个文件是否存在,若不存在,则运行相应命令;存在则跳过
free_form(必须) 参数信息中可以输入任何系统命令,实现远程管理
removes 定义一个文件是否存在,如果存在,则运行相应命令;如果不存在则跳过
远程执行脚本

管理机创建测试脚本

添加权限,方便其他人也可以执行脚本

远程执行

检查结果

查看命令执行详细过程

cron定时任务模块

cron模块用于管理定时任务的记录,编写任务

添加ntpdate定时任务

添加每5分钟执行一次和阿里云时间同步

删除定时任务

只能基于cron模块指定名字的修改

修改指定名称的定时任务

group模块

语法
模块参数    参数描述
name    创建指定的组名
gid        组的GID
state        absent,移除远程主机的组
             present,创建远端主机的组
创建xgang组,gid=8888

删除组

user用户模块

uid
用户名
用户主组
用户附加组
创建用户
删除用户
创建关于用户的公私钥
用户过期时间
用户密码过期时间
语法
模块参数 参数描述
create_home 创建家目录,设置no则不创建家目录
group 创建用户组
name 创建用户的名字
password 创建用户的密码
uid 创建用户的UID
shell 用户登录解释器
state Absent(删除用户)present(默认参数,创建)
expires 账户过期时间
创建xgang用户,uid为11111

创建用户xsu,uid、gid=6666,没有家目录,不允许登录

删除用户xsu

yum安装软件模块

yum模块明显就是一个专门用于管理软件的模块。

远程安装net-tools

远程卸载net-tools

service/systemd模块

该模块作用是针对yum包管理

service适用于centos6前的系统

systemd命令应用于centos7系统
systemd模块参数
daemon_reload:在执行任何其他操作之前运行守护进程重新加载,以确保systemd已经读取其他更改
enabled:服务是否开机自动启动yes|no。enabled和state至少要有一个被定义
masked:是否将服务设置为masked状态,被mask的服务是无法启动的
name:必选项,服务名称
no_block(2.3后新增):不要同步等待操作请求完成
state:对当前服务执行启动,停止、重启、重新加载等操作(started,stopped,restarted,reloaded)
user:使用服务的调用者运行systemctl,而不是系统的服务管理者
安装nginx

启动nginx服务

查看状态

停止服务

设置nginx开机自启

检查状态

关闭开机自启,且停止服务

检查状态

mount挂载模块

参数
mounted 挂载设备且写入fstab
present 仅写入fstab 不挂载
absent  卸载且删除fstab记录
umounted 只卸载不删除fstab记录
给web机器挂载nfs共享目录
[root@master-61 ~]#ansible web -m mount -a "src='172.16.1.31:/my_nfs_nginx' path=/usr/share/nginx/html fstype=nfs state=mounted"

检查
[root@master-61 ~]#ansible web -a "df -h"

[root@master-61 ~]#ansible web -a "cat /etc/fstab"
取消挂载,以及删除fstab记录
[root@master-61 ~]#ansible web -m mount -a "src='172.16.1.31:/my_nfs_nginx' path=/usr/share/nginx/html fstype=nfs state=absent"

验证
[root@master-61 ~]#ansible web -a "df -h"
[root@master-61 ~]#ansible web -a "cat /etc/fstab"
取消挂载,不删除fstab记录
[root@master-61 ~]#ansible web -m mount -a "src='172.16.1.31:/my_nfs_nginx' path=/usr/share/nginx/html fstype=nfs state=umounted"

archive压缩模块

支持压缩类型
bz2   gz (default)    tar    xz    zip
压缩/etc配置文件到指定路径
[root@master-61 ~]#ansible web -m archive -a "path=/etc dest=/opt/etc.tgz"
[root@master-61 ~]#ansible web -a "ls /opt -l"
[root@master-61 ~]#ansible web -a "file /opt/etc.tgz"
压缩/var/log/为zip类型到指定路径
[root@master-61 ~]#ansible web -m archive -a "path=/var/log dest=/opt/log.zip format=zip"

[root@master-61 ~]#ansible web -a "file /opt/log.zip"

unarchive解压缩模块

解压缩etc.tgz到指定目录
指定目录必须存在
[root@master-61 ~]#ansible web -m file -a "path=/opt/etc_file state=directory"


解压缩
[root@master-61 ~]#ansible web -m unarchive -a "src=/opt/etc.tgz dest=/opt/etc_file/ remote_src=yes"

查看
[root@master-61 ~]#ansible web -a "ls /opt/etc_file/etc/"
将管理机的压缩包,解压到远程机器上
1.生成etc.tgz数据
[root@master-61 ~]#cd / && tar -zcf /opt/etc.tgz etc


2.远程解压到nfs-31机器上
[root@master-61 /]#ansible nfs -m unarchive -a "src=/opt/etc.tgz  dest=/tmp/"

3.检查
[root@master-61 /]#ansible nfs -a "ls /tmp/etc/"
posted @ 2022-07-11 20:06  张开嘴  阅读(82)  评论(0)    收藏  举报