ansible基础使用
2023-03-20 14:50:51 时间
本文将从无到有注明ansible的基础使用, 以三个例子进行教学
- 测试连通性
- 使用常规Linux下的命令
- 进行apt的更新
实测
准备hosts文件
首先更新ansible主目录下的hosts文件用作测试
由于在生产中, 出于安全性考虑, 不使用ssh互信进行ansible通信, 可以在配置文件中通过键值对的方式定义变量, 注明用户名与密码
[all:vars]
ansible_python_interpreter='/usr/bin/python3'
[test:vars]
ansible_ssh_user='username'
ansible_ssh_pass='password'
[test]
10.122.1.77
10.122.1.78
测试连通性
命令格式: ansible group_name/ip -m moudle_name [-a moudel_args]
[root@localhost ansible]# ansible test -m ping
10.122.1.77 | SUCCESS => {
"changed": false,
"ping": "pong"
}
10.122.1.78 | SUCCESS => {
"changed": false,
"ping": "pong"
}
使用常规命令
以确认时区为例子
[root@localhost ansible]# ansible test -m shell -a 'timedatectl| grep Time'
10.122.1.78 | CHANGED | rc=0 >>
Time zone: Etc/UTC (UTC, +0000)
10.122.1.77 | CHANGED | rc=0 >>
Time zone: Asia/Shanghai (CST, +0800)
进行apt更新
此步可以有两种方式进行完成, 以下将一一实现
1. 使用shell模块, 直接输入相关命令
[root@localhost ansible]# ansible test -m shell -a 'apt update'
10.122.1.78 | FAILED | rc=100 >>
Reading package lists...
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
W: Problem unlinking the file /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission denied)
W: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission denied)non-zero return code
- 可以看到这里由于权限问题, 未能完成apt源的更新, 此时, 可以对ansible.cfg中的become_user进行修改, 这个配置的含义是将以哪个用户的身份进行命令执行
vim /etc/ansible/ansible.cfg
become=True
become_method=sudo
become_user=root
become_ask_pass=False
# 默认注释, 解除注释即可, 意义很明显, 就不赘述了
- 对应的, 受管主机也要设置sudo免密, 或直接以sudo用户登录
- 修改后配置文件后成功
[root@localhost ansible]# ansible test -m shell -a 'apt update'
10.122.1.77 | CHANGED | rc=0 >>
Hit:1 http://cn.archive.ubuntu.com/ubuntu bionic InRelease
Hit:2 http://cn.archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:3 http://cn.archive.ubuntu.com/ubuntu bionic-backports InRelease
Hit:4 http://cn.archive.ubuntu.com/ubuntu bionic-security InRelease
Reading package lists...
Building dependency tree...
Reading state information...
12 packages can be upgraded. Run 'apt list --upgradable' to see them.
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
2. 使用ansible内置的apt模块更新
- 创建playbook所用的yml文件
[root@localhost test]# vim apt_update.yml
---
- hosts: test
tasks:
- name: update apt source
apt:
update_cache: yes
cache_valid_time: 86400
- 执行playbook [root@localhost test]# ansible-playbook apt_update.yml PLAY [test] ******************************************************************************************************************************************************************* TASK [Gathering Facts] ******************************************************************************************************************************************************** ok: [10.122.1.78] ok: [10.122.1.77] TASK [update apt source] ****************************************************************************************************************************************************** ok: [10.122.1.78] ok: [10.122.1.77] PLAY RECAP ******************************************************************************************************************************************************************** 10.122.1.77 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 10.122.1.78 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
修改配置文件以使用多个inventory
inventory = /home/test/hosts
此时/home/test/hosts下的任何inventory文件都可以被ansible识别并使用
相关文章
- 金融服务领域的大数据:即时分析
- 影响大数据、机器学习和人工智能未来发展的8个因素
- 从0开始构建一个属于你自己的PHP框架
- 如何将Hadoop集成到工作流程中?这6个优秀实践必看
- SEO公司使用大数据优化其模型的5种方法
- 关于Web Workers你需要了解的七件事
- 深入理解HTTPS原理、过程与实践
- 增强分析:数据和分析的未来
- PHP协程实现过程详解
- AI专家:大数据知识图谱——实战经验总结
- 关于PHP的错误机制总结
- 利用数据分析量化协同过滤算法的两大常见难题
- 怎么做大数据工作流调度系统?大厂架构师一语点破!
- 2019大数据处理必备的十大工具,从Linux到架构师必修
- OpenCV中的KMeans算法介绍与应用
- 教大家如果搭建一套phpstorm+wamp+xdebug调试PHP的环境
- CentOS下三种PHP拓展安装方法
- Go语言HTTP Server源码分析
- Go语言HTTP Server源码分析
- 2017年4月编程语言排行榜:Hack首次进入前五十