博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ansible-playbook 基本语法与实例(学习笔记十九)
阅读量:7063 次
发布时间:2019-06-28

本文共 3011 字,大约阅读时间需要 10 分钟。

1、安装apache,做初始配置,并启动服务:

这个是你选择的主机

  • hosts: webservers

这个是变量

vars:

http_port: 80max_clients: 200

远端的执行权限

remote_user: root

tasks:

利用yum模块来操作

  • 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

这里的restart apache 和上面的触发是配对的。这就是handlers的作用。相当于tag

handlers:

- name: restart apache  service: name=httpd state=restarted

2、可以同时使用10个进程进行,调用格式为:

ansible-playbook test.yml -f 10

3、对于没有把握执行的任务,需要加上 ignore_errors: True,这样即使出错,下一个任务也会继续执行

4、ansible-playbook可以根据上一个任务的执行结果,来判断执行下一个任务,系统参数为when:

tasks:

  • shell: /usr/bin/foo

    register: foo_result

    ignore_errors: True

  • name: "cmd"

    action: command touch /root/kkkkk

    when: foo_result.rc == 127

5、根据操作系统类型,执行不同安装:

  • hosts: all
    remote_user: root
    gather_facts:True
    tasks:
  • name: install apache on CentOS
    yum: name=httpd state=present
    when: ansible_os_family =="CentOS"
  • name: install apache on Debian
    yum: name=apache2 state=present
    when: ansible_os_family =="Debian"

6、notify和handlers来执行触发,handlers可多次调用:

  • name: template configuration file
    template: src=template.j2 dest=/etc/foo.conf
    notify:
    • restart memcached
    • restart apache
      handlers:
    • name: restart memcached
      service: name=memcached state=restarted
    • name: restart apache
      service: name=apache state=restarted

7、执行多个变量,每个变量为一次任务:

tasks:

  • name: panduan
    command: echo {
    { item }}
    with_items: [ 0,2,4,6,8,10 ]

8、特定任务用特定用户执行:

  • hosts: webnodes
    remote_user: mageedu
    tasks:
    • name: test connection
      ping:
      remote_user: mageedu
      sudo: yes

9、捕获执行中的错误,并进行补救:

tasks:

  • block:
    • name: Create {
      { maildir_path }}
      copy:
      src: "{
      { maildir }}"
      dest: "{
      { maildir_path }}"
      mode: 0755
      register: command_output
      rescue:
    • name: Install mail packages
      yum:
      name: "{
      { item }}"
      state: latest
      with_items:
      • "{
        { mail_package }}"
      • dovecot
        always:
    • name: start the mail service
      service:
      name: "{
      { mail_service }}"
      state: restarted

10、对任务做tag标记,可以只执行某个tag,执行语法为:ansible-playbook -t TAGS_NAME playbook.yaml

  • hosts: all #所有远程主机
    remote_user: root #以远程主机上root用户执行
    tasks: #任务
    • name: install redis #任务之安装
      yum: name=redis state=latest #动作调用yum模块安装
    • name: copy config file #任务之复制同步配置文件到远程目标主机
      copy: src=/root/playbooks/redis.conf dest=/etc/redis.conf owner=redis #动作copy模块执行
      notify: restart redis #触发的动作
      tags: configfile #任务标记名configfile

11、从外部传入变量,ansible-playbook的格式为:ansible-playbook tomcat-install.yml --extra-vars "{'host':'192.168.11.111', 'tomcat_home':'/opt/tomcat-test', 'url':'}"


  • name: Tomcat install and configuration

    hosts: "{

    { host }}"

    user: root

vars:

tomcat_home: "{
{ tomcat_home }}"

tasks:

- name: absent old tomcat  file: path={
{ item }} state=absent with_items: - "{
{ tomcat_home }}" - /geelyapp/auto_scripts/tomcat.sh- name: get tomcat_tar_gz get_url: url={
{ url }} dest=/tmp/tomcat.tar.gz- name: Create the dir file: path={
{ item }} state=directory with_items: - /geelyapp/gc_log - /geelyapp/auto_scripts - "{
{ tomcat_home }}"

转载地址:http://xiill.baihongyu.com/

你可能感兴趣的文章
Web Service单元测试工具实例介绍之SoapUI
查看>>
谈谈javascript语法里一些难点问题(一)
查看>>
【BZOJ】1082: [SCOI2005]栅栏(二分+dfs)
查看>>
通过递归组合多维数组!
查看>>
ocp 1Z0-051 23-70题解析
查看>>
关于MFLAGS与MAKEFLAGS
查看>>
NotePad++ for PHP
查看>>
ssh事务回滚,纪念这几个月困扰已久的心酸
查看>>
jQuery中的编程范式
查看>>
比较快速排序,冒泡排序,双向冒泡排序的执行效率
查看>>
还没被玩坏的robobrowser(5)——Beautiful Soup的过滤器
查看>>
Linux 精准获取进程pid--转
查看>>
Servlet、Filter、Listener总结
查看>>
[翻译] JTBorderDotAnimation
查看>>
关于Thread类中三个interrupt方法的研究与学习(转)
查看>>
mysql 加入列,改动列,删除列。
查看>>
UML的学习
查看>>
ContentProvider简单介绍
查看>>
SQL SERVER获取数据库中所有表名 XTYPE类型
查看>>
java设计模式3--单例模式(Singleton)
查看>>