zoukankan      html  css  js  c++  java
  • Ansible流程控制

    Ansible流程控制


    数据库操作问题:

    • 数据库的操作问题,python需要依耐的模块MySQL-python 。

    数据库的操作

     # 设置root的密码在,root的密码设置之后,创建用户和创建数据库的操作都需要登陆使用
      
      - login_user: 'root'
      - login_password: '123'
      - login_host: 'localhost'
      # 三个字段登陆。
      
     - name: set root pas
          mysql_user:
            name: root
            password: "123"
            host: "localhost"
            priv: '*.*:ALL'
            state: present  
          when: ansible_fqdn == "db01"
    
        - name: create database
          mysql_db:
            login_user: 'root'
            login_password: '123'
            login_host: 'localhost'
            name: wp_db
            state: present
          when: ansible_fqdn == "db01"
    
        - name: create admin
          mysql_user:
            login_user: 'root'
            login_password: '123'
            login_host: 'localhost'
            name: wp
            password: "123"
            host: "%"
            priv: '*.*:ALL' 
            state: present
          when: ansible_fqdn == "db01"
    
    # 数据库有密码,想要操作数据库,得先连接数据库(登录数据库)
    login_user: root
    login_password: '123'
    login_host: localhost
    login_port: 3306
    


    导出数据库

    [root@db01 ~]# mysqladmin -uroot password "123"
    
    [root@db01 ~]# mysqldump wp_db -uroot -p123 > wp_db.sql
    
    # 导出所有数据库
    [root@db01 ~]# mysqldump -A -uroot -p123 >backup.sql
    
    grant all on wp.* to wp_user@'localhost' identified by '111';
    

    导入

    mysql -u用户名 -p 数据库名 < 数据库名.sql
    
    [root@db01 ~]# mysql -uroot -p123 < all_databases_backup.sql
    

    判断语句

    主机清单

    [web_group]
    web01 ansible_ssh_host=10.0.0.7
    web02 ansible_ssh_host=10.0.0.8
    
    

    使用when,最大的好处就是,不用重复的收集主机变量,相对于使用多个play的方式。

    # web_group中有两台主机web01和web02,使用when语句,只有在web01上安装httpd
    [root@m01 ~/ansible]# vi when.yml 
    - hosts: web_group
      tasks:
        - name: Install httpd
          yum:
            name: httpd
            state: present
          when: ansible_fqdn == "web01"
          
    # 主机名变量官方的推荐写法
    [root@m01 ~/ansible]# vi when.yml 
    - hosts: web_group
      tasks:
        - name: Install httpd
          yum:
            name: httpd
            state: present
          when: ansible_facts['fqdn'] == "web01"
    
    # 此种方式表示的是,通过对不同主机操作系统的判断,来安装不同版本的apache
    [root@m01 ~/ansible]# cat when.yml 
    - hosts: web_group
      tasks:
        - name: Install CentOS Httpd
          yum:
            name: httpd
            state: present
          when: ansible_distribution == "CentOS"
    
        - name: Install Ubuntu Httpd
          yum:
            name: apache2
            state: present
          when: ansible_facts['os_family'] == "Ubuntu"
    
    

    还可以使用括号对条件进行分组,多条件的判断

    tasks:
      - name: "shut down CentOS 6 and Debian 7 systems"
        command: /sbin/shutdown -t now
        when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "6") or (ansible_facts['distribution'] == "Debian" and ansible_facts['distribution_major_version'] == "7")
        
    # 变量加上逻辑运算来判断,上面语句表示为操作系统为CentOS 6的和Debian 7版本的关机
    

    也可以指定多条件为列表

    tasks:
      - name: "shut down CentOS 6 systems"
        command: /sbin/shutdown -t now
        when:
          - ansible_facts['distribution'] == "CentOS"
          - ansible_facts['distribution_major_version'] == "6"
          
    # 表示CentOs 6的操作系统关闭。
    

    判断语句的模糊匹配

    - hosts: all
      tasks:
        - name: Install Rsync Server
          yum:
            name: rsync
            state: present
          when: ansible_fqdn == 'backup' or ansible_fqdn == 'nfs'
    
        - name: Configure Rsync Conf
          copy:
            src: /root/ansible/rsync/rsyncd.conf
            dest: /etc/rsyncd.conf
          when: ansible_fqdn == 'backup'
    
        - name: Install Nginx
          yum:
            name: nginx
            state: present
          when: ansible_fqdn is match 'web*'
          # 模糊匹配,表示主机名是web开头的才安装nginx
    

    条件运算

    tasks:
      - shell: echo "only on Red Hat 6, derivatives, and later"
        when: ansible_facts['os_family'] == "RedHat" and ansible_facts['lsb']['major_release']|int >= 6
        
    # 多条件,操作系统是RedHat且版本大于6的打印,数学运算比较的是整形,所以要在后面使用"|int",把字符型转为整形
    

    通过变量的方式来安装本地的rpm包

    [root@m01 ~/ansible]# cat yum.yml 
    - hosts: web01
      tasks:
        - name: YUM install
          yum:
            name: "{{ var_packages }}"
            state: present
          vars:
            var_packages:
              - /root/nginx_php/mod_php71w-7.1.33-1.w7.x86_64.rpm
              - /root/nginx_php/nginx-1.18.0-1.el7.ngx.x86_64.rpm
              - /root/nginx_php/php71w-cli-7.1.33-1.w7.x86_64.rpm
              - /root/nginx_php/php71w-common-7.1.33-1.w7.x86_64.rpm
              - /root/nginx_php/php71w-devel-7.1.33-1.w7.x86_64.rpm
              - /root/nginx_php/php71w-embedded-7.1.33-1.w7.x86_64.rpm
              - /root/nginx_php/php71w-fpm-7.1.33-1.w7.x86_64.rpm
              - /root/nginx_php/php71w-gd-7.1.33-1.w7.x86_64.rpm
              - /root/nginx_php/php71w-mbstring-7.1.33-1.w7.x86_64.rpm
              - /root/nginx_php/php71w-mcrypt-7.1.33-1.w7.x86_64.rpm
              - /root/nginx_php/php71w-mysqlnd-7.1.33-1.w7.x86_64.rpm
              - /root/nginx_php/php71w-opcache-7.1.33-1.w7.x86_64.rpm
              - /root/nginx_php/php71w-pdo-7.1.33-1.w7.x86_64.rpm
              - /root/nginx_php/php71w-pear-1.10.4-1.w7.noarch.rpm
              - /root/nginx_php/php71w-pecl-igbinary-2.0.5-1.w7.x86_64.rpm
              - /root/nginx_php/php71w-pecl-memcached-3.0.4-1.w7.x86_64.rpm
              - /root/nginx_php/php71w-pecl-mongodb-1.5.3-1.w7.x86_64.rpm
              - /root/nginx_php/php71w-pecl-redis-3.1.6-1.w7.x86_64.rpm
              - /root/nginx_php/php71w-process-7.1.33-1.w7.x86_64.rpm
              - /root/nginx_php/php71w-xml-7.1.33-1.w7.x86_64.rpm
    
    

    通过注册变量的方式来控制流程

    [root@m01 ~/ansible]# vi create.yml 
    - hosts: web01
      tasks:
        - name: register var
          shell: "ls /root/nginx_php"
          register: directory_info
          ignore_errors: yes
    	# 忽略错误
        - name: create web01
          shell: "mv /root/nginx_php /root/web01"
          when: directory_info.rc == 0
          
    # 通过注册变量的方式来判断文件是否存在,然后做出相应的动作。
    

    循环语句

    [root@m01 ~/ansible]# cat yum.yml 
    - hosts: web01
      tasks:
        - name: YUM install
          yum:
            name: /root/nginx_php/{{ item }}
            state: present
          with_items:
            - mod_php71w-7.1.33-1.w7.x86_64.rpm
            - nginx-1.18.0-1.el7.ngx.x86_64.rpm
            - php71w-cli-7.1.33-1.w7.x86_64.rpm
            - php71w-common-7.1.33-1.w7.x86_64.rpm
            - php71w-devel-7.1.33-1.w7.x86_64.rpm
            - php71w-embedded-7.1.33-1.w7.x86_64.rpm
            - php71w-fpm-7.1.33-1.w7.x86_64.rpm
            - php71w-gd-7.1.33-1.w7.x86_64.rpm
            - php71w-mbstring-7.1.33-1.w7.x86_64.rpm
            - php71w-mcrypt-7.1.33-1.w7.x86_64.rpm
            - php71w-mysqlnd-7.1.33-1.w7.x86_64.rpm
            - php71w-opcache-7.1.33-1.w7.x86_64.rpm
            - php71w-pdo-7.1.33-1.w7.x86_64.rpm
            - php71w-pear-1.10.4-1.w7.noarch.rpm
            - php71w-pecl-igbinary-2.0.5-1.w7.x86_64.rpm
            - php71w-pecl-memcached-3.0.4-1.w7.x86_64.rpm
            - php71w-pecl-mongodb-1.5.3-1.w7.x86_64.rpm
            - php71w-pecl-redis-3.1.6-1.w7.x86_64.rpm
            - php71w-process-7.1.33-1.w7.x86_64.rpm
            - php71w-xml-7.1.33-1.w7.x86_64.rpm
    # 通过循环的方式来安装包
    

    字典循环

    - 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" }
            
    # 利用列表和字典组合,会循环的访问列表里面的字典,并取出里面的key
    # 再配合判断语句的使用来实现对不同主机配置文件的推送
    
  • 相关阅读:
    专职DBA-MySQL体系结构与基本管理
    JSON
    MIME类型
    文件上传下载
    response常用的方法
    2020.11.27小记
    HTTP请求状态码
    1561. Maximum Number of Coins You Can Get
    1558. Minimum Numbers of Function Calls to Make Target Array
    1557. Minimum Number of Vertices to Reach All Nodes
  • 原文地址:https://www.cnblogs.com/gshelldon/p/13375508.html
Copyright © 2011-2022 走看看