0
点赞
收藏
分享

微信扫一扫

3、Ansible高级功能--loop、until循环,with_lines逐行处理,条件判断when等

ansible的高级功能
1、loop循环

迭代:当有需要重复性执行的任务时,可以使用迭代机制

范例:初始化安装软件包

--- 
- hosts:webservers 
  remote_user: root   
  tasks    
    - name: install some packages     
      yum: name={{ item }} state=present     
      with_items:       
        - nginx       
        - memcached       
        - php-fpm

2、until循环

#until为false时才会执行循环,为true则退出循环
cat until.yml
- Hosts: localhost 
  gather _ facts: false 
  
  tasks: 
    - debug: msg = "until" 
      until: false 
      retries: 3 #默认值即为3次 
      delay: 1

3、with_lines逐行处理

cat with_lines.yml 
- hosts: localhost 
  tasks:    
    - debug: msg={{ item }}     
    with_lines: ps aux

4、条件判断when

范例: 判断OS版本

cat when.yml 
- hosts: all   
  tasks:    
    - name: install httpd     
      yum:
        name: "httpd"     
      when:
        - ansible_distribution_file_variety  == "RedHat"    
    - name: install package     
      apt:       
        name: "apache2"     
      when:        
        - ansible_distribution_file_variety == "Debian"

5、分组block

当想在满足同样条件下,执行多个任务时,就需要分组.而不再针对每个任务都是用 when

范例:

cat block.yml  
- hosts: localhost   
  tasks:    
    - block:        
      - debug: msg="first"        
      - debug: msg="second"     
      when:        
        - ansible_facts['distribution'] == "CentOS"       
        - ansible_facts['distribution_major_version'] == "8"

6、changed_when

1、关闭 changed 状态 
当确定某个task不会对被控制端做修改时但执行结果却显示是黄色的changed状态 
可以通过 changed_when: false 关闭changed状态

cat test_changed.yml
- hosts: webservers 
  tasks: 
    - name: check sshd service 
      shell: ps aux | grep sshd changed_when: false #关闭changed状态
      
2、利用changed_when检查task返回结果
changed_when 检查task返回结果,决定是否继续向下执行

cat test_changed_when.yml 
- hosts: webservers 
  tasks:   
    - name: install nginx     
      yum: name=nginx   
    - name: config file     
      template: src="nginx.conf.j2" dest="/etc/nginx/nginx.conf"     
      notify: restart nginx   
    - name: check config     
      shell: /usr/sbin/nginx -t     
      register: check_nginx_config     
      changed_when:       
        - check_nginx_config.stdout.find('successful')#如果执行结果中有successful字 符串,则继续执行,如果没有则停止向下执行
        - false     #nginx -t 每次成功执行是changed状态,关闭此changed状态
    - name: start service    
      service: name=nginx state=started enabled=yes 
  handlers:   
    - name: restart nginx     
      service: name=nginx state=restarted

7、滚动执行

管理节点过多导致的超时问题解决方法 
默认情况下,Ansible将尝试并行管理playbook中所有的机器.对于滚动更新用例,可以使用serial关键 字定义Ansible一次应管理多少主机,还可以将serial关键字指定为百分比,表示每次并行执行的主机数占总数的比例

vim test_serial.yml 
- hosts: all 
  serial: 2   #每次只同时处理2个主机,将所有task执行完成后,再选下2个主机再执行所有task,直至所有主机
  gather_facts: False 
  
  tasks:   
    - name: task one 
      command: hostname   
    - name: task two    
      command: hostname

8、委派至其它主机执行

利用委托技术,可以在非当前被控主机的其它主机上执行指定操作 
注意: 当前执行的被管理端主机需要实现到被委派主机的ssh key验证才能实现委派 

范例: 将任务委派给指定的主机执行
# 在10.0.0.8上执行hostname -I, 而非当前主机localhost 
cat delegate.yml 
- hosts: localhost 
  tasks: 
    -name: show ip address 
      command: hostname-I 
      delegate_to: 10.0.0.8   #指定当前任务被委派给的目标主机
      delegate_facts: true    #收集被委派的目标主机的facts信息

范例: 将任务被委派给控制端ansible主机执行
#在本地执行ifconfig,而非10.0.0.8
cat delegate2.yml 
- hosts: 10.0.0.8   
  tasks:  
    - name: show ip address     
    delegate_to: localhost  #被委派给控制端ansible主机执行
    run_once: true          #委派任务只执行一次

9、只执行一次

利用 run_once 指令可以只执行一次,而非在所有被控主机都执行

cat run_once.yml 
- hosts: webservers 
  tasks:    
    - command: hostname    
      run_once: true

10、Yaml 文件的相互调用

1、include
利用include 或 include_tasks 可以在某个task中调用其它的只有task内容的yaml文件
范例:
cat a.yml
- hosts: webservers   
  tasks:   
    - name: run a job     
      command: wall run a job  
    - name: excute b.yml     
      include: b.yml   #调用另一个yaml文件
      #include_tasks: b.yaml  #另一种写法
      
cat b.yml 
- name: run b job 
  command: wall run b job
  
  
2、import_playbook
还可以将多个包含完整内容的yml文件由一个yml统一调用
cat main.yml 
- import_playbook: tasks1.yml 
- import_playbook: tasks2.yml 

cat tasks1.yml 
--- 
- hosts: webservers 
  tasks:    
    - name: run task1 job     
      command: wall run task1 job 
      
cat tasks2.yml 
--- 
- hosts: dbservers 
  tasks:   
    - name: run task2 job    
      command: wall run task2 job 
      
ansible-playbook main.yml

举报

相关推荐

0 条评论