简介:
Ansible:Ansible的核心程序
Host Lnventory:记录了每一个由Ansible管理的主机信息,信息包括ssh端口,root帐号密码,ip地址等等。可以通过file来加载,可以通过CMDB加载
Playbooks:YAML格式文件,多个任务定义在一个文件中,使用时可以统一调用,“剧本”用来定义那些主机需要调用那些模块来完成的功能.
Core Modules:Ansible执行任何管理任务都不是由Ansible自己完成,而是由核心模块完成;Ansible管理主机之前,先调用core Modules中的模块,然后指明管理Host Lnventory中的主机,就可以完成管理主机。
Custom Modules:自定义模块,完成Ansible核心模块无法完成的功能,此模块支持任何语言编写。
Connection Plugins:连接插件,Ansible和Host通信使用
优点:
1.Stupied Simple ,上手简单,学习曲线平滑
2.SSH by default ,安全,无需安装客户端
3.配置简单、功能强大、扩展性强
4.支持API及自定义模块,可通过Python轻松扩展
5.通过Playbooks来定制强大的配置、状态管理
6.提供一个功能强大、操作性强的Web管理界面和REST API接口——AWX平台
7.幂等性:一种操作重复多次结果相同
安装:
1、yum install epel-release
2、yum install ansible
ansible 配置客户端(免密登陆)
第一种方法:
1.server: ssh-keygen
2.scp id_rsa.pub root@192.168.254.25:/root/.ssh/authorized_keys
第二种方法:
1.vim /etc/ansible/hosts
2.ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass=root
ansible常用命令
ansible-doc -l #查看支持的模块
ansible-doc -s MODEL_NAME #查看模块用法
ansible命令应用基础
ansible [options]
-f forks:启动并发线程数
-m model_name:要使用的模块
-a args:特有的参数
ansible all -m ping #查看client端是否正常ping通
ansible webserver -m setup #查看客户端信息
ansible webserver -m copy -a ‘src=/root/git_test/code.txt dest=/root/test’ #copy文件到cient端
ansible webserver -m user -a “name=test state=present” #创建test用户
ansible webserver -m user -a “name=test state=absent” #删除test用户
ansible webserver -m yum -a ‘name=epel-relese state=latest‘ #yum安装
ansible webserver -m service -a ‘name=httpd state=stopped enabled=no‘ #停止httpd服务
ansible webserver -m script -a ‘/tmp/test.sh‘ #运行脚本
ansible webserver -m command ‘date’ #查看时间
5.playbook
yaml介绍
yaml是一个可读性高的用来表达资料序列的格式,yaml参考了其他多种语言,包括:xml,c语言,python,perl以及电子邮件格式RFC2822等,ClarkEvans在2001年在首次发表了这种语言。
yaml的可读性好
yaml和脚本语言的交互性好
yaml使用实现语言的数据类型
yaml有一个一致的信息模型
yaml易于实现
yaml可以基于流程来处理
yaml表达能力强,扩展性好
roles介绍
什么情况下用到roles
假如我们现在有3个被管理主机,第一个要配置成httpd,第二个要配置成php服务器,第三个要配置成MySQL服务器。我们如何来定义playbook?
第一个play用到第一个主机上,用来构建httpd,第二个play用到第二个主机上,用来构建php,第三个play用到第三个主机上,用来构建MySQL。这些个play定义在playbook中比较麻烦,将来也不利于模块化调用,不利于多次调。比如说后来又加进来一个主机,这个第4个主机既是httpd服务器,又是php服务器,我们只能写第4个play,上面写上安装httpd和php。这样playbook中的代码就重复了。
课堂记录:
ansible:
hoc命令行:
是一款开源的自动化运维工具
python
paramiko #模拟ssh协议批量管理主机
jinja2 #模板语言,主要用来传递变量
yaml #相当于是一种编程语言
控制方式:
1. 免密钥:key-gen
参数形式:ansible_ssh_user=root; ansible_ssh_pass=root ansible_ssh_port=22
2. host inventory:记录着客户端的ip信息
调用ansible的三种模式:
hoc:命令行
playbooks:剧本|脚本
roles:角色
安装ansible:
yum install epel-release
yum install ansible -y
配置
[root@master ~]# vim /etc/ansible/hosts
[testhosts]
192.168.254.10 ansible_ssh_user=root ansible_ssh_pass=root ansible_ssh_port=22
192.168.254.12 ansible_ssh_user=root ansible_ssh_pass=root ansible_ssh_port=22
ansible默认并发数:5台(最高255)
ansible -f #控制并发数
线程是最小的调度单位
进程是最小的管理单元
ansible-doc -l #查看所支持的模块
ansible-doc -s MODEL_NAME #模块的具体用法和参数
ansible all --list-hosts #查看所有主机
ansible配置文件:(/etc/ansible/ansible.cfg)
#inventory = /etc/ansible/hosts 主机管理资产清单
#library = /usr/share/my_modules/ ansible操作的动作,无论是本地或者远程,都使用一小段代码来执行,这小段代码称之为模块,这个library就是用来存放这些模块的路径
#module_utils = /usr/share/my_module_utils/
#remote_tmp = ~/.ansible/tmp
#local_tmp = ~/.ansible/tmp
#forks = 5 ansible默认操作主机的并发数
#poll_interval = 15
#sudo_user = root 这是默认的执行命令的用户,也可以在playbook中重新设置这个参数
#ask_sudo_pass = True 用来控制Ansible playbook 在执行sudo之前是否询问sudo密码.默认为no
#ask_pass = True Ansible 剧本playbook 是否会自动默认弹出弹出密码.默认为no
#transport = smart
#remote_port = 22 这个指定连接被管理节点的端口,默认是22,除非设置了特殊的ssh端口
#module_lang = C 默认模块和系统之间通信的计算机语言,默认为’C’语言
#module_set_locale = False
#host_key_checking = False 这是设置是否检查ssh秘钥,可以设置为False和True
#timeout = 10 ssh连接超时时间
#log_path = /var/log/ansible.log ansible默认是不记录日志的,如果想把ansible系统输出的日志存放到文件中可以开启这个选项
#private_key_file = /path/to/file 使用ssh公钥私钥登录系统的时候,使用的秘钥路径
.......
还有更多的配置项,大家可以参考官方文档,如下: http://docs.ansible.com/ansible/latest/intro_configuration.html#private-key-file
常用模块:
ping:指定ansible server跟client的连通性
ansible testhosts -m ping
command:命令模块,默认模块,用于在远程执行命令:
ansible all -a 'date'
cron:
state:
prsent:安装
absent:移除
#ansible testhosts -m cron -a "minute=*/10 job='echo hello' state=present name=test_cron"
user:
name=:指明创建的用户的名字
#ansible testhosts -m user -a 'name=user1 uid=250 group=group250'
group:
#ansible testhosts -m group -a 'name=group250 gid=250'
copy:
src=:定义本地源文件路径
dest=:定义远程目标文件路径
#ansible testhosts -m copy -a 'src=/root/a.txt dest=/root/a.txt'
content=:取代src,表示直接用此处指定的信息生成为目标的内容
#ansible testhosts -m copy -a 'content=nihao dest=/root/a.txt'
service:指定运行状态
enabled=:是否开机自动启动,取值为true或者false
name=:服务名称
state=:状态,取值有started,stopped,restarted
shell:在远程主机上运行命令
尤其是在用到管道等功能的复杂命令
script: 将本地脚本复制到远程主机并运行
ansible testhosts -m script -a '/root/a.sh'
yum:安装程序包
name=:指定要安装的程序包,可以带上版本号
state=:present,latest表示安装,absent表示卸载
setup: 收集远程主机的facts
每个被管理的节点在接受并运行管理命令之前,会将自己主机相关信息,如操作系统版本,ip地址等报告给远程ansible主机
playbooks:
如果用模块形式一般有幂等性,如果用shell或者command没有幂等性
playbooks相当于是shell脚本,可以把要执行的任务写到文件当中,一次执行,方便调用
tasks:一个task相当于是一个play
varibles: 变量,一定定义,多处调用
template:模板,可以区分不同主机的特点
handlers:触发器,依赖于前一个任务,前一个任务如果执行改变,那么就会触发handlers
定义playbook任务:
- hosts: testhosts
remote_user: root
tasks:
- name: copy httpd.conf
copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
- name: restart httpd
service: name=httpd state=restarted
打标签:
-t:执行指定tag标签任务
--skip-tags:执行--skip-tags以外的任务
执行:
ansible-playbook xxx.yaml -t TAG
ansible-playbook xxx.yaml --skip-tags TAG
定义带tag标签的yaml文件:
- hosts: testhosts
remote_user: root
tasks:
- name: "touch file"
shell: echo {{ ansible_all_ipv4_addresses }} > a.txt
tags:
- tag3
- name: "date"
shell: date >> a.txt
tags:
- tag1
- tag2
include:如果有多个任务要执行,写在一个yaml文件里就显得太臃肿了,而且阅读性也比较低,这个时候可以用include关键字把多个任务联合到1个yaml文件里,方便调用
例子:比如有2个文件,a.yaml和b.yaml,那么我们可以把a.yaml,b.yaml联合到一个exec.yaml文件中执行
a.yaml的内容如下:
- name: "touch file"
shell: echo 'hello1' >> a.txt
b.yaml的内容如下:
- name: "touch file"
shell: echo 'hello2' >> a.txt
exec.yaml的内容如下:
- hosts: testhosts
remote_user: root
tasks:
- include_tasks: 1.yaml
- include_tasks: 2.yaml
定义变量:
在yaml文件当中传入模板变量
{{变量名}}
第一种:
vars:
- file: httpd.conf
第二种:
vim /etc/ansible/hosts
[testhosts:vars]
file=httpd.conf
packages=tree
第三种
执行playbook文件时候给与变量 --extra-vars
ansible-playbook test.yaml --extra-vars "touch_file=test.txt"
注册变量:
register注册变量:把date命令输出的结果赋予给date_output
- hosts: 192.168.254.10
remote_user: root
tasks:
- name: get date
command: date
register: date_output
- name: echo date_output
shell: "echo {{date_output.stdout}}>/tmp/a.txt"
when语句:
when条件语句:可以根据setup显示出客户端信息为依据来判断
- hosts: 192.168.254.12
remote_user: root
tasks:
- name: echo date_output
shell: "touch /tmp/a.txt"
when: ansible_distribution=='CentOS' and ansible_distribution_major_version=='8'
异常处理:
ignore_errors:如果任务出错,直接跳过,不会影响其他任务
- hosts: 192.168.254.12
remote_user: root
tasks:
- name: add several user
command: touch1 a.txt
ignore_errors: yes
循环语句:
第一种:
{{ item }}:循环创建
- hosts: 192.168.254.12
remote_user: root
tasks:
- name: add many users
user: name={{ item }} state=present
with_items:
- user1
- user2
- user3
- user4
第二种:
- hosts: 192.168.254.12
remote_user: root
tasks:
- name: add several user
user: name={{item.name}} state=present groups={{item.groups}}
with_items:
- { name: 'testuser1', groups: 'wheel'}
- { name: 'testuser2', groups: 'root'}
触发器:
handlers:如果执行的任务被改变那么会触发handlers的任务
- hosts: testhosts
remote_user: root
tasks:
- name: copy httpd.conf
copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
- restarted httpd service
handlers:
- name: restarted httpd service
service: name=httpd state=restarted
模板拷贝:
1.copy模块替换成template
2.vim httpd.conf编辑要拷贝的文件,把不同的地方定义成变量形式{{变量名}}
3.vim /etc/ansible/hosts 在主机后面定义变量的值:变量名=变量值
template,用来区分不同客户端上的特性
- hosts: testhosts
remote_user: root
tasks:
- name: copy httpd.conf
template: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
- restarted httpd service
handlers:
- name: restarted httpd service
service: name=httpd state=restarted
roles:
roles的作用是减少代码的复写
[root@localhost ~]# tree playbooks/
playbooks/
├── roles
│ ├── dbservers
│ │ ├── files
│ │ │ └── httpd.conf
│ │ ├── handlers
│ │ │ └── main.yaml
│ │ ├── tasks
│ │ │ └── main.yaml
│ │ ├── templates
│ │ │ └── httpd.conf
│ │ └── vars
│ │ └── main.yaml
│ └── webservers
│ ├── files
│ ├── handlers
│ ├── tasks
│ ├── templates
│ └── vars
└── site.yaml
#vim playbooks/roles/dbservers/tasks/main.yaml
- name: create {{ file1 }}
shell: touch {{ file1 }}
- name: create {{ file2 }}
shell: touch {{ file2 }}
- name: copy httpd.conf
template: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd
#vim playbooks/roles/dbservers/handlers/main.yaml
- name: restart httpd
service: name=httpd state=restarted
#vim playbooks/roles/dbservers/vars/main.yaml
file1: file1.txt
file2: file2.txt
file3: file3.txt
#files/ 用copy模块来拷贝文件,src直接指定文件名字,不需要绝对路径
#templates/ 用template模块来拷贝文件,src直接指定文件名字,不需要绝对路径
个人总结:我在以后的工作中,首先用cobbler批量装机,然后在使用ansible装服务。