当前位置:主页 > 查看内容

ansible---------剧本playbook详解

发布时间:2021-05-21 00:00| 位朋友查看

简介:目录 一、ansible的主机清单管理 1、基本配置 2、类似的主机名配置 3、定义变量 二、YAML 1、YAML介绍 2、格式 3、YAML支持的数据结构 三、playbook简介 四、playbook的核心元素 五、playbook剧本示例 一、ansible的主机清单管理 1、基本配置 vim / etc / an……

一、ansible的主机清单管理

1、基本配置

vim /etc/ansible/hosts
[webserver]					# 设置组名
www1.xxxxx.com				# 定义被监控的主机,可以是主机名也可以是IP地址
www2.xxxxx.com:22222		# 冒号后定义远程连接端口,默认是ssh的22端口

2、类似的主机名配置

[webserver]
www[01:50].xxxxxxx.com ansible_ssh_user=root ansible_ssh_pass=123123
# 这句话表示0150的主机,可以使用免交互的方式进行管理
[dbbservers]
db-[a:f].xxxxxxx.com		# 支持匹配a b c d e f 

3、定义变量

1)主机变量

[webserver]
www1.xxxxxxx.com http_port=80 maxRequestsChild=808
www2.xxxxxxx.com http_port=8080 maxRequestsChild=909

2)组变量

[servers:vars]
ntp_server=ntp.xxxxxx.com
nfs_server=nfs.xxxxxx.com

3)组嵌套

[apache]
http1.xxxxxxx.com
http2.xxxxxxx.com
[nginx]
ngx1.xxxxxxx.com
ngx2.xxxxxxx.com
[webservers:children]
apache
nginx

4)inventory变量参数

参数													说明
ansible_ssh_host						将要连接的远程主机名,与你想要设定的主机的别名不同的话,可通过此变量设置 
ansible_ssh_port						ssh端口号,如果不是默认的端口号,通过此变量设置
ansible_ssh_user						默认的ssh用户名
ansible_ssh_pass						ssh密码(这种方式并不安全,强烈建议使用--ask-pass或SSH密钥)
ansible_ssh_private_key_file			ssh使用的私钥文件,适用于有多个密钥,而你不想用SSH代理的情况
ansible_ssh_common_args					此设置附加到sftp,scp和ssh默认命令行
ansible_sftp_extra_args					此设置附加到默认sftp命令行
ansible_scp_extra_args					此设置附加到默认scp命令行
ansible_ssh_extra_args					此设置附加到默认ssh命令行
ansible_ssh_pipelining					确定是否使用SSH管道,可以覆盖ansible.cfg中的设置
ansible_shell_type						目标系统的shell类型,默认情况下,命令的执行使用”sh“语法,可设置为”csh“或”fish“
ansible_python_interpreter				目标主机的python路径,适用于的情况系统中有多个 Python, 或者命令路径不是"/usr/bin/python
ansible_*_interpreter					这里的*可以是ruby或Perl或者其他语言的解释器,作用和ansible_python_interpreter类似	
ansible_shell_executable				将设置ansible控制器将在目标机器上使用的shell,覆盖ansible.cfg中的默认为/bin/sh

二、YAML

1、YAML介绍

  • YAML另一种标记语言,是用来写配置文件的语言,非常简洁和强大
  • YAML语法和其他语言类似,也可以表达散列表,标量等数据结构
  • 结构通过空格来展示,序列里配置项通过”-“来代表,Map里键值用”:“来分隔;

2、格式

  • 大小写敏感
  • 使用缩进表示层级关系
  • 缩进时不允许使用制表符,只允许使用空格
  • 缩进的空格数不重要,只要相同层级的元素左侧对齐即可

3、YAML支持的数据结构

  • 对象:键值对的集合,又称映射(mapping)、哈希(hashes)、字典(dictionary)
例如:name:example  Developer
	  	
  • 数组:一组按次序排列的值,又称为序列(sequence)、列表(list)
例如: - football
      - basketball
  • 纯量 单个的、不可再分的值
例如:number:33.33
	 sure:true

YAML例子

name:zhangsan
age:21
name:lisi
age:22
people:
- name:zhangsan
		age:21
		-name:lisi
		age:22

三、playbook简介

playbook配置文件使用了YAML语法,具有简介明了、结构清晰等特点,playbook配置文件类似于shell脚本,是一个YAML格式的文件,用于保存针对特定需求的任务列表,之前介绍的ansible命令虽然可以完成各种任务,但是当配置一些复杂的任务时,逐条输入就显得效率非常地下了,更有效的方案是在playbook配置文件中放置所有的任务代码,利用ansible-playbook命令执行该文件,可实现自动化运维,YAML文件的扩展名通常为.yaml或者.yml

四、playbook的核心元素

  • tasks:任务,即调用模块完成某操作
  • variables:变量
  • templates:模板
  • handlers:处理器,当某种条件满足时,触发执行的操作
  • roles:角色

五、playbook剧本示例

- hosts: test01
  vars:
    http_port: 80
    max_clients: 200
  user: root
  tasks:
  - name: ensure apache is at the latest version
    yum: name=httpd state=latest
  - name: write the apache config file
    template: src=/srv/httpd.j2 dest=/etc/httpd.conf
    notify:
    - restart apache
  - name: ensure apache is running
    service: name=httpd state=started
  handlers:
   - name: restart apache
     service: name=httpd state=restarted

执行一个playbook

ansible-playbook [yaml文件名]

例如:

ansible-playbook ping.yml
-k (-ask-pass)用来交互输入ssh密码
-K(-ask-become-pass)用来交互输入sudo密码;-u:指定用户
补充命令

ansible-playbook nginx.yaml --syntax-check			# 检查yaml文件的语法
ansible-playbook nginx.yaml --list-task				# 检查tasks任务
ansible-playbook nginx.yaml --list-hosts			# 检查生效的主机
ansible-playbook nginx.yaml --start-at-task='Copy Nginx.conf'    # 从某一个task任务开始

1)hosts和users介绍

- hosts: test01					# 指定主机组,可以是一个或多个组
  remote_user:root				# 指定远程主机执行的用户名

2)为每个任务定义远程执行用户

- hosts: test
  remote_user: root
  tasks:
   - name: test connection
     ping:
     remote_user: root		
  1. 指定远程主机sudo切换用户
- hosts:test
  remote_user: root
  become: yes
  become_user: mysql

4)tasks列表和action

- hosts: 192.168.241.20
  remote_user: root
  tasks:
   - name: disable selinux
     command: '/sbin/setenforce 0'
   - name: make sure apache is running
     service: name=httpd state=started

play中只要执行命令的返回值不为0,就会报错,tasks停止

- hosts: test
  remote_user: root
  tasks:
   - name: disable selinux
     command: '/sbin/setenforce 0'
     ignore_errors: True					# 忽略错误,强制返回成功
   - name: make sure apache is running
     service: name=httpd state=started

另一个示例:

- hosts: test
  remote_user: root
  tasks:
   - name: create nginx group
     group: name=nginx system=yes gid=208
   - name: create nginx user
     user: name=nginx uid=208 group=nginx system=yes
- hosts: test01
  remote_user: root
  tasks:
   - name: copy file to mysql
     copy: src=/etc/inittab dest=/opt/inittab.bak     

5)Handlers

- hosts: test
  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:
      - restart httpd
   - name: start httpd service
     service: enabled=true name=httpd state=started
  handlers:
   - name: restart httpd
     service: name=httpd state=restarted

这里也可以引入变量

- hosts: test
  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
     service: name={{service}} state=restarted

playbook使用变量的方法
1、通过ansible命令传递
例如:编译如下yaml

- hosts: test01
  remote_user: root
  vars:
  - user:
  tasks:
  - name: add new user
    user: name={{user}}

执行命令:
ansible-playbook a.yml -e “user=testvar”
查看:
ansible test01 -a ‘tail /etc/passwd’

2、直接在yaml中定义变量—如上handlers示例
3、直接引用一些变量
如:引用ansible的固定变量

vim test.yml
- hosts: test01
  remote_user: root
  tasks:
   - name: copy file
     copy: content={{ansible_all_ipv4_addresses}} dest=/opt/1.txt

4、引用主机变量

vim /etc/ansible/hosts
在test01组的主机后面添加如下
[test01]
192.168.241.4 testvar="8080"			# 定义testvar变量的值为8080

vim test.yml
- hosts: test01
  remote_user: root
  tasks:
   - name: copy file
     copy: content= {{ansible_all_ipv4_addresses}} {{testvar}} dest=/opt/1.txt

5、条件测试
单条件判断:

vim when.yml
- hosts: test01
  remote_user: root
  tasks:
   - name: shutdown centos
     command: /sbin/shutdown -r now				# h:关机,r:重启
     when: ansible_distribution == "CentOS"

多条件判断:

vim when.yml
- hosts: test01
  remote_user: root
  tasks:
   - name: shutdown centos7 systems
     command: /sbin/shutdown -r now
     when:
      - ansible_distribution == "CentOS"
      - ansible_distribution_major_version == "7"

组条件判断:

vim when.yml
- hosts: test01
  remote_user: root
  tasks:
    - name: shutdown 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")

自定义变量进行条件测试

vim when.yml
- hosts: all
  vars:
   exist: "True"
  tasks:
   - name: create file
     command: touch /tmp/test.txt
     when: exist | match("True")
   - name: delete file
     command: rm -rf /tmp/test.txt
     when: exist | match("False")

6、迭代

- hosts: test
  remote_user: root
  tasks:
   - name: add user
     user: name={{item}}
     with_items:
       - tw
       - gcc
       - yy

也可以自己定义

- hosts: test01
  remote_user: root
  tasks:
   - name: add user
     user: name={{ item.name }} state=present groups={{ item.groups }}
     with_items:
       - { name: 'test1', groups: 'wheel'}
       - { name: 'test2', groups: 'root'}

;原文链接:https://blog.csdn.net/weixin_51432789/article/details/115477169
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!
上一篇:为什么使用框架 使用框架的优缺点 下一篇:没有了

推荐图文


随机推荐