Ansible---Playbook基本案例

栏目: 服务器 · 发布时间: 7年前

内容简介:cp /etc/httpd/conf/httpd.conf /opt/httpd.conf.j2 复制模板,注意文件格式为j2结尾vim /opt/httpd.conf.j2执行命令:

PlayBook介绍

  • playbook是由一个或者多个play组成的列表,主要功能是将task定义好的角色归并为一组进行统一管理。
  • playbooks本身组成部分有如下几份:
1、tasks:任务,即调用模块完成的操作
2、variables:变量
3、templates:模板
4、handlers:处理器,当条件满足时执行操作,通常前面使用notify声明。
5、roles:角色,分门别类管理playbook的一种方式,后面将详细介绍roles的用法。

playbook--yaml文件格式内容

  • 编写yaml文件,注意不同层级之间严格的格式要求。
vim /opt/test.yaml
- hosts: webserver              //定义的主机组,即应用的主机
  vars:                        //定义变量
    http_port: 80              //此处自定义变量中变量写在yaml文件,也可写在ansible/hosts文件中应用主机后
    max_clients: 200
  remote_user: root      //远程用户为root
  tasks:                               //执行的任务
  - name: ensure apache is at the latest version   //下面即将执行的步骤名称
    yum: pkg=httpd state=latest
  - name: write the apache config file   //下面即将执行的步骤名称
    template: src=/srv/httpd.j2 dest=/etc/httpd.conf    //模板文件,src为路径为控制端,dest为被控制端
    notify:          //声明需要执行的操作
    - restart apache  //操作名称
  - name: ensure apache is running
    service: name=httpd state=started
  handlers:                       //处理器,处理名称需要对应notify的名称,才能自动执行
    - name: restart apache
      service: name=httpd state=restarted

playbook基本命令格式

ansible-playbook test.yaml --syntax-check    #检查yaml文件的语法是否正确
ansible-playbook test.yaml --list-task       #检查tasks任务
ansible-playbook test.yaml --list-hosts      #检查生效的主机
ansible-playbook test.yaml --start-at-task='Copy Nginx.conf'     #指定从某个task开始运行

hosts和users介绍

---                             #表示该文件是YAML文件,非必须
- hosts: webserver               #指定主机组,可以是一个或多个组。
  remote_user: root                #指定远程主机执行的用户名

还可以为每个任务定义远程执行用户:
---
- hosts: mysql
  remote_user: root             
  tasks:
    - name: test connection
      ping:
      remote_user: mysql          #指定远程主机执行tasks的运行用户为mysql
  • 执行playbook:
ansible-playbook ping.yaml 
  • 指定远程主机sudo切换用户:
---
- hosts: mysql
  remote_user: root            
  become: yes                  #2.6版本以后的参数,之前是sudo,意思为切换用户运行
  become_user: mysql          #指定sudo用户为mysql

tasks列表和action

  • 1.Play的主体部分是task列表,task列表中的各任务按次序逐个在hosts中指定的主机上执行,即在所有主机上完成第一个任务后再开始第二个任务。
    在运行playbook时(从上到下执行),如果一个host执行task失败,整个tasks都会回滚,请修正playbook 中的错误,然后重新执行即可。
    Task的目的是使用指定的参数执行模块,而在模块参数中可以使用变量,模块执行时幂等的,这意味着多次执行是安全的,因为其结果一致。
  • 2.每一个task必须有一个名称name,这样在运行playbook时,从其输出的任务执行信息中可以很好的辨别出是属于哪一个task的。如果没有定义name,‘action’的值将会用作输出信息中标记特定的task,也就是会报错。
  • 3.定义一个task,常见的格式:”module: options” 例如:yum: name=httpd
  • 4.ansible的自带模块中,command模块和 shell 模块无需使用key=value格式

Handslers介绍

  • Handlers也是一些task的列表,和一般的task并没有什么区别。是由通知者进行的notify,如果没有被notify,则Handlers不会执行,假如被notify了,则Handlers被执行,不管有多少个通知者进行了notify,等到play中的所有task执行完成之后,handlers也只会被执行一次。

  • 示例:
- hosts: webserver
  remote_user: root
  tasks:
   - name: install httpd package
     yum: name=httpd state=latest
   - name: install configuration file for httpd
     copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf
     notify:            //声明后面Handlers的名称
      -restart httpd    //注意该名称为notify的子集,需要靠空格隔开
   - name: start httpd service
     service: enabled=true name=httpd state=started
  handlers:
   - name: restart httpd
     service: name=httpd state=restarted

也可以引入变量
    ---
- hosts: webserver
  remote_user: root
  vars:               //引入变量
  - package: httpd    //变量名称
  - service: httpd
  tasks:
   - name: install httpd package
     yum: name={{package}} state=latest    //引入变量方式
   - name: install configuration file for httpd
     copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf
     notify:
      -restart httpd
   - name: start httpd service
     service: enabled=true name={{service}} state=started
  handlers:
   - name: restart httpd     //handlers执行的操作必须是notify中声明的操作名称,若此处执行的操作名称notify中没有声明,那么handlers不会执行
     service: name={{service}} state=restarted
  • 在引入变量过程中,若针对一个组里不同主机需要引入的变量值不相同时,比如IP地址等,则需要在hosts文件中,在不同主机后面跟上变量名称与值,yaml文件中执行操作时引入变量名称即可。

  • 引用主机变量
vim /etc/ansible/hosts
[webserver]
192.168.144.110 httpd_port="192.168.144.110:80" server_name="www.yun01.com:80"
192.168.144.111 httpd_port="192.168.144.111:80" server_name="www.yun02.com:80"

Templates用法

  • Jinja是基于 Python 的模板引擎。template类是jinja的另一个重要组件,可以看做是编译过的模板文件,用来生产目标文件,传递Python的变量给模板去替换模板中的标记。

  • 制作模板文件

cp /etc/httpd/conf/httpd.conf /opt/httpd.conf.j2 复制模板,注意文件格式为j2结尾

vim /opt/httpd.conf.j2

  • 引入变量至模板文件中,后面为自定义变量复制,涉及到不同主机不同变量值得需要在hosts文件中主机后设定,相同变量值可直接在yaml文件中设定。
Listen {{httpd_port}}          //为需要修改的部分设定变量
...

ServerName {{server_name}}
  • 在Apache配置文件中,如上修改部分为每台主机独立变量值,需要在hosts文件中指定。
vim /etc/ansible/hosts
[webserver]
192.168.144.110 httpd_port="192.168.144.110:80" server_name="www.yun01.com:80"
192.168.144.111 httpd_port="192.168.144.111:80" server_name="www.yun02.com:80"
  • 编写playbook执行yaml文件
vim /opt/httpd.yaml
- hosts: webserver    //针对webserver组执行
  vars:
    package: httpd   //变量名称
    server: httpd
  tasks:
    - name: install httpd
      yum: name={{package}} state=latest
    - name: write the apache config file
      template: src=/opt/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf      //注意模板文件位置
      notify:
        restart httpd
    - name: start httpd
      service: name={{server}} enabled=true state=started
  handlers:
    - name: restart httpd
      service: name={{server}} state=restarted

条件测试

  • 如果需要根据变量、facts(setup)或此前任务的执行结果来作为某task执行与否的前提时要用到条件测试,在Playbook中条件测试使用when子句。
  • 在task后添加when子句即可使用条件测试:when子句支持jinjia2表达式或语法,例如:
vi when.yml
---
- hosts: mysql
  remote_user: root
  tasks:
    - name: "shutdown CentOS"
      command: /sbin/shutdown -h now
      when: ansible_distribution == "CentOS"
  • 多条件判断
vi when.yml
---
- hosts: mysql
  remote_user: root
  tasks:
    - name: "shut down CentOS 7 systems"
      command: /sbin/shutdown -r now
      when:
        - ansible_distribution == "CentOS"
        - ansible_distribution_major_version == "7"
  • 组条件判断
vi when.yml
---
- hosts: mysql
  remote_user: root
  tasks:
    - name: "shut down CentOS 6 and Debian 7 systems"
      command: /sbin/shutdown -t now
      when: (ansible_distribution == "CentOS" and ansible_distribution_major_version == "6") or
            (ansible_distribution == "Debian" and ansible_distribution_major_version == "7")
  • 自定义变量进行条件测试
vi when.yml
---
- hosts: all
  vars:
    exist: "True"
  tasks:
  - name: creaet file
    command:  touch /tmp/test.txt
    when: exist | match("True")

  - name: delete file
    command:  rm -rf /tmp/test.txt
    when: exist | match("False")

迭代

  • 当有需要重复性执行的任务时,可以使用迭代机制。其使用格式为将需要迭代的内容定义为item变量引用,并通过with_items语句指明迭代的元素列表即可。例如:
---
- hosts: webserver
  remote_user: root
  tasks:
    - name: "Install Packages"
      yum: name={{ item }} state=latest
      with_items:
        - httpd
        - mysql-server
        - php
  • 也可以自己定义
---
- hosts: webserver
  remote_user: root
  tasks:
    - name: "Add users"
      user: name={{ item.name }} state=present groups={{ item.groups }}
      with_items:
        - { name:'test1', groups:'wheel'}
        - { name:'test2', groups:'root'}

TAG模块

  • 在一个playbook中,我们一般会定义很多个task,如果我们只想执行其中的某一个task或多个task时就可以使用tags标签功能了,格式如下:
vi hosts.yml
---
- hosts: webserver
  remote_user: root
  tasks:
    - name: Copy hosts file
      copy: src=/etc/hosts dest=/etc/hosts
      tags:
      - only
    - name: touch file
      file: path=/opt/hosts state=touch

执行命令:

ansible-playbook hosts.yml --tags="only" ansible-playbook hosts.yml
  • 事实上,不光可以为单个或多个task指定同一个tags。playbook还提供了一个特殊的tags为always。作用就是当使用always当tags的task时,无论执行哪一个tags时,定义有always的tags都会执行。
vi hosts.yml
---
- hosts: webserver
  remote_user: root
  tasks:
    - name: Copy hosts file
      copy: src=/etc/hosts dest=/etc/hosts
      tags:
      - only
    - name: copy test file
      copy: src=/etc/test.txt dest=/opt/test.txt
    - name: touch file
      file: path=/opt/hosts state=touch
      tags:
      - always

执行命令:

ansible-playbook hosts.yml --tags="only"
  • 注:此处我执行标记only,那么只会执行copy hosts file与touch file,而不会执行没有标记的test file
  • 若是执行:ansible-playbook hosts.yml,则所有tasks全部会执行。
  • 分别去两台被管理服务器上去查看文件创建情况

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Mobilizing Web Sites

Mobilizing Web Sites

Layon, Kristofer / 2011-12 / 266.00元

Everyone has been talking about the mobile web in recent years, and more of us are browsing the web on smartphones and similar devices than ever before. But most of what we are viewing has not yet bee......一起来看看 《Mobilizing Web Sites》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具