Ansible小结(四)---playbook介绍及基本应用

playbook是由一个或者多个play组成的列表。play的主要功能在于将事先归并为一组的主机装扮成事先通过ansible中task定义好的角色。从根本上来讲,所谓的task无非是调用ansible的一个module。将多个play组织在一个playbook中,即可以让它们联合起来按事先编排的机制来完成某一任务。
示例:
- hosts: test
  user: root
  tasks:
   - name: ssh-copy
     authorized_key: user=root key="{{ lookup('file', '/root/.ssh/id_rsa.pub') }}"
     tags:
       - sshkey

一、playbook基本介绍

1. 每一个playbook分为4个部分:

target section
    定义将要执行playbook的远程主机组
variable section
    定义playbook运行时需要使用的变量
task section
    定义将要在远程主机上执行的任务列表
handler section
    定义task执行完成以后需要调用的任务

1.1 target section

hosts: 定义远程的主机组
user: 执行该任务组的用户
become:如果设置为yes。执行该任务组的用户在执行任务的时候,获取root权限
become_user: 指定sudo提权的用户,默认为root,如果需要sudo到其他用户,这里可以指定,如A用户sudo到B用户,这里设置为B即可
connection:指定通过什么方式连接到远程主机,默认ssh
gathe_facts:在执行playbook的时候默认会执行setup模块获取远程主机信息。一般情况下默认即可。如果远程主机过多,或者playbook执行慢,可以设置为no关闭此选项,提高运行效率。

备注:如果sudo用户没有设置为nopassword,需要在ansible的hosts文件中指定sudo_pass,如下所示

[root@Server playbooks]# grep -A 2 test /etc/ansible/hosts 
[test]
192.168.10.101 ansible_sudo_pass="123456"

示例:

向远程主机发送一个文件,通过sudo的方式

- hosts: test
  user: root
  become: yes
  become_user: root
  gather_facts: no 
  tasks:
    - name: touch a file
      template: src=named.conf.j2 dest=/etc/named.conf

执行结果:

开启gather_facts结果:

[root@Server playbooks]# time ansible-playbook ls.yaml 

PLAY [test] ******************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************
ok: [192.168.10.101]

TASK [touch a file] **********************************************************************************************************************************************************************************************************************************************************
ok: [192.168.10.101]

PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
192.168.10.101             : ok=2    changed=0    unreachable=0    failed=0   


real    0m4.829s
user    0m1.231s
sys    0m0.290s

关闭开启gather_facts结果:

[root@Server playbooks]# time ansible-playbook ls.yaml 

PLAY [test] ******************************************************************************************************************************************************************************************************************************************************************

TASK [touch a file] **********************************************************************************************************************************************************************************************************************************************************
ok: [192.168.10.101]

PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
192.168.10.101             : ok=1    changed=0    unreachable=0    failed=0   


real    0m1.560s
user    0m0.886s
sys    0m0.228s

可以看出节省了3秒多,如果大批量执行的情况下,效率会提高不少。

1.2  variable section

变量部分主要有3中方法,

var:直接调用变量

vars_files:在files文件中通过字典的方式调用变量,变量值预先设定好了

vars_prompt:通过交互的方式为变量赋值,可设置为隐私模式,输入值的时候屏幕不显示

1.2.1 vars用法

pass

1.2.2 vars_files用法

[root@Server playbooks]# cat test.yaml
- hosts: test
  user: root
  vars_files:
    - variables

  tasks: 
    - name: copy a file
      template: src=files/test.txt dest=/data/test.txt

  [root@Server playbooks]# cat variables
  port: 8080
  http: nginx
  [root@Server playbooks]# cat files/test.txt
  {{port}}
  {{http}}

远程主机执行结果:

[root@Client data]# cat test.txt 
8080
nginx

 1.2.3 vars_prompts用法

[root@Server playbooks]# cat test1.yaml 
- hosts: test
  
  vars_prompt: 
    - name: http
      prompt: plz input something
      private: yes

  tasks: 
    - name: this is a test
      template: src=files/test.txt dest=/data/test.txt 
[root@Server playbooks]# cat files/test.txt 
{{ http }}
private: yes  表示屏幕不显示输入内容,设置为隐私

执行结果:
[root@Client data]# cat test.txt 
11111

备注:注意!!vars_prompt下面的name属性就是文件的中的大括号呢的属性

1.3 task section

定义一组组任务,以tasks开头

案列:

调用tasks任务的三种方法:
tasks: 
#第一种
  - name:install mysqld (定义任务名)
  action: yum name=mysqld state=install (定义任务动作)

#第二种
  -name: configure mysql
  copy: src=/etc/my.cnf dest=/etc/my.cnf 

#第三种
  - name: start mysql
  service: 
    name: mysqld
    state: start

1.4  handler section

handler只有在接到通知的时候才会执行,不管接到多少次通知,handler只执行一次。

完整案列:

在远程主机安装httpd并通过handler的方式启动httpd服务,关闭获取setup模块信息。

[root@Server playbooks]# cat install_httpd.yaml 
- hosts: test
  user: root
  gather_facts: no
  tasks:
    - name: install httpd
      yum: name=httpd state=installed
      notify: 
        - start httpd
  handlers: 
    - name: start httpd
      service: name=httpd state=started

猜你喜欢

转载自www.cnblogs.com/cangyuefeng/p/9081972.html