Ansible的脚本—playbook剧本

本文介绍了Ansible的playbooks,包括Tasks、Variables、Templates、Handlers和Roles等组成部分,并提供了实际操作示例,如在远程主机上修改配置文件并复制到控制节点,以及如何添加主机和管理多台主机。
摘要由CSDN通过智能技术生成

一.playbooks 概述以及实例操作

1、playbooks 的组成

playbooks 本身由以下各部分组成

(1)Tasks:任务,即通过 task 调用 ansible 的模板将多个操作组织在一个 playbook 中运行

(2)Variables:变量

(3)Templates:模板

(4)Handlers:处理器,当changed状态条件满足时,(notify)触发执行的操作

(5)Roles:角色

2、操作示例一:

2.1  编写yaml文件也就是playbook

vim test1.yaml
---                                                                    #yaml文件以---开头,以表明这是一个yaml文件,可省略
   - name: first play                                                     #定义一个play的名称,可省略
     gather_facts: false                                                    #设置不进行facts信息收集,这可以加快执行速度,可省略
     hosts: webservers                                                      #指定要执行任务的被管理主机组,如多个主机组用冒号分隔
     remote_user: root                                                      #指定被管理主机上执行任务的用户
     tasks:                                                                 #定义任务列表,任务列表中的各任务按次序逐个在hosts中指定的主机上执行
     - name: test connection                                                # 自定义任务名称
       ping:                                                                  #使用 module: [options] 格式来定义一个任务
     - name: disable selinux
       command: '/sbin/setenforce 0'                                           #command模块和shell模块无需使用key=value格式
       ignore_errors: True                                                     #如执行命令的返回值不为0,就会报错,tasks停止,可使用ignore_errors忽略失败的任务
     - name: disable firewalld
       service: name=firewalld state=stopped                                   #使用 module: options 格式来定义任务,option使用key=value格式
     - name: install httpd
       yum: name=httpd state=latest
     - name: install configuration file for httpd
       copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf               #这里需要一个事先准备好的/opt/httpd.conf文件
       notify: "restart httpd"                                                 #如以上操作后为changed的状态时,会通过notify指定的名称触发对应名称的handlers操作
     - name: start httpd service
       service: enabled=true name=httpd state=started
   handlers:                                                               #handlers中定义的就是任务,此处handlers中的任务使用的是service模块
     - name: restart httpd                                                   #notify和handlers中任务的名称必须一致
       service: name=httpd state=restarted
//运行playbook
ansible-playbook test1.yaml
//补充参数:
-k(–ask-pass):用来交互输入ssh密码
-K(-ask-become-pass):用来交互输入sudo密码
-u:指定用户
ansible-playbook test1.yaml --syntax-check    #检查yaml文件的语法是否正确
ansible-playbook test1.yaml --list-task       #检查tasks任务
ansible-playbook test1.yaml --list-hosts      #检查生效的主机
ansible-playbook test1.yaml --start-at-task='install httpd'     #指定从某个task开始运行

 

 在另一台主机修改httpd.conf并复制到管理端

 

 

 

 

 

 加一台主机

 

 

 

 

//定义、引用变量
- name: second play
  hosts: dbservers
  remote_user: root
  vars:                 #定义变量
   - groupname: mysql   #格式为 key: value
   - username: nginx
  tasks:
   - name: create group
     group: name={{groupname}} system=yes gid=306    #使用 {{key}} 引用变量的值
   - name: create user
     user: name={{username}} uid=306 group={{groupname}}
   - name: copy file
     copy: content="{{ansible_default_ipv4}}" dest=/opt/vars.txt    #在setup模块中可以获取facts变量信息
 
 
ansible-playbook test1.yaml -e "username=nginx"     #在命令行里定义变量

 

 

 

 

 

 

 

//指定远程主机sudo切换用户
---
- hosts: dbservers
  remote_user: zhangsan           
  become: yes                    #2.6版本以后的参数,之前是sudo,意思为切换用户运行
  become_user: root              #指定sudo用户为root
执行playbook时:ansible-playbook test1.yml -K <密码>
 
 
//when条件判断
在Ansible中,提供的唯一一个通用的条件判断是when指令,当when指令的值为true时,则该任务执行,否则不执行该任务。
 
//when一个比较常见的应用场景是实现跳过某个主机不执行任务或者只有满足条件的主机执行任务
vim test2.yaml
---
- hosts: all
  remote_user: root
  tasks:
   - name: shutdown host
     command: /sbin/shutdown -r now
     when: ansible_default_ipv4.address == "192.168.30.18"      #when指令中的变量名不需要手动加上 {{}}
或
     when: inventory_hostname == "<主机名>"
     
ansible-playbook test2.yaml

 

 或者

 

 

//迭代
Ansible提供了很多种循环结构,一般都命名为with_items,作用等同于 loop 循环。
vim test3.yaml
---
- name: play1
  hosts: dbservers
  gather_facts: false
  tasks:
    - name: create directories
      file:
        path: "{{item}}"
        state: directory
      with_items:          #等同于 loop:
        - /tmp/test1
        - /tmp/test2
    - name: add users
      user: name={{item.name}} state=present groups={{item.groups}}
      with_items:
        - name: test1
          groups: wheel
        - name: test2
          groups: root
或
      with_items:
        - {name:'test1', groups:'wheel'}
        - {name:'test2', groups:'root'}
 
ansible-playbook test3.yaml

 

文件形成创建 

 

 

 

 多个play

 

 

 

 

 

//Templates 模块
Jinja是基于Python的模板引擎。Template类是Jinja的一个重要组件,可以看作是一个编译过的模板文件,用来产生目标文本,传递Python的变量<br>给模板去替换模板中的标记。
 
1.先准备一个以 .j2 为后缀的 template 模板文件,设置引用的变量
cp /etc/httpd/conf/httpd.conf /opt/httpd.conf.j2
 
vim /opt/httpd.conf.j2
Listen {{http_port}}                #42行,修改
ServerName {{server_name}}          #95行,修改
DocumentRoot "{{root_dir}}"          #119行,修改
 
2.修改主机清单文件,使用主机变量定义一个变量名相同,而值不同的变量
vim /etc/ansible/hosts      
[webservers]
192.168.30.18 http_port=192.168.30.18:8888 server_name=www.11.com:80 root_dir=/opt/aaa
 
[dbservers]
192.168.30.19 http_port=192.168.30.19:9999 server_name=www.33.com:80 root_dir=/opt/bbb
 
3.编写 playbook
vim apache.yaml
---
- hosts: all
  remote_user: root
  vars:
    - package: httpd
    - service: httpd
  tasks:
    - name: install httpd package
      yum: name={{package}} state=latest
    - name: install configure file
      template: src=/opt/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf     #使用template模板
      notify:
        - restart httpd
    - name: create root dir
      file: path=/etc/httpd/htdocs state=directory
    - name: start httpd server
      service: name={{service}} enabled=true state=started
  handlers:
    - name: restart httpd
      service: name={{service}} state=restarted
 
ansible-playbook apache.yaml

 

 

 

 

 

 

 

 

 

 

 

//tags 模块
可以在一个playbook中为某个或某些任务定义“标签”,在执行此playbook时通过ansible-playbook命令使用--tags选项能实现仅运行指定的tasks。
playbook还提供了一个特殊的tags为always。作用就是当使用always当tags的task时,无论执行哪一个tags时,定义有always的tags都会执行。
 
vim webhosts.yaml
---
- hosts: webservers
  remote_user: root
  tasks:
    - name: Copy hosts file
      copy: src=/etc/hosts dest=/opt/hosts
      tags:
      - only     #可自定义
    - name: touch file
      file: path=/opt/testhost state=touch
      tags:
      - always    #表示始终要运行的代码
 
ansible-playbook webhosts.yaml --tags="only"
 
vim dbhosts.yaml
---
- hosts: dbservers
  remote_user: root
  tasks:
    - name: Copy hosts file
      copy: src=/etc/hosts dest=/opt/hosts
      tags:
        - only
    - name: touch file
      file: path=/opt/testhost state=touch
 
 
ansible-playbook dbhosts.yaml --tags="gxs"
//分别去两台被管理主机上去查看文件创建情况

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值