ansible自动化运维工具使用详解
一、ansible简介1.ansible ansible是新出现的自动化运维工具,基于Python研发。糅合了众多老牌运维工具的优点实现了批量操作系统配置、批量程序的部署、批量运行命令等功能。仅需在管理工作站上安装ansible程序配置被管控主机的IP信息,被管控的主机无客户端。ansible应用程序存在于epel(第三方社区)源,依赖于很多python组件 参考站点:http://www.ansible.com.cn 2.ansible特性模块化设计,调用特定的模块来完成特定任务,本身是核心组件,短小精悍;基于Python语言实现,由Paramiko(python的一个可并发连接ssh主机功能库), PyYAML和Jinja2(模板化)三个关键模块实现;部署简单,agentless无客户端工具;主从模式工作;支持自定义模块功能;支持playbook剧本,连续任务按先后设置顺序完成;期望每个命令具有幂等性: 3.ansible架构 ansible core:ansible自身核心模块 host inventory:主机库,定义可管控的主机列表 connection plugins:连接插件,一般默认基于ssh协议连接 modules:core modules(自带模块)、custom modules(自定义模块) playbooks:剧本,按照所设定编排的顺序执行完成安排任务4.配置文件: (1)ansible应用程序的主配置文件:/etc/ansible/ansible.cfg (2) Host Inventory定义管控主机:/etc/ansible/hosts 遵循INI风格;中括号中的字符是组名;一个主机可同时属于多个组; 示例:# Ex 1: Ungrouped hosts, specify before any groupheaders.直接在任何组的头部前面指定,不属于任何组的主机green.example.comblue.example.com192.168.100.1192.168.100.10# Ex 2: A collection of hosts belonging to the'webservers' group;一批主机属于一个组,例如定义为'webservers'的组alpha.example.orgbeta.example.org192.168.1.100192.168.1.110 注意:默认是以root用户执行,但是基于ssh连接操作要多次输入密码,为方便可以使用基于ssh密钥方式进行认证
二、ansible应用程序命令1.ansible-doc命令:获取模块列表,及模块使用格式;ansible-doc -l:获取列表ansible-doc -smodule_name:获取指定模块的使用信息 2.ansible命令格式 ansible<host-pattern>[-f forks] [-m module_name][-a args]
<host-pattern>指明管控主机,以模式形式表示或者直接给定IP,必须事先定义在文件中;all设置所有
[-f forks]指明每批管控多少主机,默认为5个主机一批次
[-m module_name]使用何种模块管理操作,所有的操作都需要通过模块来指定
[-a args]指明模块专用参数;args一般为key=value格式注意:command模块的参数非为kv格式,而是直接给出要执行的命令即可;
注意:<host-pattern>默认读取/etc/ansible/hosts,也可以指明自定义文件路径-iPATH, --inventory=PATH:指明使用的host inventory文件路径;常用模块(module_name):1)command:默认模块,可省略。在远程主机上进行操作命令-a'COMMAND'注意:comand模块的参数非key=value格式,直接给出要执行的命令# ansible all -m command -a'ifconfig'2)user:-a 'name=state={present(创建)|absent(删除)}force=(是否强制操作删除家目录)system=uid=shell= home='# ansible all -m user -a 'name=ansible state=present'3)group:-a 'name= state={present|absent}gid=system=(系统组)'# ansible all -m group -a 'name=mygroup state=presentsystem=true'4)cron:-a'name= state=minute=hour= day=month=weekday= job='# ansible all -m cron -a 'name='Time' state=presentminute='*/5' job='/usr/sbin/ntpdate 172.168.0.1 &> /dev/null''5)ping:无参数# ansible all -m ping6)file:文件管理-a 'path=mode=owner= group= state={file|directory|link|hard|touch|absent}src=(link,链接至何处)'# ansible all -m file -a 'path=/tmp/testdirstate=directory'# ansible all -m file -a 'path=/tmp/test.txt state=touchmod=600 owner=user1'7)copy:-a 'dest=(远程主机上路径)src=(本地主机路径)content=(直接指明内容) owner=group=mode='# ansible web -m copy -a 'src=/etc/yum.repos.d/aliyun.repodest=/etc/yum.repos.d/'8)template-a'dest= src=\'#\'"content=owner= group=mode='9)yum:-a 'name=conf_file=(指明配置文件) state={present|latest|absent} enablerepo= disablerepo=' # ansible all -m yum 'name=httpd state=present' 10)service:-a 'name= state={started|stopped|restarted} enabled=(是否开机自动启动)runlevel='# ansible all -m service -a 'name=httpd state=started'11)shell:-a 'COMMAND' 运行shell命令# ansible all -m shell -a echo "123456789" |passwd --stdin user1'12)script:-a '/PATH/TO/SCRIPT'运行脚本# ansible all -m script -a '/tmp/a.sh'13)setup:获取指定主机的facts变量;
三、Playbooks剧本 1.playbook组织格式:YAML语言格式 playbooks是ansible更强大的配置管理组件,实现基于文本文件编排执行的多个任务,且多次重复执行 (1)YAML简介 YAML:YAML Ain'tMarkup Language;Yet Another Markup Language; 类似于半结构化数据,声明式配置;可读性较高的用来表达资料序列的格式,易于与脚本语言交互 官方站点:http://www.yaml.org (2)语法格式 1)任何书记结构都用缩进来标识,可以嵌套 2)每一行是一个键值数据key:value,冒号隔开。若想在一行标识需要用{ }和,分隔格式 3)列表用 - 标识
2.inventory参数:主机库ssh参数设置ansible基于ssh连接inventory中指定的远程主机时,将以此处的参数指定的属性进行;
ansible_ssh_port指定ssh端口
ansible_ssh_user指定ssh用户
ansible_ssh_pass指定ssh用户登录是认证密码,明文密码不安全
ansible_sudo_pass指明sudo时候的密码
实例:192.168.0.101 ansible_ssh_port=22ansible_ssh_user=rootansible_ssh_pass=xuding192.168.0.102注意:在/etc/ansible/hosts中直接定义连接时候的密码不安全,一般建议基于ssh的密钥认证方式实现
3.playbooks (1)核心元素 Tasks任务、Variables变量、Templates模板、Handlers处理器、Roles角色 (2)playbooks中定义任务:- name: task description 注释描述信息 module_name: module_args 声明模块:定义ansible模块参数 (3)ansible-playbook执行命令: ansible-playbook<filename.yml> ...
4.playbook--- 变量 (1)变量命名:字母、数字和下划线组成,仅能以字母开头; (2)变量种类: 1)facts:由远程主机发回的主机特有的属性信息,这些信息被保存在ansible变量中;无须声明,可直接调用;2)自定义变量: 通过命令行传递:ansible-playbooktest.yml--extra-vars "host=www user=test" 通过roles传递 3)主机变量:定义在inventory中的主机之后的变量;直接传递给单个主机的变量实例:# vim /etc/ansible/hosts中直接定义在主机之后192.168.0.101 host=mail192.168.0.102192.168.0.103 4)组变量:定义在inventory中的组上的变量(例如在默认的文件/etc/ansible/hosts上编辑)var1=valuevar2=value注意:组名要事先存在,实例如下:192.168.0.101192.168.0.102host=mail变量使用示例: # vim useradd.yml- hosts: websrvs remote_user: root vars:username: testuserpassword: xuding tasks:-name: add user user: name={{ username }} state=present-name: set passwordshell: /bin/echo {{ password }} |/usr/bin/passwd --stdin {{ username }}注释: 1) {{ }} 调用变量 2) #ansible-playbook /PATH/TO/SOME_YAML_FILE{ -eVARS|--extra-vars=VARS} 变量的重新赋值调用方法 # ansible-playbookuseradd.yml --extra-vars "username=ubuntu"
5.playbook---tasks (1)条件测试: 在某task后面添加when子句即可实现条件测试功能;when语句支持Jinja2语法; 实例:当时RedHat系列系统时候调用yum安装tasks:-name: install web server packageyum: name=httpd state=presentwhen: ansible_os_family == "RedHat" (2)迭代:item 在task中调用内置的item变量;在某task后面使用with_items语句来定义元素列表;tasks:-name: add four usersuser: name={{ item }}state=presentwith_items:-testuser1-testuser2-testuser3-testuser4 注意:迭代中,列表中的每个元素可以为字典格式; 实例:-name: add two usersuser: name={{ item.name }}state=present groups={{ item.groups }}with_items:- { name: 'testuser5', groups: 'wheel' }- { name: 'testuser6', groups: 'root' }
6.playbook--- handlers:处理器;触发器 只有其关注的条件满足时,才会被触发执行的任务;实例:配置文件发生改变触发重启服务-hosts: websrvsremote_user: roottasks:-name: install httpdyum:name=httpd state=present-name: install config filecopy: src=/root/httpd.confdest=/etc/httpd/conf/httpd.conf notify: restart httpd-name: start httpd service service: name=httpd state=startedhandlers:-name: restart httpd service: name=httpd state=restarted
7.playbook模板 templates: 用于生成文本文件(配置文件);模板文件中可使用jinja2表达式,表达式要定义在{{}},也可以简单地仅执行变量替换; roles:roles用于实现“代码复用”;roles以特定的层次型格式组织起来的playbook元素(variables,tasks, templates, handlers);可被playbook以role的名字直接进行调用; 用法:在roles/下建立子目录,并非全部都要创建;例如:/etc/ansible/roles/(在/etc/ansible/ansible.cfg定义roles目录)webserver/files/:此角色中用到的所有文件均放置于此目录中;templates/:Jinja2模板文件存放位置;tasks/:任务列表文件;可以有多个,但至少有一个叫做main.yml的文件;handlers/:处理器列表文件;可以有多个,但至少有一个叫做main.yml的文件;vars/:变量字典文件;可以有多个,但至少有一个叫做main.yml的文件;meta/:此角色的特殊设定及依赖关系;
页:
[1]