day03
[toc]
playbook
模块
# 在test组中的主机上,安装httpd、php、php-mysqlnd[root@control ansible]# vim pkg.yml---- name: install pkgshosts: testtasks:- name: install web pkgs # 此任务通过yum安装三个包yum:name: httpd,php,php-mysqlndstate: present# 安装多个软件包,还可以写为:---- name: install pkgshosts: testtasks:- name: install web pkgsyum:name: [httpd,php,php-mysqlnd]state: present# 安装多个软件包,还可以写为:---- name: install pkgshosts: testtasks:- name: install web pkgsyum:name:- httpd- php- php-mysqlndstate: present# 根据功能等,可以将一系列软件放到一个组中,安装软件包组,将会把很多软件一起安装上。比如gcc、java等都是开发工具,安装开发工具包组,将会把它们一起安装。[root@node1 ~]# yum grouplist # 列出所有的软件包组[root@node1 ~]# yum groupinstall "Development Tools"# 如果列出的组名为中文,可以这样进行:[root@node1 ~]# LANG=C yum grouplist# 继续编辑pkg.yml,在test组中的主机上安装Development tools组[root@control ansible]# vim pkg.yml---- name: install pkgshosts: testtasks:- name: install web pkgs # 此任务通过yum安装三个包yum:name:- httpd- php- php-mysqlndstate: present- name: install dev group # 此任务通过yum安装一组包yum:name: "@Development Tools" # @表示后面的名字是组名state: present[root@control ansible]# ansible-playbook pkg.yml# 系统升级命令[root@control ansible]# yum update# 继续编辑pkg.yml,在test组中的主机上升级所有的包到最新版本---- name: install pkgshosts: testtasks:- name: install web pkgsyum:name:- httpd- php- php-mysqlndstate: present- name: install dev groupyum:name: "@Development Tools"state: present- name: update system # 相当于yum update命令yum:name: "*" # 表示系统已经安装的所有包state: latest[root@control ansible]# ansible-playbook pkg.yml
ansible变量
facts变量
- facts翻译过来就是事实。
- facts变量是ansible自带的预定义变量,用于描述被控端软硬件信息。
- facts变量通过setup模块获得。
# 通过setup模块查看所有facts变量[root@control ansible]# ansible test -m setup
- facts变量是一个大的由
{}构成的键值对字典。在{}中,有很多层级的嵌套。可以通过参数过滤出第一个层级的内容。
# 查看所有的IPV4地址,filter是过滤的意思[root@control ansible]# ansible test -m setup -a "filter=ansible_all_ipv4_addresses"# 查看可用内存[root@control ansible]# ansible test -m setup -a "filter=ansible_memfree_mb"
常用的facts变量
- ansible_all_ipv4_addresses:所有的IPV4地址
- ansible_bios_version:BIOS版本信息
- ansible_memtotal_mb:总内存大小
- ansible_hostname:主机名
- 在playbook中使用变量
# 显示远程主机的主机名和内存大小。在ansible中,变量使用{{}}表示# debug模块用于输出信息,常用的参数是msg,用于输出指定内容[root@control ansible]# vim debug.yml---- name: display host infohosts: testtasks:- name: display hostname and memorydebug: # debug是模块,它的选项msg可以输出指定信息msg: "hostname: {{ansible_hostname}}; mem: {{ansible_memtotal_mb}} MB"[root@control ansible]# ansible-playbook debug.yml
自定义变量
- 引入变量,可以方便Playbook重用。比如装包的playbook,包名使用变量。多次执行playbook,只要改变变量名即可,不用编写新的playbook。
ansible支持10种以上的变量定义方式。常用的变量来源如下:
- inventory变量。变量来自于主机清单文件
- facts变量。
- playbook变量。变量在playbook中定义。
- 变量文件。专门创建用于保存变量的文件。推荐变量写入单独的文件。
# 使用inventory变量。[root@control ansible]# vim hosts[test]node1 iname="nb" # 主机变量定义的方法。iname是自定义名称[proxy]node2[webservers]node[3:4][database]node5[cluster:children]webserversdatabase[webservers:vars] # 组变量定义方法。:vars是固定格式iname="dachui"# 通过变量创建用户[root@control ansible]# vim var1.yml---- name: test create userhosts: testtasks:- name: create useruser:name: "{{iname}}"state: present- name: create user in webservershosts: webserverstasks:- name: create some usersuser:name: "{{iname}}"state: present[root@control ansible]# ansible-playbook var1.yml# 上述两个play也可以合并为一个,如下:[root@control ansible]# vim var1.yml---- name: test create userhosts: test,webservers # 指定执行的目标是test组和webservers组tasks:- name: create useruser:name: "{{iname}}"state: present# 在playbook中定义变量# 在test组中的主机上创建用户jack,他的密码是123456[root@control ansible]# vim user_jack.yml---- name: create userhosts: testvars: # 固定格式,用于声明变量username: "jack" # 此处引号可有可无mima: "123456" # 此处引号是需要的,表示数字字符tasks:- name: create some usersuser:name: "{{username}}" # {}出现在开头,必须有引号state: presentpassword: "{{mima|password_hash('sha512')}}"[root@control ansible]# ansible-playbook user_jack.yml# 将变量定义在文件中[root@control ansible]# vim vars.yml # 文件名自定义---yonghu: rosemima: abcd[root@control ansible]# vim user_rose.yml---- name: create userhosts: testvars_files: vars.yml # vars_files用于声明变量文件tasks:- name: create some usersuser:name: "{{yonghu}}" # 这里的变量来自于vars.ymlstate: presentpassword: "{{mima|password_hash('sha512')}}"[root@control ansible]# ansible-playbook user_rose.yml
补充模块
firewalld模块
用于配置防火墙的模块
常用选项:
- port:声明端口
- permanent:永久生效,但不会立即生效
- immediate:立即生效,临时生效
- state:enabled,放行;disabled拒绝
防火墙一般默认拒绝,明确写入允许的服务。
有一些服务有名字,有些服务没有名字。但是最终都是基于TCP或UDP的某些端口。比如http服务基于TCP80端口。服务名和端口号对应关系的说明文件是:
/etc/services配置服务器的防火墙,一般来说只要配置开放哪些服务或端口即可。没有明确开放的,都默认拒绝。
应用
- 在test组中的主机上安装并启动httpd
- 客户端访问服务器的http服务
- 在test组中的主机上安装并启动firewalld
- 客户端访问服务器的http服务
- 在test组中的主机上开放http服务
# 配置httpd服务[root@control ansible]# vim firewall.yml---- name: configure testhosts: testtasks:- name: install httpd pkg # 这里通过yum模块装httpdyum:name: httpdstate: present- name: start httpd service # 这里通过service模块启httpd服务service:name: httpdstate: startedenabled: yes[root@control ansible]# ansible-playbook firewall.yml[root@control ansible]# curl http://192.168.88.11/ # 可访问# 安装并启动firewalld[root@control ansible]# vim firewall.yml---- name: configure testhosts: testtasks:- name: install httpd pkg # 这里通过yum模块装httpdyum:name: httpdstate: present- name: start httpd service # 这里通过service模块启httpd服务service:name: httpdstate: startedenabled: yes- name: install firewalld pkg # 这里通过yum模块装firewalldyum:name: firewalldstate: present- name: start firewalld service # 这里通过service模块启firewalld服务service:name: firewalldstate: startedenabled: yes[root@control ansible]# ansible-playbook firewall.yml[root@control ansible]# curl http://192.168.88.11/ # 被拒绝curl: (7) Failed to connect to 192.168.88.11 port 80: 没有到主机的路由# 配置防火墙规则,放行http协议[root@control ansible]# vim firewall.yml---- name: configure testhosts: testtasks:- name: install httpd pkg # 这里通过yum模块装httpdyum:name: httpdstate: present- name: start httpd service # 这里通过service模块启httpd服务service:name: httpdstate: startedenabled: yes- name: install firewalld pkg # 这里通过yum模块安装firewalldyum:name: firewalldstate: present- name: start firewalld service # 这里通过service模块启service服务service:name: firewalldstate: startedenabled: yes- name: set firewalld rules # 通过firewalld模块开放80端口firewalld:port: 80/tcppermanent: yesimmediate: yesstate: enabled[root@control ansible]# ansible-playbook firewall.yml[root@control ansible]# curl http://192.168.88.11/ # 可访问
template模块
- copy模块可以上传文件,但是文件内容固定
- template模块可以上传具有特定格式的文件(如文件中包含变量)
- 当远程主机接收到文件之后,文件中的变量将会变成具体的值
- template模块上传的文件,使用的语法叫Jinja2。
常用选项:
- src:要上传的文件
- dest:目标文件路径
# 使用template模块将含有变量的文件上传到test组中的主机[root@control ansible]# vim index.j2Welcome to {{ansible_hostname}} on {{ansible_eth0.ipv4.address}}[root@control ansible]# vim templ.yml---- name: upload indexhosts: testtasks:- name: create web indextemplate:src: index.j2dest: /var/www/html/index.html[root@control ansible]# ansible-playbook templ.yml[root@control ansible]# curl http://192.168.88.11/Welcome to node1 on 192.168.88.11[root@node1 ~]# cat /var/www/html/index.htmlWelcome to node1 on 192.168.88.11
进阶语法
错误处理
- 当Playbook中包含很多任务时,当某一个任务遇到错误,它将崩溃,终止执行
# 在test组中的主机上启动mysqld服务,然后创建/tmp/service.txt# 因为目标主机上没有mysqld服务,所以它将崩溃,终止执行。即,不会创建/tmp/service.txt文件[root@control ansible]# vim myerr.yml---- name: my errorshosts: testtasks:- name: start mysqld service # 通过service模块启动mysqld服务service:name: mysqldstate: startedenabled: yes- name: touch a file # 通过file模块创建文件file:path: /tmp/service.txtstate: touch# 执行playbook,第1个任务就会失败[root@control ansible]# ansible-playbook myerr.yml# 到node1上查看,因为第2个任务没有执行,所以文件不会创建[root@node1 ~]# ls /tmp/service.txtls: cannot access '/tmp/service.txt': No such file or directory
- 可以指定某一个任务如果出现错误,则忽略它
# 编辑myerr.yml,如果myslqd服务无法启动,则忽略它[root@control ansible]# vim myerr.yml---- name: my errorshosts: testtasks:- name: start mysqld serviceservice:name: mysqldstate: startedenabled: yesignore_errors: yes # 即使这个任务失败了,也要继续执行下去- name: touch a filefile:path: /tmp/service.txtstate: touch[root@control ansible]# ansible-playbook myerr.yml[root@node1 ~]# ls /tmp/service.txt # 第2个任务已执行/tmp/service.txt
- 通过全局设置,无论哪个任务出现问题,都要忽略
[root@control ansible]# vim myerr.yml---- name: my errorshosts: testignore_errors: yestasks:- name: start mysqld serviceservice:name: mysqldstate: startedenabled: yes- name: touch a filefile:path: /tmp/mysql.txtstate: touch[root@control ansible]# ansible-playbook myerr.yml[root@node1 ~]# ls /tmp/mysql.txt/tmp/mysql.txt
触发执行任务
- 通过handlers定义触发执行的任务
- handlers中定义的任务,不是一定会执行的
- 在tasks中定义的任务,通过notify关键通知handlers中的哪个任务要执行
- 只有tasks中的任务状态是changed才会进行通知。
# 下载node1上的/etc/httpd/conf/httpd.conf[root@control ansible]# vim get_conf.yml---- name: download httpd.confhosts: testtasks:- name: get httpd.conffetch:src: /etc/httpd/conf/httpd.confdest: ./flat: yes # 直接下载文件,不要目录[root@control ansible]# ansible-playbook get_conf.yml# 修改httpd.conf的端口为变量[root@control ansible]# vim +45 httpd.conf... ...Listen {{http_port}}... ...# 修改httpd服务的端口为8000,重启httpd[root@control ansible]# vim trigger.yml---- name: configure httpdhosts: testvars:http_port: "8000" # 定义httpd.conf中的变量和值tasks:- name: upload httpd.conf # 上传httpd.conftemplate:src: ./httpd.confdest: /etc/httpd/conf/httpd.conf- name: restart httpd # 重启服务service:name: httpdstate: restarted# 第一次执行trigger.yml,上传文件和重启服务两个任务的状态都是黄色changed[root@control ansible]# ansible-playbook trigger.yml# 第二次执行trigger.yml,上传文件的任务状态是绿色的ok,重启服务任务的状态是黄色changed[root@control ansible]# ansible-playbook trigger.yml# 既然配置文件没有改变,那么服务就不应该重启# 修改Playbook,只有配置文件变化了,才重启服务[root@control ansible]# vim trigger.yml---- name: configure httpdhosts: testvars:http_port: "80"tasks:- name: upload httpd.conftemplate:src: ./httpd.confdest: /etc/httpd/conf/httpd.confnotify: restart httpd # 通知restart httpd需要执行handlers:- name: restart httpdservice:name: httpdstate: restarted# 第一次运行Playbook,因为第1个任务是黄色的changed,所以handlers中的任务也被触发执行[root@control ansible]# ansible-playbook trigger.yml# 第二次运行Playbook,因为第1个任务是绿色的OK,也就不会再触发执行其他任务了[root@control ansible]# ansible-playbook trigger.yml
when条件
只有满足某一条件时,才执行任务
常用的操作符:
- ==:相等
- !=:不等
>:大于<:小于<=:小于等于>=:大于等于
多个条件或以使用and或or进行连接
when表达式中的变量,可以不使用
{{}}
# 当test组中的主机内存大于2G的时候,才安装mariadb-server[root@control ansible]# vim when1.yml---- name: install mariadbhosts: testtasks:- name: install mariadb pkgyum:name: mariadb-serverstate: presentwhen: ansible_memtotal_mb>2048# 如果目标主机没有2GB内存,则不会安装mariadb-server[root@control ansible]# ansible-playbook when1.yml# 多条件。系统发行版是RedHat8才执行任务# /etc/motd中的内容,将会在用户登陆时显示在屏幕上[root@control ansible]# vim motd_____________< hello world >-------------\ ^__^\ (oo)\_______(__)\ )\/\||----w ||| ||[root@control ansible]# vim when2.yml---- name: when conditionhosts: testtasks:- name: modify /etc/motdcopy:dest: /etc/motdsrc: motdwhen: > # 以下三行合并成一行ansible_distribution == "RedHat"andansible_distribution_major_version == "8"[root@control ansible]# ansible-playbook when2.yml
cowsay:
```shell [root@node1 ~]# yum install -y cowsay-3.04-4.el7.noarch.rpm [root@node1 ~]# cowsay hello world # 默认是奶牛形象 [root@node1 ~]# cowsay -l # 查看可用形象 [root@node1 ~]# cowsay -f sheep hello world
[root@node1 ~]# cowsay -l > ss.txt [root@node1 ~]# vim ss.txt # 删除第1行的说明 [root@node1 ~]# for i in $(cat ss.txt)
do echo “———————$i————————“ cowsay -f $i hello sleep 3 echo “————————————————“ done ```
