欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

ansible 流程控制

程序员文章站 2023-04-04 15:13:43
ansible 流程控制 使用when判断主机名 2.使用when判断系统 3.使用when判断系统版本 4.使用注册变量对返回值进行判断 ansible循环语句 1.with_items 2.变量循环 3.字典循环 ansible handlers(触发器) 注意: 1.无论多少个task通知了相 ......

ansible 流程控制

使用when判断主机名

- hosts: rsync_server
  tasks:

    - name: install rsyncd server
      yum:
        name: rsync
        state: present

    - name: config rsyncd conf
      copy:
        src: ./rsyncd.j2
        dest: /etc/rsyncd.conf
        owner: root
        group: root
        mode: 0644
      when: ansible_fqdn == 'backup'

    - name: create dir
      file:
        path: /backup
        state: directory
        owner: www
        group: www
        mode: 0755
        recurse: yes
      when: ansible_fqdn == 'backup'


    - name: create passwd file
      copy:
        content: "rsync_backup:123"
        dest: /etc/rsync.passwd
        owner: root
        group: root
        mode: 0600
      when: ansible_fqdn == 'backup'

#单条件判断
    - name: start rsyncd
      systemd:
        name: rsyncd
        state: started
        enabled: yes
      when: ansible_fqdn == 'backup'

#多条件判断,使用小括号分组
    - name: copy shell
      template:
        src: ./backup.sh
        dest: /root
      when: (ansible_fqdn == 'web01') or (ansible_fqdn == 'web02')

#多条件判断,使用list列表形式
    - name: copy shell
      template:
        src: ./backup.sh
        dest: /root
      when:
        - ansible_fqdn == 'web01'
        - ansible_fqdn == 'web02'
        
#多条件判断,使用is match 支持通配符
    - name: add crontab
      cron:
        name: "backup"
        minute: "00"
        hour: "01"
        job: "/bin/sh /root/backup.sh &>/dev/null"
      when: ansible_fqdn is match 'web*'

2.使用when判断系统

- hosts: webs
  tasks:
    - name: install centos apache
      yum:
        name: httpd
        state: present
      when: ansible_distribution == 'centos'

    - name: install ubuntu apache
      apt:
        name: apache2
        state: present
      when: ansible_distribution == 'ubuntu'

3.使用when判断系统版本

- hosts: webs
  tasks:
    - name: start centos6 httpd
      shell: "/etc/init.d/httpd start"
      when: ansible_distribution_major_version == '6'

    - name: start centos7 httpd
      shell: "systemctl start httpd"
      when: ansible_distribution_major_version == '7'

4.使用注册变量对返回值进行判断

- hosts: web_group
  tasks:
    - name: check httpd server
      command: systemctl is-active httpd
      ignore_errors: yes
      register: check_httpd

    - name: debug outprint
      debug: var=check_httpd

    - name: httpd restart
      service:
        name: httpd
        state: restarted
      
      when: check_httpd.rc == 0
    - name: pan duan rpm bao
      shell: "rpm -qa|grep php"
      register: check_php

    - name: install php
      shell: "cd /usr/local/src && rpm -uvh *rpm"
      when: check_php.rc != 0

ansible循环语句

1.with_items

    - name: start php and nginx
      systemd:
        name: "{{ item }}"
        state: started
        enabled: yes
      with_items:
        - nginx
        - php-fpm

2.变量循环

- name: ensure a list of packages installed
  yum:
    name: "{{ packages }}"
  vars:
    packages:
    - httpd
    - httpd-tools

3.字典循环

- hosts: web_group
  tasks:
    - name: copy conf and code
      copy:
        src: "{{ item.src }}"
        dest: "{{ item.dest }}"
        mode: "{{ item.mode }}"
      with_items:
        - { src: "./httpd.conf", dest: "/etc/httpd/conf/", mode: "0644" }
        - { src: "./upload_file.php", dest: "/var/www/html/", mode: "0600" }
    - name: tar php and nginx and wordpress
      unarchive:
        src: "{{ item.src }}"
        dest: "{{ item.dest }}"
        owner: "{{ item.user }}"
        group: "{{ item.user }}"
        copy: yes
      with_items:
        - { src: "./php.tgz", dest: "/usr/local/src", user: "root" }
        - { src: "./nginx-1.16.0.tar.gz", dest: "/root", user: "root" }
        - { src: "./wordpress.tgz", dest: "/code", user: "www" }

ansible handlers(触发器)

    - name: scp nginx shell conf
      copy:
        src: "{{ item.src }}"
        dest: "{{ item.dest }}"
      with_items:
        - { src: "./nginx.service", dest: "/usr/lib/systemd/system" }
        - { src: "./nginx.conf", dest: "/usr/local/nginx/conf/" }
        - { src: "./www.drz.com.conf", dest: "/usr/local/nginx/conf/conf.d" }
      #添加触发器
      notify: 
        - reload nginx
        - reload php

  handlers:
    - name: reload nginx
      systemd:
        name: nginx
        state: reloaded
    - name: reload php
      systemd:
        name: php
        state: reloaded

注意:

1.无论多少个task通知了相同的handlers,handlers仅会在所有tasks结束后运行一次。

2.handlers只有在其所在的任务被执行时,才会被运行;如果一个任务中定义了notify调用handlers,但是由于条件判断等原因,该任务未被执行,那么handlers同样不会被执行。

3.handlers只会在每一个play的末尾运行一次;如果想在一个playbook中间运行handlers,则需要使用meta模块来实现。例如: -meta: flush_handlers。

4.如果一个play在运行到调用handlers的语句之前失败了,那么这个handlers将不会被执行。我们可以使用meta模块的--force-handlers选项来强制执行handlers,即使handlers所在的play中途运行失败也能执行。

5.不能使用handlers替代tasks

playbook复用include

1.只调用task include_tasks

[root@m01 web]# vim install_nginx.yml 

- name: install nginx
  yum:
    name: nginx
    state: present

[root@m01 web]# vim conf_nginx.yml 
- name: conf nginx
  copy:
    src: ./www.drz.com.conf
    dest: /etc/nginx/conf.d
  notify: reload nginx


[root@m01 web]# vim main.yml 
- hosts:
  tasks:
    - include_tasks: ./install_nginx.yml
    - include_tasks: ./conf_nginx.yml
  handlers:
    - name: reload nginx
      systemd:
        name: nginx
        state: reloaded

[root@m01 m01]# ansible-playbook tag.yml --list-tags
[root@m01 m01]# ansible-playbook tag.yml -t httpd_server
[root@m01 m01]# ansible-playbook tag.yml -t install_httpd,confiure_httpd
[root@m01 m01]# ansible-playbook tag.yml --skip-tags httpd_server

2.直接复用写好的yml文件

  • 旧版:include
  • 新版:import_playbook
  • 在saltstack中,叫做top file入口文件
- import_playbook: ./lnmp.yml
- import_playbook: ../mariadb/mysql.yml

示例一:

[root@m01 m01]# cat task.yml 
- hosts: web_group
  vars:
    - http_port: 8080

  tasks:
    - include_tasks: task_install.yml
    - include_tasks: task_configure.yml
    - include_tasks: task_start.yml

  handlers:
    - name: restart httpd server
      systemd:
        name: httpd
        state: restarted

[root@m01 m01]# cat task_install.yml 
- name: install http server
  yum:
    name: httpd
    state: present

[root@m01 m01]# cat task_configure.yml 
- name: configure httpd server
  template:
    src: ./httpd.j2
    dest: /etc/httpd/conf/httpd.conf
  notify: restart httpd server

[root@m01 m01]# cat task_start.yml 
- name: start httpd server
  service:
    name: httpd
    state: started
    enabled: yes

示例二:

- include: httpd.yml
- include: nfs.yml
- include: rsync.yml

示例三:

- include: httpd.yml
- include: nfs.yml
- include: rsync.yml