Some tips learning from “Ansible Up & Running”.
The repo of this book https://github.com/lorin/ansiblebook
Introduction
In a way, YAML is to JSON what Markdown is to HTML.
Installing Ansible
|
ansible.cfg
Ansible looks for an ansible.cfg file in the following places, in this order:
- File specified by the ANSIBLE_CONFIG environment variable
- ./ansible.cfg (ansible.cfg in the current directory)
- ~/.ansible.cfg (.ansible.cfg in your home directory)
- /etc/ansible/ansible.cfg
Playbooks:A Beginning
web-notls.yml
YAML truthy and falsey
- YAML truthy
true, True, TRUE, yes, Yes, YES, on, On, ON, y, Y
- YAML falsey
false, False, FALSE, no, No, NO, off, Off, OFF, n, N
Cowsay
|
Disable cowsay be setting the ANSIBLE_NOCOWS environment variable like this:
Also add the following in ansible.cfg file
Playbooks Are YAML
Ansible playbooks are written in YAML syntax. YAML is a file format similar in intent to JSON, but generally easier for humans to read and write. Before we go over the playbook, let’s cover the concepts of YAML that are most important for writing playbooks.
the JSON equivalent of web-notls.yml
Handlers
Handlers are one of the conditional forms that Ansible supports. A handler is similar to a task, but it runs only if it has been notified by a task. A task will fire the notifica‐ tion if Ansible recognizes that the task has changed the state of the system.
web-tls.yml
Inventory: Describing Your Servers
Behavioral Inventory Parameters
Name | Default | Description |
---|---|---|
ansible_ssh_host | name of host | Hostname or IP address to SSH to |
ansible_ssh_port | 22 | Port to SSH to |
ansible_ssh_user | root | User to SSH to |
ansible_ssh_pass | none | Password to use for SSH authentication |
ansible_connection | smart | How Ansible will connect to host |
ansible_ssh_private_key_file | none | SSH private key to use for SSH authentication |
ansible_shell_type | sh | Shell to use for commands |
ansible_python_interpreter | /usr/bin/python | Pyhton interpreter on host |
ansible_*_interpreter | none | Like ansible_python_interpreter for other languages |
The Interface for a Dynamic Inventory Script
An Ansible dynamic inventory script must support two command-line flags:
- –host=
for showing host details - –list for listing groups
Adding Entries at Runtime with add_host and group_by
add_by
The add_host module adds a host to the inventory. This module is useful if you’re using Ansible to provision new virtual machine instances inside of an infrastructure- as-a-service cloud.
group_by
Use the ansible_distribution fact to group hosts by Linux distribution(e.g., Ubuntu, CentOS)
Example. Creating ad-hoc(点对点) groups based on Linux distribution
Variables and Facts
Defining Variables in Playbooks
The simplest way to define variables is to put a vars section in your playbook with the names and values of variables.
Ansible also allows you to put variables into one or more files, using a section called vars_files.
The nginx.yml file would look like:
Registering Variables
|
Facts
Viewing All Facts Associated with a Server
|
Viewing a Subset of Facts
|
Local Facts
Ansible also provides an additional mechanism for associating facts with a host. You can place one or more files on the host machine in the /etc/ansible/facts.d directory. Ansible will recognize the file if it’s:
- In .ini format
- In JSON format
- An executable that takes no arguments and outputs JSON on standard out
http://docs.ansible.com/ansible/playbooks_variables.html#local-facts-facts-d
Deploying Mezzanine with Ansible
Listing Tasks in a Playbook
|
Using Iteration(with_items) to Install Multiple Packages
|
equal to below:
Installing Packages into a virtualenv
|
directory of requirements is in remote host(client host),也可以写成virtualenv路径的相对路径
Complex Arguments in Tasks
|
Complex Playbooks
Running a Task on a Machine Other Than the Host
delegate_to
In this example, Ansible would execute the nagios task on nagios.example.com, but the inventory_hostname variable referenced in the play would evaluate to the web host.
Encrypting Sensitive Data with Vault
|
|
Roles:Scaling Up Playbooks
Basic Structure of a Role
roles/database/tasks/main.yml
Tasks
roles/database/files/
Holds files to be uploaded to hosts
roles/database/templates/
Holds Jinja2 template files
roles/database/handlers/main.yml
Handlers
roles/database/vars/main.yml
Variables that shouldn’t be overridden
roles/database/defaults/main.yml
Default variables that can be overridden
roles/database/meta/main.yml
Dependency information about a role