ansible 入门
1. 环境准备
三台linux 虚拟机
virtual box
镜像在这里下载 https://www.osboxes.org
下面开始创建虚机模板
注:上面为修改模块配置
到此为止,虚拟机模板已经搞到,下面开始克隆三台服务器
分别克隆三台机器 ,名称为ansible-controller ansible-node1 ansible-node2
鼠标在虚机里面,拿出来时命令:windows用ctrol+alt,mac 用command+control
到目前为止,三台机器已经创建出来了。
ansible-controller:192.168.0.108
ansible-node1:192.168.0.109
ansible-node2:192.168.0.110
远程管理工具推荐 mobaxterm
接下来改服务器的名字,改为真实角色名字
ansible的安装
参考文档:docs.ansible.com
https://docs.ansible.com/ansible/2.7/installation_guide/intro_installation.html#latest-release-via-dnf-or-yum
sudo yum -y install ansible
ansible --version # 查看版本
linux hosts配置和VScode的使用
在控制器上面增加地址解析配置
vscode的使用
新建一个文件夹
安装插件sftp
注:已经同步过来了
注:此也是同步方法
使用inventory
在ansible-code 里面创建目录 inventory 然后创建文件 inventory.ini
安装插件 ansible
关于inventory 文件
分组
intventory 正则
inventory--SSH KEY认证方式的实现
生成key
playbook--yaml格式介绍
三种文件格式
xml 格式: 现在在互联网上面用的少,繁琐,冗余数据
json 格式: 互联网上非常流行。api web 开发
yaml 格式:通过一系列缩进或短线的形式来表示数据结构或层次。
注:冒号后面一定要有空格
- 代表列表
缩进代表的一个结构
Playbook——Ansible Playbook介绍
Playbook——Ansible Playbook的演示
Playbook——Ansible的核心概念
ansible modules 链接
https://docs.ansible.com/ansible/2.7/modules/list_of_all_modules.html
使用使用Vagrant快速搭建学习环境
vrgrant网站链接
vargant 配置文件
https://github.com/udemy-course/ansible-cn/tree/master/lab-setup/vagrant-setup
注:底层是使用的virtualbox
vagrant stop 停止虚机
vagrant destory 删除虚机
vagrant status 查看虚机状态
debug 模块的使用
参考链接 https://docs.ansible.com/ansible/2.7/modules/debug_module.html#debug-module
msg 默认为 "Hello world"
var 表示变量,与msg是相冲突的
verbosity 显示debug级别,默认为0,如果大于零执行playbook,则不显示msg 信息,可以在执行ansible-playbook时添加-vvv 参数实现debug信息打印
在playbook中使用Variables
注:后面的会覆盖前面的
variable 参考,后面的优先
https://docs.ansible.com/ansible/2.7/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable
Loop循环在playbook中的使用
下面是列表分开打印
多重循环
条件判断when的使用
参考链接
https://docs.ansible.com/ansible/2.7/user_guide/playbooks_conditionals.html#the-when-statement
Group和Host变量
另一种方法
ansible.cfg的使用
ansible 运行时会找ansible的配置文件
ansible.cfg 默认会放在/etc/ansible.cfg的文件里面。如果程序当前运行目录有一个ansible.cfg的配置文件,那么他的优先给是最高的。
优先级:1.运行目录 2.环境变量ANSIBLE_CONFIG如上图 3.用户目录下面的ansible.cfg 4./etc/ansible.cfg
注:部分变量也可以放到ansible.cfg 里面
目录怎么设计
https://docs.ansible.com/ansible/2.7/user_guide/playbooks_best_practices.html
文件模块之copy/file
https://docs.ansible.com/ansible/2.7/modules/list_of_files_modules.html
https://docs.ansible.com/ansible/2.7/modules/copy_module.html#copy-module
backup 参数演示
文件模块之template
当没法copy 完全一样的文件到不同的机器(不是完全不同,只是部分参数不同)
for 循环
系统模块之ping和gather_facts
https://docs.ansible.com/ansible/2.7/modules/list_of_system_modules.html
https://docs.ansible.com/ansible/2.8/modules/gather_facts_module.html#gather-facts-module
ansible all -m gather_facts
注:默认是收集信息的
系统模块之User和Group
https://docs.ansible.com/ansible/2.8/modules/group_module.html#group-module
注:创建后再删除,实际上无意义
用户帐号操作
包管理模块之yum
https://docs.ansible.com/ansible/2.8/modules/list_of_packaging_modules.html
https://docs.ansible.com/ansible/2.8/modules/yum_module.html#yum-module
包管理模块之apt
https://docs.ansible.com/ansible/2.8/modules/apt_module.html#apt-module
包管理模块之package
https://docs.ansible.com/ansible/2.8/modules/package_module.html#package-module
包管理模块之pip
https://docs.ansible.com/ansible/2.8/modules/pip_module.html#pip-module
Net Tools模块之get_url
https://docs.ansible.com/ansible/2.8/modules/list_of_net_tools_modules.html#
https://docs.ansible.com/ansible/2.8/modules/get_url_module.html#get-url-module
Command模块
https://docs.ansible.com/ansible/2.8/modules/list_of_commands_modules.html
https://docs.ansible.com/ansible/2.8/reference_appendices/common_return_values.html#common-return-values
system模块之service
systemd 配置参考
https://docs.ansible.com/ansible/2.8/modules/systemd_module.html#systemd-module
Ansible Vault保护敏感数据
可以针对文件加密,或者只针对文件里面的一个内容加密
ansible set_fact的使用
https://docs.ansible.com/ansible/2.9/modules/set_fact_module.html
ansible-lint进行代码风格检查
https://ansible.readthedocs.io/projects/lint/installing/
类似于python的pep8
对代码风格进行检测
fetch和uri模块
https://docs.ansible.com/ansible/2.9/modules/fetch_module.html#fetch-module
fetch 是copy的反向,将文件从远程下载到本地
https://docs.ansible.com/ansible/2.9/modules/uri_module.html#uri-module
Blocks的使用
https://docs.ansible.com/ansible/2.9/user_guide/playbooks_blocks.html
正常情况下,在执行过程中只要一个task失败了,就会导致后面的所有任务都不会被执行。
我们可以将一些个task 放入到block。可以将block 看作一个区间或都一个任务段。
类似python 里面的try...except 语句。
闲聊如何参与到Ansible项目中去
ansible的配置生成
在正式使用role之前
https://docs.ansible.com/ansible/2.9/user_guide/playbooks_reuse_roles.html
可以重复使用的playbooks,类似编程语言的函数
ansible的原码是通过python实现的。
Role的最基本使用
Role内部的tasks和vars
main.yaml role 里面task执行的入口,类似编程里面的main函数
在Role中使用Handlers
https://docs.ansible.com/ansible/2.9/user_guide/playbooks_intro.html#handlers-running-operations-on-change
handlers 本身的内容就是一个完整的task
handlers的执行是有条件的,通过notify 关键词去触发的。
这个task 必须是changed 状态后才会执行notify。如果是ok 状态,不会执行notify
handlers和tasks是同级的
ansible-galaxy初始化Role
https://docs.ansible.com/ansible/2.9/cli/ansible-galaxy.html
可以帮我们自动管理、创建、查看role和collcection
collection 是从2.9这个版本引入的
从GitHub上安装Role到本地
从Ansible Galaxy安装Role到本地
类似于pypi.org
https://galaxy.ansible.com/ui/
可以用github帐号直接登陆
对于ansible 的role 名字最好使用下划线,不要使用短线的名字
Collection简介
https://docs.ansible.com/ansible/latest/collections_guide/index.html
Ansible Tower介绍
redhat公司推出的产品,不是免费的
可以做的事:用户管理,用户权限管理,执行结果的根踪、展示
可以看做ansible的hub
AWX 是一个开源的项目,源代码可以在github上面找到。redhat赞助。不建议在生产环境中使用。适合实验、学习。
AWX的安装
https://github.com/lifei188022/udemycourse_ansible/tree/master/awx
端口80
默认 用户名 admin 密码 password
在awx中 必须先有project
注:开始同步
AWX的Templates
注:有两种template,一个是job template,另一个是workflow template。我们先看job template.
AWX的credentials