美文网首页Ansible我爱编程
Ansible-playbook 基本语法与实例(学习笔记十九)

Ansible-playbook 基本语法与实例(学习笔记十九)

作者: SkTj | 来源:发表于2018-03-27 11:49 被阅读36次

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

    这个是你选择的主机

    • hosts: webservers

    这个是变量

    vars:

    http_port: 80
    
    max_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':'http://www.apache.com/tomcat-7.0.57.tar.gz'}"


    • 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 }}"
    

    相关文章

      网友评论

        本文标题:Ansible-playbook 基本语法与实例(学习笔记十九)

        本文链接:https://www.haomeiwen.com/subject/rsvjcftx.html