自动化运维之---Ansible Playbook语法介绍(一)

Playbook语法简介

  playbook简单语法介绍:

 1 ---
 2 - hosts: webservers #选择hosts,在invonter配置的
 3   vars:    #设置变量
 4     http_port: 80
 5     max_clients: 200
 6   remote_user: root  #远端执行权限
 7   tasks: 
 8     - name: ensure apache is at the latest version
 9       yum: pkg=httpd state=latest
10     - name: write the apache config file
11       templete: src=/srv/httpd.j2 dest=/etc/httpd.conf
12   notify: restart apache
13     - name: ensure apache is running
14       service: name=httpd state=started
15   handlers:
16      - name: restart apache
17        service: name=httpd state=restarted

   handlers当某个任务需要依赖其他任务怎么办?

  • 可以通过handlers定义一组任务
  • 仅当某个任务触发(notify)handlers时才执行相应的任务
  • 如果有多个notify触发执行handlers任务,也仅执行一次
  • 仅当任务的执行状态为changed时handlers任务才执行
  • handlers任务在所有其他任务都执行后才执行
 1 ---
 2 - hosts: test
 3   tasks:
 4     - name: create dir
 5       file:
 6         path: /tmp/parents/subdir/
 7         state: directory
 8       notify: touch file
 9   handlers:
10     - name: touch file
11       file:
12         path: /tmp/parents/subdir/new.txt
13         state: touch
handlers.yml

   when条件判断

  • when可以定义判断条件,条件为真时才执行某个任务
  • 常见条件操作符如下:==、!=、>、<、>=、<=
  • 多个条件可以使用and或or分隔
  • when表达式中调用变量不要使用{{}}

  示例:

  远程主机剩余内存不足700M,则关闭NetworkManager服务

1 ---
2 - hosts: test
3   tasks:
4     - name: check memory size
5       service:
6         name: NetworkManager
7         state: stopped
8       when: ansible_memfree_mb < 700
when.yml

    判断操作系统是Redhat8则创建测试文件

  > 支持多行输入,不保留换行符

 1 ---
 2 - hosts:
 3   tasks:
 4     - name: touch a file
 5       file:
 6         path: /tmp/when.txt
 7         state: touch
 8       when: >
 9         ansible_distribution == "RedHat"
10           and
11         ansible_distribution_major_version == "8"
when2.yml

  block任务块

  使用block可以将多个任务合并为一个组

 1 ---
 2 - hosts: test
 3   tasks:
 4     - name: define a group of tasks
 5       block:
 6         - name:install httpd
 7           yum: 
 8             name: httpd
 9             state: present
10         - name: start httpd
11           service:
12             name: httpd
13             state: started
14       when: ansible_distribution == "RedHat"
block.yml
  • rescue定义block任务执行失败时要执行的其他任务
  • always定义无论block任务是否成功,都要执行的任务
 1 ---
 2 - hosts:
 3   tasks:
 4     - block:
 5         - name: touch a file test1.txt
 6             file:
 7               path: /tmp/test.txt
 8               state: touch
 9       rescue:
10         - name : touch a file test2.txt
11             file:
12               path: /tmp/test2.txt
13               state: touch
14       always:
15         - name: touch a file test3.txt
16             file;
17               path: /tmp/test3.txt
18               state: touch
block2.yml

  loop循环

  很多任务都在用相同的模块?使用loop循环避免重复

  示例1

 1 ---
 2 - hosts:
 3   tasks:
 4     - name: mkeir mutil directory
 5         file:
 6           path: /tmp/{{item}}     #注意,itme是关键字
 7           state: directory
 8         loop:
 9           - School
10           - Legend
11           - Life
loop.yml

  示例2

 1 ---
 2 - hosts:
 3   tasks:
 4     - name: create mutil user
 5         user:
 6           name: "{{item.iname}}"
 7           password: "{{item.ipass | password_hash('sha512')}}"
 8         loop:
 9           - {iname:'term',ipass:'123456'}
10           - {iname:'amy',ipass:'654321'} 
loop2.yml

   playbook语法检查

1 # ansible-playbook group_proxy-install.yml --syntax-check

  playbook语法中定义变量,在playbook中使用变量,使用{{ 变量名 }}来使用变量

1 ---
2 - hosts: test
3   ignore_errors: yes
4   gather_facts: false
5   vars:
6     linux_dir: /opt
7     linux_package: /ansible/packages
8     ansible_package: /opt/ansiblepkg
View Code

  

 

 

 

 

 

 

 

  

posted @ 2022-07-27 11:35  摩天居士-谢烟客  阅读(128)  评论(0编辑  收藏  举报