最近在学习Ansible,感觉还是官方文档比较方便学习,截取一些基本概念放到这篇文章里方便将来参考。接下来我会再写几篇关于Ansible搭建及实践的文章。欢迎大家指正错误,提问交流。(一些重点概念,超链到官方文档上了,感兴趣的同学可以详细阅读)
=============== 分割线 ===============
About Ansible:
Ansible is an agentless IT automation tool. It can configure systems, deploy software, and orchestrate more advanced IT tasks such as continuous deployments or zero downtime rolling updates.
YAML
YAML is easier for humans to read and write than other common data formats like XML or JSON. Further, there are libraries available in most programming languages for working with YAML.
1. list are lines beginning at the same indentation level starting with a "- " (a dash and a space)
--- # A list of tasty fruits fruits: - Apple - Orange - Strawberry - Mango ...
2. A dictionary is represented in a simple key: value form (the colon must be followed by a space):
# An employee record martin: name: Martin D'vloper job: Developer skill: Elite
3. More complicated data structures are possible, such as lists of dictionaries, dictionaries whose values are lists or a mix of both:
# Employee records - martin: name: Martin D'vloper job: Developer skills: - python - perl - pascal - tabitha: name: Tabitha Bitumen job: Developer skills: - lisp - fortran - erlang
A list of managed nodes. An inventory file is also sometimes called a “hostfile”. Your inventory can specify information like IP address for each managed node. An inventory can also organize managed nodes, creating and nesting groups for easier scaling.
# Hosts and Groups, more pls refer to official doc mail.example.com [webservers] foo.example.com bar.example.com [dbservers] one.example.com two.example.com three.example.com
Ordered lists of tasks, saved so you can run those tasks in that order repeatedly.
--- - hosts: webservers vars: http_port: 80 max_clients: 200 remote_user: root tasks: - name: ensure apache is at the latest version yum: name: 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 (and enable it at boot) service: name: httpd state: started enabled: yes handlers: - name: restart apache service: name: httpd state: restarted
The units of code Ansible executes. Each module has a particular use. Ansible modules are categorized into various groups based on their functionality.
While automation exists to make it easier to make things repeatable, all of your systems are likely not exactly alike.
On some systems you may want to set some behavior or configuration that is slightly different from others. Also, some of the observed behavior or state of remote systems might need to influence how you configure those systems. (Such as you might need to find out the IP address of a system and even use it as a configuration value on another system). You might have some templates for configuration files that are mostly the same, but slightly different based on those variables.
Variables in Ansible are how we deal with differences between systems
Often the result of a play may depend on the value of a variable, fact (something learned about the remote system), or previous task result.
# The When Statement
tasks: - name: "shut down Debian flavored systems" command: /sbin/shutdown -t now when: ansible_os_family == "Debian" # note that Ansible facts and vars like ansible_os_family can be used # directly in conditionals without double curly braces
Often you’ll want to do many things in one task, such as create a lot of users, install a lot of packages, or repeat a polling step until a certain result is reached.
- name: add several users user: name: "{{ item }}" state: present groups: "wheel" loop: - testuser1 - testuser2
If you have worked with any other programming languages, or scripts, you probably know that you could write a large script or program in a single file or what is more preferred is to modularize it into packages, modules, classes and functions. That way our code becomes more organized, reusable and easy to read and build upon and share with others. This is implemented in ansible with the help of roles.
When we have too many things to automate, our playbook inventory files, variables are going to get bigger and more difficult to manage. Writing a single large playbook may not be ideal in that case.
INCLUDE
[ -include <playbook name> ]
We simply cut this large playbook into smaller files that address different use cases, and then finally have a master playbook that includes the smaller playbooks. (vars_files is what we use for including variables defined in another file. )
Rerference Doc: Ansible official website