0 Basic idea
A task is a call to an ansible module.
A play contains one or more tasks against the same group of servers.
A playbook is a file that contains one or more plays.
1 ansible command can only run one task at once
When using the ansible command-line tool, you can only run one task.
$ ansible localhost -m command -a 'date'
Of course, you can write a script to run more tasks. like:
#!/bin/bash
ansible localhost -m command -a 'date'
ansible localhost -m copy -a 'src=test dest=/etc/test'
ansible localhost -m shell -a 'cat /etc/test'
Even though it works, Ansible provides a simpler and more powerful way ---- Playbook.
2 Run multiple tasks with ansible-playbook
A playbook is a file in YAML format. Below is an example.
# PLAY 1
- hosts: dbservers
tasks:
# TASK 1
- name: install mysql
command: touch /etc/mysql
# PLAY 2
- hosts: webservers
become: yes
become_method: su
become_user: root
tasks:
# TASK 1
- name: "ensure ~user01/.ssh exists"
file:
path: ~user01/.ssh
state: directory
owner: user01
group: user01
mode: '0600'
# TASK 2
- name: create authorized_keys if it's not there
file:
path: "~user01/.ssh/authorized_keys"
state: touch
owner: user01
group: user01
mode: '0600'
access_time: preserve
modification_time: preserve
In this playbook, there are 2 plays, while play1 has 1 task, play2 with 2 tasks.
In a task, besides module information, other information can be added to describe how to call the module.
In a play, besides tasks, other information can be added to describe how to run tasks.
No comments:
Post a Comment