Ansible 流程控制

2022/6/30 23:20:10

本文主要是介绍Ansible 流程控制,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Ansible 流程控制

条件语句(判断)

当满足什么条件时,就执行那些tasks
when 当...时

ansible获取主机名

# 主机名中,不包含'.'没有区别
ansible_hostname  # 包含'.'只显示第一个'.'前面的名字
ansible_fqdn      # 包含'.'显示完整的主机名

不管是shell还是各大编程语言中,流程控制,条件判断这些都是必不可少的,在我们使用Ansible的过程中,条件判断的使用率极高。例如:

# 1.我们是有不同的系统的时候,可以通过判断系统来对软件包进行安装。

centos安装apache:yum install iy httpd
unbuntu安装apache:apt-get install apache2

- hosts: all 
  tasks:
  - name: Debian 
    command: /sbin/shutdown -t now
    when: ansible_facts['os_family'] == "Debian"
  - name: ubuntu
    command: apt-get install apache2
    when: ansible_os_family == "ubuntu" 
    

- hosts: backups
  tasks:
    - name: 创建目录
      file:
        path: /tmp/{{ ansible_facts['default_ipv4']['address'] }}
        state: directory
- hosts: backups
  tasks:
    - name: 创建目录
      file:
      # setup模块,看到什么名字就写什么名字
      # path: /opt/{{ ansible_default_ipv4.address }}
      # 官方写法
        path: /tmp/{{ ansible_facts['default_ipv4']['address'] }}
        state: directory
    - name: 下载
      yum:
        name: 
          - rsync
          - nfs-utils
        state: present
        

# 2.在nfs和rsync安装过程中,客户端服务器不需要推送配置文件,之前都是写多play,会影响效率

- hosts: backups
  tasks:
    - name: 创建目录
      file:
        path: /tmp/{{ ansible_facts['default_ipv4']['address'] }}
        state: directory
    - name: 下载
      yum:
        name: 
          - rsync
          - nfs-utils
        state: present
    - name: 推送rsync配置文件
      copy:
        src: /root/rsyncd.conf
        dest: /etc/
      when: ansible_hostname == 'backup'
  
# 多条件判断
    - name: 下载rsync和nfs
      yum:
        name: 
          - rsync
          - nfs-utils
        state: present
      when: ansible_hostname == 'backup' or ansible_hostname == 'nfs'
    - name: 推送rsync配置文件
      copy:
        src: /root/rsyncd.conf
        dest: /etc/
      when: ansible_hostname == 'backup'
      
# 3.我们在源码安装nginx得时候,执行第二遍就无法执行,此时我们就可以进行判断是否安装过。

- hosts: webs
  tasks:
    - name: 查看nginx目录
      shell: 'ls -l /etc/nginx'
      register: xxx
    - name: 判断nginx是否安装如果没有则安装
      yum:
        name: nginx
      when: xxx.rc !=0 
      
# - name: 获取注册的变量 nginx目录返回记过
# debug:
#   msg: "{{ xxx }}"

# 判断中的且或非
and
or
!

# 不使用and的多条件
- name: "shut down Centos 6 systems"
      command: /sib/sutdown -t now
      when:
        - ansible_facts['distribution'] == "centos" 
        - ansible_facts['distribution_major_version']|int == 6  
        
# 模糊匹配

- hosts: all
  tasks:
  - name: 推送nginx虚拟主机配置文件
    copy:
        src: /etc/nginx/conf.d/wordpress.conf
        dest: /etc/nginx/conf.d/
    when: ansible_hostname is match 'web*'
  - name: 推送php配置文件
    copy:
      src: /etc/php-fpm.d/www.conf
      dest: /etc/php-fpm.d
    when: ansible_hostname is match 'web'

playbook循环语句

在之前的学习过程中,我们经常会用床上文件创建目录之类的操作,创建两个目录要写量个file模块,如果要创建100个目录就要写100个file模块 ,只要用循环即可,减少重复性的代码

列表循环

  - name: 启动nginx和php
    service:
      name: "{{ item }}"
      state: started
    with_items: 
      - nginx
      - php-fpm
    when: ansible_hostname == 'web02'

字典循环

- name: 推送nginxz主配置文件、nginx虚拟主机配置文件和php配置文件配置文件
    copy:
      src: "{{ item.src }}"
      dest: "{{ item.dest }}"
    with_items:
      - {src: "/etc/nginx/nginx.conf",dest: /etc/nginx/}
      - {src:  "/etc/php-fpm.d/www.conf",dest: /etc/php-fpm.d}
      - {src: "/etc/nginx/conf.d/wordpress.conf",dest: /etc/nginx/conf.d/}
    when: ansible_hostname is match 'web*'

优化作业

# 创建www用户
- hosts: all
  tasks:
    - name: group "{{ user }}"
      group:
        name: "{{ user }}"
        gid: "{{ id }}"
    - name: useradd "{{ user }}"
      user:
        name: "{{ user }}"
        uid: "{{ id }}"
        group: "{{ id }}"
        shell: /sbin/nologin
        create_home: false
# 下载nfs和rsync
    - name: 下载nfs和rsync
      yum:
        name: nfs-utils,rsync
      when: ansible_hostname == 'nfs' or ansible_hostname == 'backup'
# 配置nfs服务
    - name: 配置nfs
      copy:
        dest: /etc/exports
        content: "/data 172.16.1.0/24(rw,sync,anonuid=666,anongid=666,all_squash)"
      when: ansible_hostname == 'nfs'
    - name: 创建共享目录
      file:
        path: /data
        owner: "{{ user }}"
        group: "{{ user }}"
        state: directory
      when: ansible_hostname == 'nfs' 
    - name: 启动服务并加入开机自启
      service:
        name: nfs
        state: started
        enabled: true
      when: ansible_hostname == 'nfs' 
  # 客户端下载nfs
    - name: 客户端下载nfs
      yum:
        name: nfs-utils
      when: ansible_hostname is match "web*"
# 部署rsync配置
    - name: 部署rsync
      copy:
        src: /root/rsyncd.conf
        dest: /etc/
      when: ansible_hostname == 'backup' 
    - name: 创建服务端的密码文件
      copy:
        dest: /etc/rsync.pass
        mode: 0600
        content: "rsync_backup:123"
      when: ansible_hostname == 'backup' 
    - name: 创建备份目录
      file:
        path: /backup
        owner: "{{ user }}"
        group: "{{ user }}"
        state: directory
      when: ansible_hostname == 'backup' 
    - name: 启动rsync并加入到开机自启
      service:
        name: rsyncd
        state: started
        enabled: yes
      when: ansible_hostname == 'backup' 
# 部署wordpress
    - name: 将nginx和php压缩包解压至客户端
      unarchive:
        src: /root/phpnginx/nginx_php.tgz
        dest: /opt/
      when: ansible_hostname is match "web*"
    - name: 判断是否安装nginx
      shell: "ls -l /etc/nginx"
      register: xxx
      when: ansible_hostname is match "web*"
      ignore_errors: yes
    - name: 安装nginx和php
      shell: "cd /opt/ && rpm -Uvh *.rpm"
      when: (ansible_hostname is match "web*") and xxx.rc != 0
    - name: 推送nginxz主配置文件、nginx虚拟主机配置文件和php配置文件配置文件
      copy:
        src: "{{ item.src }}"
        dest: "{{ item.dest }}"
      with_items:
        - {src: "/etc/nginx/nginx.conf",dest: "/etc/nginx/"}
        - {src:  "/etc/php-fpm.d/www.conf",dest: "/etc/php-fpm.d/"}
        - {src: "/etc/nginx/conf.d/wordpress.conf",dest: "/etc/nginx/conf.d"}
      when: ansible_hostname is match "web*"
    - name: 创建站点目录
      file:
        path: /movie/
        state: directory
        owner: "{{ user }}"
        group: "{{ user }}"
      when: ansible_hostname is match "web*"
    - name: 将压缩包解压到站点目录
      unarchive:
        src: /root/wordpress/wordpress.tgz
        dest: /movie/
        group: "{{ user }}"
        owner: "{{ user }}"
      when: ansible_hostname is match "web*"
    - name: 启动nginx
      service:
        name: "{{ item }}"
        state: started
        enabled: true
      with_items: 
        - nginx
        - php-fpm
      when: ansible_hostname is match "web*"
# 部署数据库
    - name: 下载mysql
      yum:
        name: mariadb-server
      when: ansible_hostname == 'db01'
    - name: 启动mysql并加入开机自启
      service:
        name: mariadb
        state: started
        enabled: True
      when: ansible_hostname == 'db01'
    - name: 修改配置文件
      copy: 
        src: /etc/my.cnf
        dest: /etc/
      when: ansible_hostname == 'db01'
    - name: 安装数据库需要的模块
      yum:
        name: MySQL-python
      when: ansible_hostname == 'db01'
    - name: 创建wordpress用户
      mysql_user:
        name: wp
        password: "123"
        host: "172.16.1.%"
        priv: "*.*:ALL"
        state: present
      when: ansible_hostname == 'db01'
    - name: 创建wordpress库
      mysql_db: 
        name: wordpress
        state: present
      when: ansible_hostname == 'db01'
    - name: 将数据cp到db01
      copy:
        src: /root/wordpress/wordpress.sql
        dest: /opt 
      when: ansible_hostname == 'db01'
    - name: 将数据导入数据库
      mysql_db: 
        name: wordpress
        target: /opt/wordpress.sql
        state: import
      when: ansible_hostname == 'db01'
- hosts: webs
  tasks:
    - name: nfs挂载目录
      mount:
        path: /movie/wordpress/wp-content/uploads
        src: 172.16.1.31:/data
        fstype: nfs
        state: mounted
      when: ansible_hostname is match "web*"



这篇关于Ansible 流程控制的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程