Ansible的脚本---playbook剧本

Ansible的脚本—playbook剧本

  • 通过task调用ansible的模板将多个play组织在一个playbook中运行。

playbooks本身由以下各部分组成

(1) Tasks:任务,即调用模块完成的某操作;
(2) Variables:变量
(3) Templates:模板
(4) Handlers:处理器,当某条件满足时,触发执行的操作;
(5) Roles:角色。

下面是一个playbook的示例

- hosts: mysql      //定义的主机组,即应用的主机
vars:             //定义变量 
 http_port: 80
 max_clients: 200
user: 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
   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开始运行

hosts和users介绍

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

还可以为每个任务定义远程执行用户:

---
- hosts: mysql
  remote_user: roottasks:
   - name: test connection
    ping:
    remote_user: mysql   #指定远程主机执行tasks的运行用户为mysql
执行playbook时: ansible-playbook ping.yml -k

指定远程主机sudo切换用户:

- hosts: mysql
 remote_user: root
 become: yes     #2.6版本以后的参数,之前是sudo,意思为切换用户运行
 become_user: mysql    #指定sudo用户为mysql
 tasks:
 - name: copy text
  copy: src=/etc/fstab dest=/home/mysql/fstab.bak
  
执行playbook时: ansible-playbook ping.yml -K

小示例:

---
- hosts: 192.168.80.182
 remote_user: root
 tasks:
  - name: disable selinux
   command: " /sbin/setenforce o'
  - name: make sure apache is running
   service: name=httpd state=started
play中只要执行命令的返回值不为0,就会报错,tasks停止

修改如下

- hosts: webserver
 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

playbook使用变量的方法:

1.通过ansible命令传递

例如:编辑如下yaml
vim a.yml
---
- hosts: mysql
 remote_user: root
 vars:
 -user:
  tasks:
  - name: add new user
   user: name={{user}}

然后执行命令:ansible-playbook a.yml -e "user=testvar"
可以执行命令查看: ansible mysql -m command -a 'tail /etc/passwd'

2.直接在yaml中定义变量—如上handlers示例

3.直接引用一些变量

:引用ansible的固定变量
vi test.yml
---
- hosts: mysql
 remote_user: root
 tasks:
 - name: copy file
  copy: content="ffansible_all_ipv4_addresses)}" dest=/opt/vars.txt
 执行命令:ansible-playbook test.yml
去183上查看vars.txt文件内容

再如:引用主机变量
vi /etc/ansible/hosts
在mysql组的主机后面添加如下
[mysql]
192.168.80.183 testvar="80.183"
#定义testvar变量的值为80.183

vi test.yml   #添加{ftestvar}主机变量
---
- hosts: mysql
 remote_user: root
 tasks:
 - name: copy file
  copy: content="{{ansible_all_ipv4_addresses}}.{{testvarl}}" dest=/opt/vars.txt
 执行命令: ansible-playbook test.yml
去183上查看vars.txt文件内容



条件测试

  • 如果需要根据变量、facts (setup)或此前任务的执行结果来作为某task执行与否的前提时要用到条件测试,在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")

迭代

  • 当有需要重复性执行的任务时,可以使用迭代机制。其使用格式为将需要迭代的内容定义为item变量引用,并通过with_items语句指明

- hosts: webserver
 remote_user: root
 tasks:
  - name: "Install Packages"
   yum: name=ff 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"}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值