Ansible Playbook 入门指南

Ansible Playbook

对 ansible 基础彻底不了解能够看个人上一篇文章git

Playbook 是用YAML 格式写成。对YAML不了解的能够参见这篇简短的介绍github

大部分指令不把输出粘在这里,也是但愿读者本身边读,边操做。
为便于拷贝操做,命令行提示符$都将省略。web

ansible-playbook使用方法

脚本式

ansible-playbook playbook.yml
---
- hosts: all
  tasks:
    - name: Install Apache.
      command: yum install --quiet -y httpd httpd-devel
    - name: Copy configuration files.
      command: >
        cp httpd.conf /etc/httpd/conf/httpd.conf
    - command: >
        cp httpd-vhosts.conf /etc/httpd/conf/httpd-vhosts.conf
    - name: Start Apache and configure it to run at boot.
      command: service httpd start
    - command: chkconfig httpd on
命令:command 指令后紧跟的大于号(>)告诉YAML“自动将下一组缩进行引为一个长字符串,每行之间用空格分隔”。在某些状况下,它有助于提升任务的可读性。使用有效的YAML语法有多种描述配置的方法。

上面的playbook 与脚本无异,有助于把你原有的shell脚本的技能进行平滑过渡。shell

Ad-hoc 式

下面展现更有 ansible 味道的写法。apache

---
- hosts: all
  become: yes

  tasks:
    - name: Install Apache.
      yum: name={{ item }} state=present
      with_items:
        - httpd
        - httpd-devel
    - name: Copy configuration files.
      copy:
        src: "{{ item.src }}"
        dest: "{{ item.dest }}"
        owner: root
        group: root
        mode: 0644
      with_items:
        - src: "httpd.conf"
          dest: "/etc/httpd/conf/httpd.conf"
        - src: "httpd-vhosts.conf"
          dest: "/etc/httpd/conf/httpd-vhosts.conf"
    - name: Make sure Apache is started now and at boot.
      service: name=httpd state=started enabled=yes

这里become: yes 至关于 --sudo的做用。
state=present至关没有就安装。可选的 state 还有 latest - 保持最新,absent - 有就卸载,started - 启动等。segmentfault

运行ansible-playbook时加--check参数用于核查服务器的状态,不作修改操做(dry-run)。浏览器

能够经过 --limit参数指定范围,如:服务器

ansible-playbook playbook.yml --limit webservers

下面的指令将显示出做用于哪些主机:并发

ansible-playbook playbook.yml --list-hosts

指定专门用户:ssh

ansible-playbook playbook.yml --remote-user=johndoe

或以另外一个用户身份执行(同时在命令行询问密码):

ansible-playbook playbook.yml --become --become-user=janedoe \
--ask-become-pass

其余经常使用参数:

  • --inventory = PATH(-i PATH):定义自定义清单文件(默认为默认的Ansible清单文件,一般位于/etc/ansible/hosts中)。
  • --verbose(-v):详细模式(显示全部输出,包括成功选项的输出)。您能够传入-vvvv来提供每分钟的详细信息。
  • --extra-vars = VARS(-e VARS):以“键=值,键=值”格式定义要在剧本中使用的变量。
  • --forks = NUM​​(-f NUM):并发数(整数)。将此值设置为大于5的数字可增长Ansible将在其上同时运行任务的服务器的数量。
  • --connection = TYPE(-c TYPE):将使用的链接类型(默认为ssh;您有时可能但愿使用local在本地计算机上或经过cron在远程服务器上运行剧本)

这里有一些完整的例子,能够在DevOps实践中加以运用。

Ubuntu Server上装 solr

SoWkIImgAStDuIhEpimhI2nAp5KeIaqkISnBpqbLgERYBCelpKjnpY_AIosoiNasf_TfryAdCrVOsIaOcNBLSW41GhLIcAz-S6fHMMgHaWAAMh0v26ME1Od96QaAmVavG8KeCbw9POaA-GMfULafsKeAg7AXIQLv9QKA6ZwPUQKLcY3PMYw7rBmKeDi1

---
hosts: all
  vars_files;
  - var.yml

  pre_tasks:
    - name:  Update apt cache if needed.
      apt: update_cache=yes cache_valid_time=3600
  handlers:
      - name: restart solr
        service: name=solr state=restarted
  tasks:
      - name: Install Java.
        apt: name=openjdk-8-jdk state=present
      - name: Download Solr.
        get_url:
          url: "https://archive.apache.org/dist/lucene/solr/{{ solr_version }}/solr-{{ solr_version }}.tgz"
          dest: "{{ download_dir }}/solr-{{ solr_version }}.tgz"
          checksum: "{{ solr_checksum }}" 
      - name: Expand Solr.    
        unarchive:
            src: "{{ download_dir }}/solr-{{ solr_version }}.tgz"
            dest: "{{ download_dir }}"
            copy: no
            creates: "{{ download_dir }}/solr-{{ solr_version }}/README.txt"
      - name: Run Solr installation script.
        shell: >
            {{ download_dir }}/solr-{{ solr_version }}/bin/install_solr_service.sh
            {{ download_dir }}/solr-{{ solr_version }}.tgz
            -i /opt
            -d /var/solr
            -u solr
            -s solr
            -p 8983
            creates={{ solr_dir }}/bin/solr

       - name: Ensure solr is started and enabled on boot.
         service: name=solr state=started enabled=yes

执行:

ansible-playbook solr.yml --limit-hosts solr

顺利的话, 过一会就能够经过浏览器访问8983端口的solr管理界面了。

若有什么问题,能够参考这里的 源代码。

If everything is under control, you are going too slow...

小结

指南就到这里,实践才是关键。祝您好运。

相关文章
相关标签/搜索