ansible使用方法深入解析

ansible使用方法深入解析

ansible主配置文件

/etc/ansible/ansible.cfg

cat /etc/ansible/ansible.cfg
[defaults]

# some basic default values...

#inventory      = /etc/ansible/hosts   #主机列表配置文件
#library        = /usr/share/my_modules/  #库文件存放目录
#remote_tmp     = ~/.ansible/tmp #临时py命令文件存放在远程主机目录
#local_tmp      = ~/.ansible/tmp #本机的临时命令执行目录
#forks          = 5   #默认并发数,同时并发5次
#poll_interval  = 15  #间隔,多长时间去拉取一次数据
#sudo_user      = root #默认sudo用户,不单单是用其他用户执行操作,如果是普通用户连接上后在被控端是没权限的,就会使用sudo权限
#ask_sudo_pass = True  #每次执行ansible命令是否询问ssh密码
#ask_pass      = True  # 提供用户口令
#remote_port    = 22   #远程主机端口22
#host_key_checking = False #检查对应服务器的host_key,建议取消注释
#log_path=/var/log/ansible.log #为方便查看日志,建议取消注释

说明
根据生产环境进行更改配置文件,一般情况使用默认配置即可!

  • 配置文件参数说明
  1. ~/.ansible/tmp(自动生成)
root@ansible-1:~#:tree .ansible/
.ansible/
├── cp
└── tmp

2 directories, 0 files

在执行ansible命令模块的时候,会转化生成一个py脚本,放到这~/.ansible/tmp临时目录中,运行完毕后会删除这个临时文件,

ansible系列命令

  • ansible系列命令

ansible ansible-pull
ansible-doc ansible-inventory ansible-console ansible-galaxy
ansible-config ansible-playbook ansible-vault ansible-connection

  • ansible-doc:显示模块帮助
ansible-doc [-l|-F|-s] [options] [-t <plugin type> ] [plugin]
-a  #显示所有模块的文档
-l, --list  #列出可以模块
-s, --snippet #显示指定模块的playbook片段
  • ansible命令语法

ansible通过ssh实现配置管理,应用部署,任务执行等功能,建议配置ansible端能基于密钥认证的方式联系各个被管理的节点

Alt text

ansible <host-pettern>  [-m module_name]  [-a args]
--version #显示版本
-m module #指定慕课,默认为command
-v #详细过程-vv,-vvvv更详细
--list-hosts #显示主机列表,可简写为--list
-k,--ask-pass #提示输入ssh连接密码,默认key验证
-K,--ask-become-pass #提示输入sudo时的口令
-C,--check #检查并不执行
-T,--timeout=TIMEOUT  #执行命令的超时时间,默认为10S
-u,--user=REMOTE_USER  #执行远程执行的用户
-b,--become #代替旧版的sudo,切换
#在使用sudo的时候需要在被控端授权sudo用户

ansible的Host-pattern(主机模式)

  • ansible的host-pattern

匹配主机的列表

  • ALL : 表示所有inventory中的所有主机
ansible all -m ping
  • 通配符 "*"
ansible "*" -m ping
ansible 192.168.1.* -m ping
ansible "*app" -m ping
  • 或关系
ansible "web:app" -m ping
ansible "10.0.0.11:114.114.114.114" -m ping
  • 逻辑与

在app组并且在server组中的主机

ansible "app:&server" -m ping
  • 逻辑非

在web组中,单不在app组中的主机
注意!此处为单引号!

ansible 'web:!app' -m ping
  • 正则表达式
ansible "web:&app" -m ping
ansible "~(web|app).*\.geek\.com" -m ping
  • 综合逻辑
ansible "web:app:&server:!add" -m ping

ansible命令执行过程

  • ansible命令执行过程
  1. 加载自己的配置文件默认/etc/ansible/ansible.cfg
  2. 记载自己对应的模块文件,如command
  3. 通过ansible将模块或命令生成对应的临时py文件,并将该文件传输至远程服务器的对应执行用户$HOME/.ansible/tmp/ansible-tmp-"随机数字"/XXXXXX.py文件
  4. 给文件加上x可执行权限
  5. 执行并返回结果
  6. 删除临时py文件,sleep 0 退出

查看详细过程用参数-v/-vvvv

  • ansible 执行状态
    绿色:表示执行成功,或者没有发生任何改变时
    黄色:表示执行命令成功后,对受控主机产生影响,发生了配置改变!
    红色:表示执行命令操作出现异常/失败
    在配置文件/etc/ansible/ansible.cfg中查看颜色定义
cat /etc/ansible/ansible.cfg
[colors]
#highlight = white
#verbose = blue
#warn = bright purple
#error = red
#debug = dark gray
#deprecate = purple
#skip = cyan
#unreachable = red
#ok = green
#changed = yellow
#diff_add = green
#diff_remove = red
#diff_lines = cyan

非常抱歉,我之前提供的代码存在错误。在 PyTorch 中,并没有直接提供离散余弦变换(DCT)的函数。对于 DCT 的实现,你可以使用 `torch.rfft` 函数结合 DCT 系数矩阵来进行计算。 下面是一个修正后的示例代码: ```python import torch import torch.nn as nn import torch.optim as optim # 定义离散余弦变换(DCT)系数矩阵 dct_matrix = torch.zeros(256, 256) for i in range(256): for j in range(256): dct_matrix[i, j] = torch.cos((2 * i + 1) * j * 3.14159 / (2 * 256)) # 定义 OMP 算法 def omp(A, y, k): m, n = A.shape x = torch.zeros(n, 1) residual = y.clone() support = [] for _ in range(k): projections = torch.abs(A.t().matmul(residual)) index = torch.argmax(projections) support.append(index) AtA_inv = torch.linalg.inv(A[:, support].t().matmul(A[:, support])) x_new = AtA_inv.matmul(A[:, support].t()).matmul(y) residual = y - A[:, support].matmul(x_new) x[support] = x_new return x # 加载原始图像 image = torch.randn(256, 256) # 压缩感知成像 measurement_matrix = torch.fft.fft(torch.eye(256), dim=0).real compressed = measurement_matrix.matmul(image.flatten().unsqueeze(1)) # 使用 OMP 进行重构 reconstructed = omp(dct_matrix, compressed, k=100) # 计算重构误差 mse = nn.MSELoss() reconstruction_error = mse(image, reconstructed.reshape(image.shape)) print("重构误差:", reconstruction_error.item()) ``` 在这个示例中,我们手动定义了 DCT 系数矩阵 `dct_matrix`,然后使用 `torch.fft.fft` 函数计算测量矩阵,并进行实部提取。接下来的步骤与之前的示例相同。 请注意,这只是一个示例,用于演示如何使用自定义的 DCT 系数矩阵进行压缩感知成像。在实际应用中,你可能需要根据具体的需求进行调整和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值