ansible

image

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.Ansible优点:

  • Stupied Simple ,上手简单,学习曲线平滑
  • SSH by default ,安全,无需安装客户端
  • 配置简单、功能强大、扩展性强
  • 支持API及自定义模块,可通过Python轻松扩展
  • 通过Playbooks来定制强大的配置、状态管理
  • 提供一个功能强大、操作性强的Web管理界面和REST API接口——AWX平台
  • 幂等性:一种操作重复多次结果相同

2.ansible安装

  1. yum install epel-release
  2. yum install ansible

3.ansible配置客户端(无密码登录)

第一种方法:

  1. server: ssh-keygen
  2. scp .ssh/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

4.ansible常用命令

  • ansible-doc -l #查看支持的模块
  • ansible-doc -s MODEL_NAME #查看模块用法
  • ansible命令应用基础
  1. 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

  • tasks
  • variables
  • templates
  • handlers
  • roles

yaml介绍

yaml是一个可读性高的用来表达资料序列的格式,yaml参考了其他多种语言,包括:xml,c语言,python,perl以及电子邮件格式RFC2822等,ClarkEvans在2001年在首次发表了这种语言。

  • yaml的可读性好
  • yaml和脚本语言的交互性好
  • yaml使用实现语言的数据类型
  • yaml有一个一致的信息模型
  • yaml易于实现
  • yaml可以基于流程来处理
  • yaml表达能力强,扩展性好

示例1:基础

image

示例2:变量

image

示例3:迭代

image

示例4:触发器notify

image

示例5:模板templates

image

roles介绍

什么情况下用到roles

假如我们现在有3个被管理主机,第一个要配置成httpd,第二个要配置成php服务器,第三个要配置成MySQL服务器。我们如何来定义playbook?

第一个play用到第一个主机上,用来构建httpd,第二个play用到第二个主机上,用来构建php,第三个play用到第三个主机上,用来构建MySQL。这些个play定义在playbook中比较麻烦,将来也不利于模块化调用,不利于多次调。比如说后来又加进来一个主机,这个第4个主机既是httpd服务器,又是php服务器,我们只能写第4个play,上面写上安装httpd和php。这样playbook中的代码就重复了。

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
site.yml
- hosts: webservers
  remote_user: root
  roles:
  - websrvs
  - dbsrvs

将文件拷贝到files目录下
cp /etc/httpd/conf/httpd.conf /root/ansible_playbooks/roles/websrvs/files/

ansible的相关命令及使用:

playbooks:
如果用模块形式一般有幂等性,如果用shell或者command没有幂等性
playbooks相当于是shell脚本,可以把要执行的任务写到文件当中,一次执行,方便调用
tasks:一个task相当于是一个play
varibles: 变量,一定定义,多处调用
template:模板,可以区分不同主机的特点
handlers:触发器,依赖于前一个任务,前一个任务如果执行改变,那么就会触发handlers

定义playbook任务:

              - hosts: testhosts
			  remote_user: root
			  vars:
			  - file: httpd.conf
			  tasks:
			  - name: copy httpd.conf
				copy: src=/root/{{ file }} dest=/etc/httpd/conf/{{ file }}
			  - name: restart httpd
				service: name=httpd state=restarted

定义变量:
在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"

1、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'

2、异常处理:
ignore_errors:如果任务出错,直接跳过,不会影响其他任务

			- hosts: 192.168.254.12
			  remote_user: root
			  tasks:
			  - name: add several user
				command: touch1 a.txt
				ignore_errors: yes

3、循环语句:
第一种:{{ 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'}

4、触发器:
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
					rolse:

5、模板拷贝:
template,用来区分不同客户端上的特性
1.copy模块替换成template
2.vim httpd.conf编辑要拷贝的文件,把不同的地方定义成变量形式{{变量名}}
3.vim /etc/ansible/hosts 在主机后面定义变量的值:变量名=变量值

				- 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
posted @ 2019-07-03 15:19  BeiteJohn  阅读(114)  评论(0编辑  收藏  举报