0
点赞
收藏
分享

微信扫一扫

数据结构小记【Python/C++版】——B树篇

程序员伟杰 03-11 20:31 阅读 2

一、Ansible简介

什么是Ansible?

1.ansible是新出现的自动化运维工具,基于python开发,集合了从多的运维工具(puppet、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
2.ansible是基于paramiko开发的,并且基于模块化工作,它本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架,ansible不需要在远程主机上安装client/agents,因为它们是基于ssh来和远程主机通讯的。ansible目前已经被红帽官方收购,是自动化运维工具认可度最高的。
3.更加详细的资源参考官方文档,如右是Ansible的官方网站:Ansible Documentation

Ansible的特点

1.部署简单,只需要在主控端部署Ansible环境,被控端无需做任何操作;
2.默认使用SSH协议对设备进行管理;
3.有大量的常规运维操作模块,可实现日常绝大部分的操作;
4.配置简单、功能强大、扩展性强;
5.支持API以及自定义模块,可以通过Python轻松扩展;
6.通过Playbooks来定制强大的配置、状态管理;
7.轻量级、无需在客户端安装agent,更新时,只需在操作机上进行一次更新即可;
8.提供一个功能强大、操作性强的web管理界面和REST API接口——AWX平台。

Ansible的架构

Ansible:Ansible的核心程序

HostInventory:记录有Ansible管理的主机信息,包括端口、密码、IP地址等

Playbooks:“剧本”YAML格式的文件,多个任务定义在一个文件中,定义主机需要调用哪些模块来完成的功能。

CoreModules:核心模块,主要操作是通过调用核心模块来完成管理任务

CustomModules:自定义模块,完成核心模块无法完成的功能,支持多种语言。

ConnectionPlugins:连接插件,Ansible和Host通信使用

二.Ansible任务执行解析

ansible任务执行模式

  • ansible系统由控制主机被管节点的操作方式可以分为两类,即adhoc和playbook

  • ad-hoc模式(点对点模式)

    使用单个模块,支持批量执行单条命令。ad-hoc命令是一种可以快速输入的命令,而且不需要保存起来的命令。就相当于bash中的一句话shell
    
  • playbook模式(剧本模式)

    剧本模式是Ansible的主要管理方式,也是Ansible功能强大的关键所在。playbook通过多个task(任务)集合完成一类功能,比如web服务的安装部署、数据库服务的批量备份等。可以简单地把playbook理解为通过组合多条ad-hoc操作的配置文件
    

ansible执行流程

简单理解就是Ansible在运行时,首先读取ansible.cfg中的配置,根据规则获取Inventory中的管理主机列表,并行的在这些主机中执行配置的任务,最后等待执行返回结果。

ansible命令执行过程

1.加载自己的配置文件,默认为/etc/ansible/ansible.cfg;
2.查找对应的主机配置文件,找到要执行的主机或者组;
3.加载自己对应的模块文件,如command;
4.通过ansible将模块或者命令生成对应的py文件(python脚本),并且将该文件传输到远程服务器;
5.对应执行用户的家目录.ansible/tmp/xxx/xxx.py文件;
6.给文件添加执行权限;
7.执行并且返回结果;
8.删除临时的py文件, sleep 0退出

三.Ansible配置解析

ansible的安装方式

ansible安装常用两种方式,,yum安装和pip程序安装

ansible的程序结构(yum安装为例)

配置文件目录:/etc/ansible/
执行文件目录:/usr/bin/
Lib库依赖目录:/usr/lib/pyhtonX.X/site-packages/ansible/
Help文档目录:/usr/share/doc/ansible-X.X.X/
Man文档目录:/usr/share/man/man1/

ansible的配置文件查找顺序

ansible与我们其他的服务在这一点上又很大的不同,这里的配置文件查找是从多个地方找的,顺序如下:

1.检查环境变量 ANSIBLE_CONFIG 指向的路径文件(export ANSIBLE_CONFIG=/etc/ansible.cfg);
2.~/.ansible.cfg,检查当前目录下的ansible.cfg配置文件
3./etc/ansible.cfg检查etc目录的配置文件

ansible的配置文件

ansible的配置文件路径是/etc/ansible/ansible.cfg,ansible许多参数,下面我们列出一些常见的参数:

inventory = /etc/ansible/hoste    #这个参数表示资源清单inventory文件的位置
library = /usr/share/ansible    #指向存放ansible模块的目录,支持多个目录方式,只要用冒号(:)隔开就行。
fbrks = 5    #并发连接数,默认为5
sudo_user = root    #设置默认执行命令的用户
remote_port = 22    #指定连接被管理节点的端口,默认为22端口,为安全考虑,建议修改
host_key_checking = False    #设置是否检查SSH主机的密钥,值为True/False,关闭后第一次连接不会提示配置实例
timeout = 60    #设置SSH连接的超时时间,单位为秒
log_path = /var/log/ansible.cfg    #指定一个存储ansible日志的文件(默认不记录日志)

ansible的主机清单

在配置文件中,我们提到了资源清单,这个清单就是主机清单,里面保存的是一些ansible需要连接管理的主机列表。
如下是ansible主机清单的定义方式:

1.直接指明主机地址或者主机名:
#green.example.com#
#bule.example.com#
2.定义一个主机组(组名)把地址或者主机名加进去
[mysql_test]
192.168.43.101
192.168.43.102
192.168.43.103

需要注意的是,这里的组成员可以使用通配符来匹配,这样对于一些标准化管理就比较方便。我们可以根据实际情况来配置我们的主机列表,具体的操作如下:

vi /etc/ansible/hosts
[web]
10.1.1.172
10.1.1.173

四.Ansible常用命令

ansible命令集解释

                                                                                                     /usr/bin/ansible,Ansible AD-Hoc临时命令执行工具,常用于临时命令的执行
/usr/bin/ansible-doc,Ansible模块功能查看工具
/usr/bin/ansible-galaxy,下载上传优秀的代码或者Roles模块的官网平台,基于网络的
/usr/bin/ansible-playbook,Ansible定制自动化的任务集编排工具
/usr/bin/ansible-pull,Ansible远程执行命令的工具,拉取配置而非推送配置(使用较少,海量机器时使用,对运维架构能力要求高)
/usr/bin/ansible-vault,Ansible文件加密工具
/usr/bin/ansible-console,Ansible基于Linux Consoble界面可与用户交互的命令执行工具
较为常用的是/usr/bin/ansible和/usr/bin/ansible-playbook

ansible-doc命令

ansible-doc命令常用于获取模板块信息及其适用帮助,一般用法如下:

ansible-doc -l    ##获取全部模块信息
ansible-doc -s MOD_NAME    #获取指定模块的使用帮助

ansible命令

使用ansible -h来查看帮助

-a,MODULE_ARGS     #模块的参数,如果执行默认COMMAND的模块,即是命令参数,如:“date”,"pwd"。
-k,--ask-pass    #ask for SSH password。登录密码,提示输入SSH密码而不是假设基于密钥的验证
--ask-pass    #ask for SSH。su切换密码
-K,--ask-sudo-pass    #ask for sudo password。提示密码使用sudo,sudo表示提权操作
--ask-vault-pass    #ask for vault password。 假设我们设定了加密的密码,则用该选项进行访问
-B SECONDS    #后台超时时间
-C    #模拟运行环境并且进行预运行,可以进行查错测试
-c CONNECTION    #连接类型使用
-f FORKS    #并行任务数,默认为5
-i INVENTORY    #指定主机清单的路径,默认为/etc/ansible/hosts
--list-hosts    #查看有哪些主机组
-m MODULE_NAME #执行模块的名字,默认使用command模块,所以如果只执行单一命令可以不用 -m 参数
-o    #压缩输出,尝试讲所有结果在一行输出,一般针对收集工具使用
-S    #用su命令
-R SU_USER #指定su的用户,默认为root用户
-s    #用sudo的命令
-U SUDO_USERSUDO    #指定sudo到哪个用户,默认为root用户
-T TIMEOUT    #指定ssh默认超时时间,默认为10s,也可以在配置文件中修改
-u REMOTE_USER #远程用户,默认为root用户
-v    #查看详细信息,同时支持 -vvv,-vvvv可以查看更加详细的信息

ansible配置公私钥

1.上面已经提到ansible是基于ssh协议实现管理主机的,所以ansible配置公私钥的方式与ssh协议的方式相同,具体操作步骤如下:

1.生产私钥
ssh-keygen
2.向被管理主机分发私钥
ssh-copy-id root@10.1.1.172
ssh-copy-id root@10.1.1.173

五.部署ansible管理集群

实验环境

主机名IP地址安装包
ansible10.1.1.171yum安装ansible
node110.1.1.172yum安装ansible
node210.1.1.173yum安装ansible

实验步骤

1.安装ansible

[root@ansible1 ~]# hostnamectl set-hostname ansible
[root@ansible1 ~]# bash
[root@ansible ~]# yum -y install centos-release-ansible-29
[root@ansible ~]# yum clean all
[root@ansible ~]# yum -y install ansible-test.noarch
[root@ansible ~]# ansible --version
ansible 2.9.27
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Sep 10 2021, 09:13:53) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]

2.添加主机清单

[root@ansible ~]# cd /etc/ansible
[root@ansible ansible]# ls
ansible.cfg  hosts  roles
[root@ansible ansible]# vim hosts 
[web]  ##添加到最后一行
10.1.1.172
10.1.1.173

3.配置公私钥

[root@ansible ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:RgPdPb2Y0jD3iwyJ98OLD7TsIeiz1K+PjERA8DsIJV8 root@ansible
The key's randomart image is:
+---[RSA 3072]----+
|. o.E .. . . .   |
| + +   .. + + .  |
|. . o   o. * = . |
| . . o ...= + o  |
|  . o . S..* . . |
|     o + o .* .  |
|      + o =. o   |
|     +.o =.o.    |
|      +o+o+..    |
+----[SHA256]-----+
[root@ansible ~]# ssh-copy-id root@10.1.1.172
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '10.1.1.172 (10.1.1.172)' can't be established.
ECDSA key fingerprint is SHA256:Pk8wDSU/NMM+zwNM90gwxsiyeYnE3BXZhlisgxmdbVY.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@10.1.1.172's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@10.1.1.172'"
and check to make sure that only the key(s) you wanted were added.
[root@ansible ~]# ssh-copy-id root@10.1.1.173
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '10.1.1.173 (10.1.1.173)' can't be established.
ECDSA key fingerprint is SHA256:Pk8wDSU/NMM+zwNM90gwxsiyeYnE3BXZhlisgxmdbVY.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@10.1.1.173's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@10.1.1.173'"
and check to make sure that only the key(s) you wanted were added.

#注意:全程是只需要,在ansible上面操作就行

六.Ansible常用模块

主机连通性测试

使用下列命令对主机清单中的资源进行连通性测试

[root@ansible ~]# ansible web -m ping
10.1.1.172 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
10.1.1.173 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

command模块

command模块可以直接在远程主机上执行命令,并且结果返回打印出来,举例如下:

##-m 指定执行模块
##-a 指定命令执行
[root@ansible ~]# ansible web -m command -a 'date'
10.1.1.172 | CHANGED | rc=0 >>
2024年 03月 07日 星期四 17:24:40 CST
10.1.1.173 | CHANGED | rc=0 >>
2024年 03月 07日 星期四 17:24:40 CST

1.命令模块接受命令名称,后面是空格分隔的列表参数。给定的命令将在所有选定的节点上执行,它不会通过shell进行处理,比如$HOME和操作如“<”,“>”,"| “, " ; " ,” & " (需要使用 shell模块实现这些功能)。注意,command模块不支持 | 管道命令。

2.下面是command模块常用的几个命令:

chdir:在执行命令之前,先切换到该目录
execurable:切换shell来执行命令,需要使用命令的绝对路径
free_form:要执行的Linux指令,一般使用Ansible的-a参数代替
creates:一个文件名,当这个文件存在,则该命令不执行,可以用来做判断
removes:一个文件名,这个文件不存在,则该命令不执行

3.以下是这些命令的效果

##使用chdir切换到/etc/init.d目录
[root@ansible ~]# ansible web -m command -a 'chdir=/etc/init.d/ ls'
10.1.1.173 | CHANGED | rc=0 >>
functions
README
10.1.1.172 | CHANGED | rc=0 >>
functions
README
##使用removes,如果/demo/1.txt存在,就执行cat /demo/1.txt
[root@ansible ~]# ansible web -m command -a 'removes=/demo/1.txt cat /demo/1.txt'
10.1.1.172 | SUCCESS | rc=0 >>
skipped, since /demo/1.txt does not exist
10.1.1.173 | SUCCESS | rc=0 >>
skipped, since /demo/1.txt does not exist
##使用creates命令,如果/demo/1.txt存在,就不执行cat /demo/1.txt
[root@ansible ~]# ansible web -m command -a 'creates=/demo/1.txt cat /demo/1.txt'
10.1.1.172 | SUCCESS | rc=0 >>
skipped, since /demo/1.txt exists
10.1.1.173 | FAILED | rc=1 >>
cat: /demo/1.txt: 没有那个文件或目录non-zero return code

shell模块

1.shell模块可以在远程主机上调用shell解释器运行命令,支持shell的各种功能,例如管道等
2.只要是shell命令都可以在通过这个模块在远程主机里面运行

[root@ansible ~]# ansible web -m shell -a 'cat /etc/passwd|grep root'
10.1.1.172 | CHANGED | rc=0 >>
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
10.1.1.173 | CHANGED | rc=0 >>
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

copy模块

1.这个模块用于将文件复制到远程主机上,同时支持给定的内容生成文件和修改权限等
2.copy模块的相关选项如下:

src:被复制到远程主机的本地文件。可以是绝对路径,也可以是相对路径。如果路径是一个目录,则会递归复制,用法类似于“rsync”。
content:用于替换“src”,可以直接指定文件的值。
dest:必选项,将源文件复制到远程主机的绝对路径。
backup:当文件内容发生改变之后,在覆盖之前,把源文件备份,备份文件包含时间信息
directory_mode:递归设定目录的权限,默认为系统默认权限。
force:当目标主机包含该文件,但是内容不同时,设定为“yes”,表示强制覆盖;设定为“no”表示目标主机的目标位置不存在该文件才复制。默认为“yes”
others:所有的file模块中的选项可以在这里使用

3.实例如下:复制文件

[root@ansible ~]# cd /demo/
[root@ansible demo]# vim 3.txt
##将本机上的/demo目录下的3.txt文件,复制到两台主机上的/demo目录下
[root@ansible ~]# ansible web -m copy -a 'src=/demo/3.txt dest=/demo/'
10.1.1.172 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "b2ec2f04f7e81d27cf35e34196016e70ef046b2b",
    "dest": "/demo/3.txt",
    "gid": 0,
    "group": "root",
    "md5sum": "6a86ff2ac095d3b1bc7fd13bc53c2db8",
    "mode": "0644",
    "owner": "root",
    "size": 13,
    "src": "/root/.ansible/tmp/ansible-tmp-1709804378.0902443-23692-118779946648242/source",
    "state": "file",
    "uid": 0
}
10.1.1.173 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "b2ec2f04f7e81d27cf35e34196016e70ef046b2b",
    "dest": "/demo/3.txt",
    "gid": 0,
    "group": "root",
    "md5sum": "6a86ff2ac095d3b1bc7fd13bc53c2db8",
    "mode": "0644",
    "owner": "root",
    "size": 13,
    "src": "/root/.ansible/tmp/ansible-tmp-1709804378.0761616-23694-191936430403446/source",
    "state": "file",
    "uid": 0
}

4.给定内容生成文件,并且制定权限

##生成内容为“yaoyaolingxiang”名为huawei的文件,并且设置权限的777
[root@ansible ~]# ansible web -m copy -a 'content="yaoyaolingxian" dest=/demo/huawei mode=777'
10.1.1.173 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "87cbd171f81c29ce16bd3f3569f82b251814acbd",
    "dest": "/demo/huawei",
    "gid": 0,
    "group": "root",
    "md5sum": "201c0cb6ba714b04421682d404be7bd5",
    "mode": "0777",
    "owner": "root",
    "size": 14,
    "src": "/root/.ansible/tmp/ansible-tmp-1709804622.588549-23745-4580974246588/source",
    "state": "file",
    "uid": 0
}
10.1.1.172 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "87cbd171f81c29ce16bd3f3569f82b251814acbd",
    "dest": "/demo/huawei",
    "gid": 0,
    "group": "root",
    "md5sum": "201c0cb6ba714b04421682d404be7bd5",
    "mode": "0777",
    "owner": "root",
    "size": 14,
    "src": "/root/.ansible/tmp/ansible-tmp-1709804622.5597358-23743-99027571026469/source",
    "state": "file",
    "uid": 0
}
##我们在两台主机上查看文件以及权限
[root@ansible ~]# ansible web -m shell -a 'ls -l /demo/'
10.1.1.172 | CHANGED | rc=0 >>
总用量 8
drwxr-xr-x 2 root root  6 3月   7 17:32 1.txt
-rw-r--r-- 1 root root 13 3月   7 17:39 3.txt
-rwxrwxrwx 1 root root 14 3月   7 17:43 huawei
10.1.1.173 | CHANGED | rc=0 >>
总用量 8
-rw-r--r-- 1 root root 13 3月   7 17:39 3.txt
-rwxrwxrwx 1 root root 14 3月   7 17:43 huawei

5.备份源文件,并且覆盖源文件

##创建新的内容覆盖原文件,并且备份
[root@ansible ~]# ansible web -m copy -a 'content="is ok" backup=yes dest=/demo/huawei mode=777'
10.1.1.172 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup_file": "/demo/huawei.24989.2024-03-07@17:47:36~",
    "changed": true,
    "checksum": "15cbd79725f2c9e5e976c35486ddbe5fb8a0d50b",
    "dest": "/demo/huawei",
    "gid": 0,
    "group": "root",
    "md5sum": "e7e03e9c484102041d5eff8b4d1a602f",
    "mode": "0777",
    "owner": "root",
    "size": 5,
    "src": "/root/.ansible/tmp/ansible-tmp-1709804854.102129-23830-112665518371661/source",
    "state": "file",
    "uid": 0
}
10.1.1.173 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup_file": "/demo/huawei.25750.2024-03-07@17:47:36~",
    "changed": true,
    "checksum": "15cbd79725f2c9e5e976c35486ddbe5fb8a0d50b",
    "dest": "/demo/huawei",
    "gid": 0,
    "group": "root",
    "md5sum": "e7e03e9c484102041d5eff8b4d1a602f",
    "mode": "0777",
    "owner": "root",
    "size": 5,
    "src": "/root/.ansible/tmp/ansible-tmp-1709804854.273039-23832-44236201026099/source",
    "state": "file",
  ##查看被控主机的文件状态
  [root@ansible ~]# ansible web -m shell -a 'ls -l /demo/'
10.1.1.172 | CHANGED | rc=0 >>
总用量 12
drwxr-xr-x 2 root root  6 3月   7 17:32 1.txt
-rw-r--r-- 1 root root 13 3月   7 17:39 3.txt
-rwxrwxrwx 1 root root  5 3月   7 17:47 huawei
-rwxrwxrwx 1 root root 14 3月   7 17:43 huawei.24989.2024-03-07@17:47:36~
10.1.1.173 | CHANGED | rc=0 >>、
总用量 12
-rw-r--r-- 1 root root 13 3月   7 17:39 3.txt
-rwxrwxrwx 1 root root  5 3月   7 17:47 huawei
-rwxrwxrwx 1 root root 14 3月   7 17:43 huawei.25750.2024-03-07@17:47:36~

file模块

1.file模块主要用于设置文件的属性,比如创建文件、创建连接文件、删除文件等,如下为常见的命令:

force:需要两种情况下强制创建软连接,一种是源文件不存在,但是之后会建立的情况下;另外一种是目标软链接已存在,需要取消之前的软链接,然后创建新的,有两个选项:yes|no。
group:定义文件/目录的属组。后面可以加上mode:定义文件/目录的权限。
owner:定义文件/目录的属主,后面必须加上path:定义文件/目录的路径。
recurse:递归设置文件的属性,只对目录有效,后面跟上src:被链接的源文件路径,只应用于state=link的情况
dest:被链接到的路径,只应用于state=link的情况
sate:状态,有如下选项
 	 directory:如果目录不存在,就创建目录
 	 file:即使文件不存在,也不会被创建
	 link:创建软链接
 	 hard:创建硬链接
	 touch:如果文件不存在,则会创建一个新的文件,如果文件或者目录已经存在,则更新其最后修改时间
 	 absent:删除目录、文件或者取消链接文件

创建目录,实例如下:

[root@ansible ~]# ansible web -m file -a 'path=/opt/app state=directory'
10.1.1.173 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 0,
    "group": "root",
    "mode": "0755",
    "owner": "root",
    "path": "/opt/app",
    "size": 6,
    "state": "directory",
    "uid": 0
}
10.1.1.172 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 0,
    "group": "root",
    "mode": "0755",
    "owner": "root",
    "path": "/opt/app",
    "size": 6,
    "state": "directory",
    "uid": 0
}
[root@ansible ~]# ansible web -m shell -a 'ls -l /opt/'
10.1.1.173 | CHANGED | rc=0 >>
总用量 0
drwxr-xr-x 2 root root 6 3月   7 18:46 app
10.1.1.172 | CHANGED | rc=0 >>
总用量 0
drwxr-xr-x 2 root root 6 3月   7 18:46 app

创建链接文件,实例如下:

[root@ansible ~]# ansible web -m file -a 'path=/opt/abc src=/demo/3.txt state=link'
10.1.1.172 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "dest": "/opt/abc",
    "gid": 0,
    "group": "root",
    "mode": "0777",
    "owner": "root",
    "size": 11,
    "src": "/demo/3.txt",
    "state": "link",
    "uid": 0
}
10.1.1.173 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "dest": "/opt/abc",
    "gid": 0,
    "group": "root",
    "mode": "0777",
    "owner": "root",
    "size": 11,
    "src": "/demo/3.txt",
    "state": "link",
    "uid": 0
}
[root@ansible ~]# ansible web -m shell -a 'ls -l /opt'
10.1.1.172 | CHANGED | rc=0 >>
总用量 0
lrwxrwxrwx 1 root root 11 3月   7 18:58 abc -> /demo/3.txt
drwxr-xr-x 2 root root  6 3月   7 18:46 app
10.1.1.173 | CHANGED | rc=0 >>
总用量 0
lrwxrwxrwx 1 root root 11 3月   7 18:58 abc -> /demo/3.txt
drwxr-xr-x 2 root root  6 3月   7 18:46 app

删除文件如下所示:

[root@ansible ~]# ansible web -m file -a 'path=/opt/abc state=absent'
10.1.1.172 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "path": "/opt/abc",
    "state": "absent"
}
10.1.1.173 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "path": "/opt/abc",
    "state": "absent"
}
[root@ansible ~]# ansible web -m shell -a 'ls -l /opt'
10.1.1.172 | CHANGED | rc=0 >>
总用量 0
drwxr-xr-x 2 root root 6 3月   7 18:46 app
10.1.1.173 | CHANGED | rc=0 >>
总用量 0
drwxr-xr-x 2 root root 6 3月   7 18:46 app

fetch模块

1.fetch模块用于从远程某个主机获取(复制)文件到本地来

dest:用来存在文件的目录。
src:在远程拉取的文件,并且是一个file,不能是目录
[root@ansible ~]# ansible web -m fetch -a 'src=/demo/huawei dest=/mnt'
10.1.1.173 | CHANGED => {
    "changed": true,
    "checksum": "15cbd79725f2c9e5e976c35486ddbe5fb8a0d50b",
    "dest": "/mnt/10.1.1.173/demo/huawei",
    "md5sum": "e7e03e9c484102041d5eff8b4d1a602f",
    "remote_checksum": "15cbd79725f2c9e5e976c35486ddbe5fb8a0d50b",
    "remote_md5sum": null
}
10.1.1.172 | CHANGED => {
    "changed": true,
    "checksum": "15cbd79725f2c9e5e976c35486ddbe5fb8a0d50b",
    "dest": "/mnt/10.1.1.172/demo/huawei",
    "md5sum": "e7e03e9c484102041d5eff8b4d1a602f",
    "remote_checksum": "15cbd79725f2c9e5e976c35486ddbe5fb8a0d50b",
    "remote_md5sum": null
}
[root@ansible ~]# ls /mnt
10.1.1.172  10.1.1.173  hgfs

cron模块

1.cron模块用于管理crontab计划性任务的,它的语法和crontab中的语法一致

day= :日应该运行的工作( 1-31, *, */2, )
hour= :小时 ( 0-23, *, */2, )
minute= :分钟( 0-59, *, */2, )
month= :月( 1-12, *, /2, )
weekday= : 周 ( 0-6 for Sunday-Saturday,, )
job= :指明运行的命令是什么
name= :定时任务描述
reboot :任务在重启时运行,不建议使用,建议使用special_time
special_time :特殊的时间范围,参数:reboot(重启时),annually(每年),monthly(每月),weekly(每周),daily(每天),hourly(每小时)
state :指定状态,present表示添加定时任务,也是默认设置,absent表示删除定时任务
user :以哪个用户的身份执行

2.添加计划性任务如下,实例如下:

[root@ansible ~]# ansible web -m cron -a 'name="ifconfig every 5 min" minute=*/5 job="/sbin/ifconfig"'
10.1.1.173 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "envs": [],
    "jobs": [
        "ifconfig every 5 min"
    ]
}
10.1.1.172 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "envs": [],
    "jobs": [
        "ifconfig every 5 min"
    ]
}
[root@ansible ~]# ansible web -m shell -a 'crontab -l'
10.1.1.172 | CHANGED | rc=0 >>
#Ansible: ifconfig every 5 min
*/5 * * * * /sbin/ifconfig
10.1.1.173 | CHANGED | rc=0 >>
#Ansible: ifconfig every 5 min
*/5 * * * * /sbin/ifconfig

3.删除计划性任务,比如我们计划任务添加错误,则可以执行以下命令删除计划任务

##首先查看一下,已有的计划性任务
[root@ansible ~]# ansible web -m shell -a 'crontab -l'
10.1.1.172 | CHANGED | rc=0 >>
#Ansible: ifconfig every 5 min
*/5 * * * * /sbin/ifconfig
10.1.1.173 | CHANGED | rc=0 >>
#Ansible: ifconfig every 5 min
*/5 * * * * /sbin/ifconfig
##准备删除计划性任务
[root@ansible ~]# ansible web -m cron -a 'name="ifconfig every 5 min" minute=*/5 job="/sbin/ifconfig state=absent"'
10.1.1.173 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "envs": [],
    "jobs": [
        "ifconfig every 5 min"
    ]
}
10.1.1.172 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "envs": [],
    "jobs": [
        "ifconfig every 5 min"
    ]
}

yum模块

yum模块主要用于软件的安装,它的选项如下

name=  :所安装的软件包的名称
state=  :present--》安装,latest--》安装最新的,absent--》卸载软件

update_cache :强制更新yum的缓存

conf_file  :指定远程yum安装时所依赖的配置文件(安装本地已有的包)。

disable_pgp_check  :是否禁止GPG checking,只用于present 或者 latest。

disablerepo  :临时禁止使用yum库。只用于安装或者更新时。

enablerepo  :临时使用的yum库。只用于安装或者更新时。

1.如下演示安装net-tools软件包

[root@ansible ~]# ansible web -m yum -a 'name=net-tools state=present'
10.1.1.172 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "msg": "Nothing to do",
    "rc": 0,
    "results": []
}
10.1.1.173 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "msg": "Nothing to do",
    "rc": 0,
    "results": []
}
[root@ansible ~]# ansible web -m shell -a 'rpm -q net-tools'
10.1.1.173 | CHANGED | rc=0 >>
net-tools-2.0-0.52.20160912git.el8.x86_64
10.1.1.172 | CHANGED | rc=0 >>
net-tools-2.0-0.52.20160912git.el8.x86_64

service模块

1.service模块用于服务程序的管理,它的主要选项如下:

arguments:命令行提供额外的参数
enabled:设置开机自启
name:服务名称
runlevel:开机启动的级别,一般不用指定。
sleep:在重启服务的过程中,是否等待。如在服务关闭以后等待2秒在启动。(定义在剧本当中)
state:有四种状态分别为:started(启动服务),stopped(停止服务),restart(重启服务),reload(重载服务)

2.开启httpd服务并且设置开启自启

##httpd服务必须自行安装httpd软件包
[root@ansible ~]# ansible web -m yum -a 'name=httpd state=present'
10.1.1.172 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "msg": "Nothing to do",
    "rc": 0,
    "results": []
}
10.1.1.173 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "msg": "Nothing to do",
    "rc": 0,
    "results": []
}
##检查80端口有没有开启
[root@ansible ~]# ansible web -m shell -a 'netstat -natp|grep 80'
10.1.1.173 | FAILED | rc=1 >>
non-zero return code
10.1.1.172 | FAILED | rc=1 >>
non-zero return code
##开启httpd服务并且设置开机自启动
[root@ansible ~]# ansible web -m service -a 'name=httpd state=started enabled=true
此处过程省略
##查看是否开启
[root@ansible ~]# ansible web -m shell -a 'netstat -natp|grep 80'
10.1.1.173 | CHANGED | rc=0 >>
tcp6       0      0 :::80                   :::*                    LISTEN      28805/httpd         
10.1.1.172 | CHANGED | rc=0 >>
tcp6       0      0 :::80                   :::*                    LISTEN      28047/httpd  

3.通过service模块关闭httpd服务

[root@ansible ~]# ansible web -m shell -a 'netstat -natp|grep 80'
10.1.1.173 | CHANGED | rc=0 >>
tcp6       0      0 :::80                   :::*                    LISTEN      28805/httpd         
10.1.1.172 | CHANGED | rc=0 >>
tcp6       0      0 :::80                   :::*                    LISTEN      28047/httpd    
[root@ansible ~]# ansible web -m service -a 'name=httpd state=stopped'
[root@ansible ~]# ansible web -m shell -a 'netstat -natp|grep 80'
10.1.1.173 | FAILED | rc=1 >>
non-zero return code
10.1.1.172 | FAILED | rc=1 >>
non-zero return code

user模块

1.user模块主要用来管理用户账号,它的主要选项如下所示:

comment :用户的描述信息
reatehome:是否创建家目录
force:在使用state=absent时,行为于userdel -force一致
group:指定基本组
groups:指定附加组
home:指定用户家目录
move_home:如果设置为home=时,试图将用户主目录移动到指定的目录
name:指定用户名
non_unique:该选项允许改变非唯一的用户ID值
password:指定用户密码
remove:在使用state=absent时,行为是与userdel -remove一致
shell:指定默认的shell
state:设置账号状态,不指定为创建,指定值为absent表示删除
system:当创建一个用户,设置这个用户是系统用户。这个设置不能更改现有的用户
uid:指定用户的uid

2.添加用户并且指定用户的uid和密码

[root@ansible ~]# ansible web -m user -a 'name=shixiaohao uid=2222 password=123456'
[WARNING]: The input password appears not to have been hashed. The 'password' argument must be encrypted for this
module to work properly.
10.1.1.172 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 2222,
    "home": "/home/shixiaohao",
    "name": "shixiaohao",
    "password": "NOT_LOGGING_PASSWORD",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 2222
}
10.1.1.173 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 2222,
    "home": "/home/shixiaohao",
    "name": "shixiaohao",
    "password": "NOT_LOGGING_PASSWORD",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 2222
}
[root@ansible ~]# ansible web -m shell -a 'cat /etc/passwd |grep shixiaohao'
10.1.1.173 | CHANGED | rc=0 >>
shixiaohao:x:2222:2222::/home/shixiaohao:/bin/bash
10.1.1.172 | CHANGED | rc=0 >>
shixiaohao:x:2222:2222::/home/shixiaohao:/bin/bash

3.删除用户

[root@ansible ~]# ansible web -m user -a 'name=shixiaohao state=absent'
10.1.1.172 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "force": false,
    "name": "shixiaohao",
    "remove": false,
    "state": "absent"
}
10.1.1.173 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "force": false,
    "name": "shixiaohao",
    "remove": false,
    "state": "absent"
}
[root@ansible ~]# ansible web -m shell -a 'cat /etc/passwd |grep shixiaohao'
10.1.1.172 | FAILED | rc=1 >>
non-zero return code
10.1.1.173 | FAILED | rc=1 >>
non-zero return code

group模块

1.group模块主要用于添加或者删除组,常用选项如下所示:

gid= 设置组的GID号
name= 指定组的名称
state= 指定组的状态,默认为创建,设置值为absent为删除
system= 设置值为yes,表示创建为系统组

实例命令如下

#创建组
[root@ansible ~]# ansible web -m group -a 'name=shixiaohao gid=3333'
10.1.1.172 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 3333,
    "name": "shixiaohao",
    "state": "present",
    "system": false
}
10.1.1.173 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 3333,
    "name": "shixiaohao",
    "state": "present",
    "system": false
}
#查看已经创建的组
[root@ansible ~]# ansible web -m shell -a 'cat /etc/group|grep 3333'
10.1.1.172 | CHANGED | rc=0 >>
shixiaohao:x:3333:
10.1.1.173 | CHANGED | rc=0 >>
shixiaohao:x:3333:
##删除组
[root@ansible ~]# ansible web -m group -a 'name=shixiaohao state=absent'
10.1.1.173 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "name": "shixiaohao",
    "state": "absent"
}
10.1.1.172 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "name": "shixiaohao",
    "state": "absent"
}

script模块

1.script模块用于将本机的脚本在被管理端的机器上运行。该模块直接指定脚本的路径即可
现在本机写一个脚本文件

[root@ansible ~]# vim demo.sh
[root@ansible ~]# chmod +x demo.sh 
[root@ansible ~]# ls
anaconda-ks.cfg  demo.sh
[root@ansible ~]# cat demo.sh 
#!/bin/bash
df -hT
[root@a

2.运行这个脚本于被管理端

[root@ansible ~]# ansible web -m script -a '/root/demo.sh'
10.1.1.172 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 10.1.1.172 closed.\r\n",
    "stderr_lines": [
        "Shared connection to 10.1.1.172 closed."
    ],
    "stdout": "文件系统       类型      容量  已用  可用 已用% 挂载点\r\ndevtmpfs       devtmpfs  1.4G     0  1.4G    0% /dev\r\ntmpfs          tmpfs     1.4G     0  1.4G    0% /dev/shm\r\ntmpfs          tmpfs     1.4G   17M  1.4G    2% /run\r\ntmpfs          tmpfs     1.4G     0  1.4G    0% /sys/fs/cgroup\r\n/dev/sda2      xfs        94G  2.5G   91G    3% /\r\n/dev/sda5      xfs       4.7G   66M  4.6G    2% /swap\r\n/dev/sda3      xfs        47G  365M   47G    1% /data\r\n/dev/sda1      xfs       1.9G  212M  1.7G   12% /boot\r\ntmpfs          tmpfs     277M     0  277M    0% /run/user/0\r\n",
    "stdout_lines": [
        "文件系统       类型      容量  已用  可用 已用% 挂载点",
        "devtmpfs       devtmpfs  1.4G     0  1.4G    0% /dev",
        "tmpfs          tmpfs     1.4G     0  1.4G    0% /dev/shm",
        "tmpfs          tmpfs     1.4G   17M  1.4G    2% /run",
        "tmpfs          tmpfs     1.4G     0  1.4G    0% /sys/fs/cgroup",
        "/dev/sda2      xfs        94G  2.5G   91G    3% /",
        "/dev/sda5      xfs       4.7G   66M  4.6G    2% /swap",
        "/dev/sda3      xfs        47G  365M   47G    1% /data",
        "/dev/sda1      xfs       1.9G  212M  1.7G   12% /boot",
        "tmpfs          tmpfs     277M     0  277M    0% /run/user/0"
    ]
}
10.1.1.173 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 10.1.1.173 closed.\r\n",
    "stderr_lines": [
        "Shared connection to 10.1.1.173 closed."
    ],
    "stdout": "文件系统       类型      容量  已用  可用 已用% 挂载点\r\ndevtmpfs       devtmpfs  1.4G     0  1.4G    0% /dev\r\ntmpfs          tmpfs     1.4G     0  1.4G    0% /dev/shm\r\ntmpfs          tmpfs     1.4G   17M  1.4G    2% /run\r\ntmpfs          tmpfs     1.4G     0  1.4G    0% /sys/fs/cgroup\r\n/dev/sda2      xfs        94G  2.6G   91G    3% /\r\n/dev/sda3      xfs        47G  365M   47G    1% /data\r\n/dev/sda5      xfs       4.7G   66M  4.6G    2% /swap\r\n/dev/sda1      xfs       1.9G  212M  1.7G   12% /boot\r\ntmpfs          tmpfs     277M     0  277M    0% /run/user/0\r\n",
    "stdout_lines": [
        "文件系统       类型      容量  已用  可用 已用% 挂载点",
        "devtmpfs       devtmpfs  1.4G     0  1.4G    0% /dev",
        "tmpfs          tmpfs     1.4G     0  1.4G    0% /dev/shm",
        "tmpfs          tmpfs     1.4G   17M  1.4G    2% /run",
        "tmpfs          tmpfs     1.4G     0  1.4G    0% /sys/fs/cgroup",
        "/dev/sda2      xfs        94G  2.6G   91G    3% /",
        "/dev/sda3      xfs        47G  365M   47G    1% /data",
        "/dev/sda5      xfs       4.7G   66M  4.6G    2% /swap",
        "/dev/sda1      xfs       1.9G  212M  1.7G   12% /boot",
        "tmpfs          tmpfs     277M     0  277M    0% /run/user/0"
    ]
}

setup模块

1.setup模块主要用于收集信息,是通过调用facts组件来实现的,facts组件时Ansible用于采集被管理机器设备信息的一个功能。我们可以使用setup模块查看机器的所有facts信息,可以使用filter来查看指定信息。整个facts信息被包装在一个JSON格式的数据文件中,ansible_facts是最上层的值。
2.facts就是变量,内建变量。每个主机的各种信息,cpu个数,内存的大小等。会存在facts中的某个变量中,调用后返回很多对应主机的信息,在后面的操作中可以根据不同的信息来做不同的操作。比如redhat系列用yum安装,而debian系列用apt安装软件
3.查看信息实例,查看被管理主机的内存

[root@ansible ~]# ansible web -m setup -a 'filter="*mem*"'
10.1.1.172 | SUCCESS => {
    "ansible_facts": {
        "ansible_memfree_mb": 1862,
        "ansible_memory_mb": {
            "nocache": {
                "free": 2410,
                "used": 353
            },
            "real": {
                "free": 1862,
                "total": 2763,
                "used": 901
            },
            "swap": {
                "cached": 0,
                "free": 0,
                "total": 0,
                "used": 0
            }
        },
        "ansible_memtotal_mb": 2763,
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}
10.1.1.173 | SUCCESS => {
    "ansible_facts": {
        "ansible_memfree_mb": 1742,
        "ansible_memory_mb": {
            "nocache": {
                "free": 2395,
                "used": 368
            },
            "real": {
                "free": 1742,
                "total": 2763,
                "used": 1021
            },
            "swap": {
                "cached": 0,
                "free": 0,
                "total": 0,
                "used": 0
            }
        },
        "ansible_memtotal_mb": 2763,
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}

4.setup模块还有一个很好的功能就是保存我们筛选出来的信息到我们的主机上,同时,文件名是被管理主机的IP地址,方便检测那台主机出现问题。

[root@ansible ~]# ansible web -m setup -a 'filter="*mem*"' --tree /tmp/facts
10.1.1.173 | SUCCESS => {
    "ansible_facts": {
        "ansible_memfree_mb": 1736,
        "ansible_memory_mb": {
            "nocache": {
                "free": 2390,
                "used": 373
            },
            "real": {
                "free": 1736,
                "total": 2763,
                "used": 1027
            },
            "swap": {
                "cached": 0,
                "free": 0,
                "total": 0,
                "used": 0
            }
        },
        "ansible_memtotal_mb": 2763,
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}
10.1.1.172 | SUCCESS => {
    "ansible_facts": {
        "ansible_memfree_mb": 1856,
        "ansible_memory_mb": {
            "nocache": {
                "free": 2406,
                "used": 357
            },
            "real": {
                "free": 1856,
                "total": 2763,
                "used": 907
            },
            "swap": {
                "cached": 0,
                "free": 0,
                "total": 0,
                "used": 0
            }
        },
        "ansible_memtotal_mb": 2763,
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}
[root@ansible ~]# ls /tmp/facts/
10.1.1.172  10.1.1.173
举报

相关推荐

0 条评论