一、playbook

playbooks是一个不同于使用Ansible命令行执行方式的模式,其功能更强大灵活。
简单来说,playbook是一个非常简单的配置管理和多主机部署系统,不同于任何已经存在的模式,可作为一个适合部署复杂应用程序的基础。
Playbook可以定制配置,可以按照指定的操作步骤有序执行,支持同步和异步方式。
值得注意的是playbook是通过YAML格式来进行描述定义的。
playbook是由一个或多个模块组成的,使用多个不同的模块,完成一件事情。
playbook通过yaml语法识别描述的状态文件。扩展名是yaml

二、创建一个playbook

 

 

[root@mongodb05 playbook]# cat run.yml
---
- hosts: agent
  remote_user: root
  tasks:
  - name: mkdir prometheus
    command: 'mkdir /u01/soft/prometheus -p'
  - name: scp node_exporter
    copy: src=/u01/soft/prometheus/node_exporter-0.18.1.linux-amd64.tar.gz dest=/tmp/node_exporter-0.18.1.linux-amd64.tar.gz
  - name: tar node_exporter-0.18.1.linux-amd64.tar.gz
    command: 'tar -vzxf /tmp/node_exporter-0.18.1.linux-amd64.tar.gz -C /u01/soft/prometheus'
  - name: "rename node_exporter"
    command: 'mv /u01/soft/prometheus/node_exporter-0.18.1.linux-amd64   /u01/soft/prometheus/node_exporter'
  - name: "scp node_exporter.service"
    copy: src=/usr/lib/systemd/system/node_exporter.service dest=/usr/lib/systemd/system/node_exporter.service backup=yes
  - name: create group
    command: 'groupadd prometheus'
  - name: create user
    command: 'useradd -g prometheus prometheus'
  - name: change prometheus owner
    command: 'chown -R prometheus.prometheus /u01/soft/prometheus/'
  - name: systemctl daemon-reload
    command: 'systemctl daemon-reload'
  - name: enable node_exporter.service
    command: 'systemctl enable node_exporter.service'
  - name: start node_exporter.service
    command: 'systemctl start node_exporter.service'

三、检查playbook语法

ansible-playbook run.yml  --syntax-check

 

 

四、执行playbook

每一步都是分服务器执行

[root@mongodb05 playbook]# ansible-playbook run.yml
[DEPRECATION WARNING]: The TRANSFORM_INVALID_GROUP_CHARS settings is set to allow bad characters in group names by default, this will change, but still be user configurable on deprecation. This feature will be removed in version
2.10. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details

PLAY [agent] *****************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************
ok: [172.16.102.16]
ok: [172.16.102.14]
ok: [172.16.102.13]
ok: [172.16.102.12]
ok: [172.16.102.19]
ok: [172.16.102.18]
ok: [172.16.102.20]
ok: [172.16.102.11]
TASK [mkdir prometheus] ******************************************************************************************************************************************************************************************************************
[WARNING]: Consider using the file module with state=directory rather than running 'mkdir'.  If you need to use command because file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in
ansible.cfg to get rid of this message.
changed: [172.16.102.14]
changed: [172.16.102.16]
changed: [172.16.102.12]
changed: [172.16.102.13]
changed: [172.16.102.19]
changed: [172.16.102.20]
changed: [172.16.102.18]
changed: [172.16.102.11]
TASK [scp node_exporter] *****************************************************************************************************************************************************************************************************************
changed: [172.16.102.14]
changed: [172.16.102.12]
changed: [172.16.102.16]
changed: [172.16.102.13]
changed: [172.16.102.19]
changed: [172.16.102.20]
changed: [172.16.102.18]
changed: [172.16.102.11]
TASK [tar node_exporter-0.18.1.linux-amd64.tar.gz] ***************************************************************************************************************************************************************************************
[WARNING]: Consider using the unarchive module rather than running 'tar'.  If you need to use command because unarchive is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to
get rid of this message.
changed: [172.16.102.14]
changed: [172.16.102.16]
changed: [172.16.102.13]
changed: [172.16.102.12]
changed: [172.16.102.19]
changed: [172.16.102.18]
changed: [172.16.102.20]
changed: [172.16.102.11]
TASK [rename node_exporter] **************************************************************************************************************************************************************************************************************
changed: [172.16.102.16]
changed: [172.16.102.13]
changed: [172.16.102.12]
changed: [172.16.102.14]
changed: [172.16.102.19]
changed: [172.16.102.20]
changed: [172.16.102.18]
changed: [172.16.102.11]
TASK [scp node_exporter.service] *********************************************************************************************************************************************************************************************************
changed: [172.16.102.16]
changed: [172.16.102.14]
changed: [172.16.102.13]
ok: [172.16.102.12]
changed: [172.16.102.19]
changed: [172.16.102.18]
changed: [172.16.102.20]
changed: [172.16.102.11]
TASK [create group] **********************************************************************************************************************************************************************************************************************
changed: [172.16.102.16]
changed: [172.16.102.14]
changed: [172.16.102.13]
changed: [172.16.102.12]
changed: [172.16.102.19]
changed: [172.16.102.18]
changed: [172.16.102.20]
changed: [172.16.102.11]
TASK [create user] ***********************************************************************************************************************************************************************************************************************
changed: [172.16.102.14]
changed: [172.16.102.16]
changed: [172.16.102.12]
changed: [172.16.102.19]
changed: [172.16.102.13]
changed: [172.16.102.18]
changed: [172.16.102.20]
changed: [172.16.102.11]
TASK [change prometheus owner] ***********************************************************************************************************************************************************************************************************
[WARNING]: Consider using the file module with owner rather than running 'chown'.  If you need to use command because file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg
to get rid of this message.
changed: [172.16.102.14]
changed: [172.16.102.13]
changed: [172.16.102.16]
changed: [172.16.102.12]
changed: [172.16.102.19]
changed: [172.16.102.18]
changed: [172.16.102.20]
changed: [172.16.102.11]
TASK [systemctl daemon-reload] ***********************************************************************************************************************************************************************************************************
changed: [172.16.102.16]
changed: [172.16.102.14]
changed: [172.16.102.12]
changed: [172.16.102.13]
changed: [172.16.102.19]
changed: [172.16.102.18]
changed: [172.16.102.20]
changed: [172.16.102.11]
TASK [enable node_exporter.service] ******************************************************************************************************************************************************************************************************
changed: [172.16.102.16]
changed: [172.16.102.14]
changed: [172.16.102.12]
changed: [172.16.102.13]
changed: [172.16.102.19]
changed: [172.16.102.18]
changed: [172.16.102.20]
changed: [172.16.102.11]
TASK [start node_exporter.service] *******************************************************************************************************************************************************************************************************
changed: [172.16.102.16]
changed: [172.16.102.12]
changed: [172.16.102.14]
changed: [172.16.102.13]
changed: [172.16.102.19]
changed: [172.16.102.18]
changed: [172.16.102.20]
changed: [172.16.102.11]
PLAY RECAP *******************************************************************************************************************************************************************************************************************************
172.16.102.11              : ok=12   changed=11   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
172.16.102.12              : ok=12   changed=10   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
172.16.102.13              : ok=12   changed=11   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
172.16.102.14              : ok=12   changed=11   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
172.16.102.16              : ok=12   changed=11   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
172.16.102.18              : ok=12   changed=11   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
172.16.102.19              : ok=12   changed=11   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
172.16.102.20              : ok=12   changed=11   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
 posted on 2020-01-21 14:14  xibuhaohao  阅读(772)  评论(0编辑  收藏  举报