ansible-palybooks

ansible-playbooks

如果用模块形式一般有幂等性,如果用shell或者command没有幂等性
playbooks相当于是shell脚本,可以把要执行的任务写到文件当中,一次执行,方便调用
tasks:一个task相当于是一个play
varibles: 变量,一定定义,多处调用
template:模板,可以区分不同主机的特点
handlers:触发器,依赖于前一个任务,前一个任务如果执行改变,那么就会触发handlers

定义playbook任务:
- hosts: testhosts
remote_user: root
tasks:
- name: copy httpd.conf
copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
- name: restart httpd
service: name=httpd state=restarted
打标签:
-t:执行指定tag标签任务
--skip-tags:执行--skip-tags以外的任务
执行:
ansible-playbook xxx.yaml -t TAG
ansible-playbook xxx.yaml --skip-tags TAG

定义带tag标签的yaml文件:
- hosts: testhosts
remote_user: root
tasks:
- name: "touch file"
shell: echo {{ ansible_all_ipv4_addresses }} > a.txt
tags:
- tag3
- name: "date"
shell: date >> a.txt
tags:
- tag1
- tag2
include:如果有多个任务要执行,写在一个yaml文件里就显得太臃肿了,而且阅读性也比较低,这个时候可以用include关键字把多个任务联合到1个yaml文件里,方便调用
例子:比如有2个文件,a.yaml和b.yaml,那么我们可以把a.yaml,b.yaml联合到一个exec.yaml文件中执行
a.yaml的内容如下:
- name: "touch file1"
shell: echo 'hello1' >> file1.txt
b.yaml的内容如下:
- name: "touch file"
shell: echo 'hello2' >> file2.txt
exec.yaml的内容如下:
- hosts: testhosts
remote_user: root
tasks:
- include_tasks: a.yaml
- include_tasks: b.yaml

定义变量:
在yaml文件当中传入模板变量
{{变量名}}
第一种:
声明变量只需要声明一次,定义变量可以定义多次
vars:
- file: httpd.conf

第二种:
vim /etc/ansible/hosts
[testhosts:vars]
file=httpd.conf
packages=tree
第三种
执行playbook文件时候给与变量 --extra-vars,--extra-vars可以出现一次或者多次
ansible-playbook test.yaml --extra-vars "touch_file=test.txt"


注册变量:
register注册变量:把date命令输出的结果赋予给date_output
- hosts: 192.168.254.10
remote_user: root
tasks:
- name: get date
command: date
register: date_output
- name: echo date_output
shell: "echo {{date_output.stdout}}>/tmp/a.txt"

when语句:
when条件语句:可以根据setup显示出客户端信息为依据来判断
- hosts: 192.168.254.12
remote_user: root
tasks:
- name: echo date_output
shell: "touch /tmp/a.txt"
when: ansible_distribution=='CentOS' and ansible_distribution_major_version=='8'
异常处理:
ignore_errors:如果任务出错,直接跳过,不会影响其他任务
- hosts: 192.168.254.12
remote_user: root
tasks:
- name: add several user
command: touch1 a.txt
ignore_errors: yes

循环语句:
第一种:
{{ item }}:循环创建
- hosts: 192.168.254.12
remote_user: root
tasks:
- name: add many users
user: name={{ item }} state=present
with_items:
- user1
- user2
- user3
- user4
第二种:
- hosts: 192.168.254.12
remote_user: root
tasks:
- name: add several user
user: name={{item.name}} state=present groups={{item.groups}}
with_items:
- { name: 'testuser1', groups: 'wheel'}
- { name: 'testuser2', groups: 'root'}
触发器:
handlers:如果执行的任务被改变那么会触发handlers的任务,notify下面的-引导的列表可以为一个或者多个,handlers要定义在最下方
- hosts: testhosts
remote_user: root
tasks:
- name: copy httpd.conf
copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
- restarted httpd service

handlers:
- name: restarted httpd service
service: name=httpd state=restarted

模板拷贝:
1.copy模块替换成template
2.vim httpd.conf编辑要拷贝的文件,把不同的地方定义成变量形式{{变量名}}
3.vim /etc/ansible/hosts 在主机后面定义变量的值:变量名=变量值
template,用来区分不同客户端上的特性
- hosts: testhosts
remote_user: root
tasks:
- name: copy httpd.conf
template: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
- restarted httpd service
handlers:
- name: restarted httpd service
service: name=httpd state=restarted

猜你喜欢

转载自www.cnblogs.com/lxwei0101/p/11703719.html