day16.4

Ansible流程控制

变量注册

	当absible的模块在运行之后,都会返回result结果,就像执行脚本,我们需要脚本给我们一些return返回值,这样才知道,上一步是否执行成功。但是,ansible的result并不会显示出来,所以,我们可以将这些返回值放到变量中,这样就能通过调用对应的变量名,从而获取到这些result,这种将模块的返回值写入到变量中的方法被称为变量注册
- hosts: web_group 
  tasks: 
  - name: 查看nginx目录 
    shell: "ls -l /etcinx" 
    register: xxx 
  - name: 获取注册的变量值 nginx目录返回记过 
  	debug: 
  	  msg: "{{ xxx }}"

只需要打印详细的结果

- hosts: web_group 
  tasks: 
  - name: 查看nginx目录 
    shell: "ls -l /etcinx" 
    register: xxx 
  - name: 获取注册的变量值 nginx目录返回记过 
  	debug: 
  	  msg: "{{ xxx.stdout_lines }}"

利用变量做判断

- hosts: web_group 
  tasks: 
  - name: 查看nginx目录 
    shell: "ls -l /etcinx" 
    register: xxx 
  - name: 
  	shell: cd /opt && rpm -Uvh *.rpm
  	when: xxx.rc != 0

facts缓存

	Ansible facts是在被管理追击上通过Ansible自动采集发现的变量。facts包含每台特定的主机信息。例:被控端的主机名、ip地址、系统版本、CPU数量等

facts缓存应用场景

1.根据主机cpu,设置nginx配置文件,cpu亲和
2.根据内存,配置MySQL的配置文件
3.根据IP地址,配置redis配置文件

gather_facts:False:# 关闭facts缓存


条件语句(判断)

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

Ansible获取主机名

# 主机名中,不包含'.',没有区别
ansible_hostname	# 包含'.' 只显示第一个'.' 前面的名字
ansible_fqdm		# 包含'.' 显示安装的主机名
不管是shell还是各大编程语言中,流程控制,条件判断这些都是必不可少的,在我们使用Ansible的过程中,条件判断的使用频率极其高。 例如: 1.我们使用不同的系统的时候,可以通过判断系统来对软件包进行安装。
centos 安装apche :yum install -y httpd
unbuntu 安装apache: apt-get install apache2
语法:
tasks:
  - name: "shut down Debian flavored systems"
  command: /sbin/shutdown -t now
  when: ansible_facts['os_family'] == "Debian"

tasks:
  - name: "shut down Debian flavored systems"
  command: apt-get install apache2
  when: ansible_os_family == "Ubuntu"
  
  - hosts: web_group
  tasks: 
  - name: 创建目录
    file:
      path: /opt/{{ ansible_default_ipv4.address }}
      state: directory

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

- hosts: rsync_nfs
tasks:
  - name: 安装rsync和nfs服务
    yum:
      name:
      - rsync
      - nfs-utils
    state: present
- name: 推送rsync配置文件
  template:
    src: /root/wordpress_ansible/rsync/rsyncd.conf
    dest: /etc
   when: ansible_hostname == 'backup
   
 # 多条件判断
 - hosts: rsync_nfs
   tasks:
     - name: 安装rsync和nfs服务
       yum:
        name:
          - rsync
          - nfs-utils
         state: present
      when: ansible_hostname == 'backup' or ansible_hostname == 'nfs'
- name: 推送rsync配置文件
  template:
    src: /root/wordpress_ansible/rsync/rsyncd.conf
    dest: /etc
  when: ansible_hostname == 'backup'
3.我们在源码安装nginx的时候,执行第二遍就无法执行了,此时我们就可以进行判断是否安装过。
- hosts: web_group
  tasks: 
  - name: 创建目录
    shell: "ls -l /etc/nginx"
    register: sb
  
  - name: 判断是否安装过nginx
    shell: 'cd /opt && rpm -Uvh *.rpm'
    when: sb.rc != 0
# 不使用and的多条件
tasks:
  - name: "shut down CentOS 6 systems"
  command: /sbin/shutdown -t now
  when:
  - ansible_facts['distribution'] == "CentOS"
  - ansible_facts['distribution_major_version']|int == 6
# 模糊匹配
- hosts: all
  tasks:
  - name: 推送nginx虚拟主机配置文件
    copy:
      src: /root/wordpress_ansible/nginx_php/blog.zls.com.conf
      dest: /etc/nginx/conf.d
# when: ansible_hostname == 'web01' or ansible_hostname == 'web02'
      when: ansible_hostname is match 'web*'
- name: 推送php配置文件
  copy:
    src: /root/wordpress_ansible/nginx_php/www.conf
    dest: /etc/php-fpm.d

playbook循环语句

在之前的学习过程中,我们经常会有传送文件,创建目录之类的操作,创建2个目录就要写两个le模块来创建,如果要创建100个目录,我们需要写100个le模块???妈耶~~ 当然不是,只要有循环即可,减少重复性代码。

列表循环

# 启动多个服务
数据类型:列表
for 循环列表类型

- hosts: web_group
  tasks: 
  - name: 创建目录
    service:
      name: "{{ item }}"
      state: started
    with_items:
      - nginx
      - php-fpm
    when: ansible_hostname is match 'web*'

字典循环

- hosts: all
  tasks:
  - name: 启动nginx 和 php
    service:
      name: "{{ item }}"
      state: stopped
    with_items:
     - nginx
     - php-fpm
   when: ansible_hostname is match 'web*'
- name: 推送nginx主配置文件、nginx虚拟主机配置文件和php配置文件
  template:
    src: "{{ item.src }}"
    dest: "{{ item.dest }}"
  with_items:
    - {src: "/root/wordpress_ansible/nginx_php/blog.zls.com.conf",dest:
"/etc/nginx/conf.d"}
    - {src: "/root/wordpress_ansible/nginx_php/nginx.conf",dest: "/etc/nginx"}
   when: ansible_hostname is match 'web*'

# 代码优化
```bash
- hosts: all
  tasks:
  - name: Create group
    group:
      name: "{{ user_group }}"
      gid: "{{ id }}"
  - name: cerate user
    user: 
      name: "{{ user_group }}"
      uid: "{{ id }}"
      group: "{{ id }}"
      shell: /sbin/nologin
      create_home: no
  - name: Install rsync
    yum:
      name: nfs-utils,rsync
      state: present
    when: ansible_hostname !=  'db01'
  - name: xxx
    copy: 
      src: /root/wordpress_ansible/rsync/rsyncd.conf
      dest: /etc/
    when: ansible_hostname == 'backup'
  - name: Creating a password file
    copy:
      content: rsync_backup:123
      dest: /etc/rsync.passwd
      mode: 0600
    when: ansible_hostname == 'backup'
  - name: create backup directory
    file: 
      path: /backup
      owner: "{{ user_group }}"
      group: "{{ user_group }}"
      state: directory
    when: ansible_hostname == 'backup'
  - name: start rsync
    service: 
      name: rsyncd
      state: started
      enabled: yes
    when: ansible_hostname == 'backup'

  - name: Creating a password file
    copy: 
      content: 123
      dest: /etc/rsync.passwd
      mode: 0600
    when: ansible_hostname == 'nfs'      

  - name: nfs Profile
    copy:
      content: /{{ nfs_dir }} 172.16.1.0/24(rw,sync,anonuid="{{ id }}",anongid="{{ id }},all_squash")
      dest: /etc/exports
    when: ansible_hostname == 'nfs' or ansible_hostname == 'backup'
  
  - name: create directory
    file: 
      path: /{{ nfs_dir }}
      owner: "{{ user_group }}"
      group: "{{ user_group }}"
      state: directory
    when: ansible_hostname == 'nfs' or ansible_hostname == 'backup'
  - name: start nfs
    service: 
      name: nfs
      state: started
      enabled: yes
    when: ansible_hostname == 'nfs' or ansible_hostname == 'backup'
  - name: upload
    unarchive: 
      src: /root/wordpress_ansible/nfs/2022.tgz
      dest: /data
      owner: www
      group: www
    when: ansible_hostname == 'nfs' or ansible_hostname == 'backup'

  - name: php nginx Install
    unarchive: 
      src: /root/wordpress_ansible/nginx_php/nginx_php.tgz
      dest: /opt/
    when: ansible_hostname is match 'web*'

  - name: 推送mysql文件
    copy: 
      src: "{{ item.src }}"
      dest: "{{ item.dest }}"
    with_items: 
      - {src: "/root/wordpress_ansible/mariadb/my.cnf",dest: "/etc/"}
      - {src: "/root/wordpress_ansible/mariadb/wp_ansible.sql",dest: "/opt/"}
    when: ansible_hostname == 'db01'

  - name: mariadb Install
    yum: 
      name: mariadb-server,MySQL-python
    when: ansible_hostname == 'db01'
  - name: start mariadb
    service:
      name: mariadb
      state: started
      enabled: True
    when: ansible_hostname == 'db01'
  - name: create a batabase
    mysql_db: 
      name: wordpress
      state: present
    when: ansible_hostname == 'db01'
  - name: grant mysql user
    mysql_user: 
      name: wp_user
      password: 123
      host: "172.16.1.%"
      priv: "wordpress.*:ALL"
      state: present
    when: ansible_hostname == 'db01'
  - name: import mysql
    mysql_db: 
      name: wordpress
      target: /opt/wp_ansible.sql
      state: import
    when: ansible_hostname == 'db01' 
  
- hosts: web_group
  tasks:
  - name: php nginx Install
    shell: "yum localinstall -y /opt/*.rpm"
  - name: nginx Profile
    template: 
      src: "{{ item.src }}"
      dest: "{{ item.dest }}"
    with_items: 
      - {src: "/root/wordpress_ansible/nginx_php/nginx.conf",dest: "/etc/nginx/"}
      - {src: "/root/wordpress_ansible/nginx_php/blog.jl.com.conf",dest: "/etc/nginx/conf.d/"}
  - name: php Profile
    copy: 
      src: /root/wordpress_ansible/nginx_php/www.conf
      dest: /etc/php-fpm.d/

  - name: create directory
    file: 
      path: /{{ web_dir }}
      owner: "{{ user_group }}"
      group: "{{ user_group }}"
      state: directory
  - name: download wordpress
    unarchive: 
      src: /root/wordpress_ansible/wordpress/wordpress.tgz
      dest: /{{ web_dir }}
      owner: "{{ user_group }}"
      group: "{{ user_group }}"
  - name: start nginx php
    service: 
      name: "{{ item }}"
      state: started
      enabled: yes
    with_items: 
      - nginx
      - php-fpm
  - name: nfs
    mount: 
      path: /{{ web_dir }}/wordpress/wp-content/uploads
      src: 172.16.1.31:/data
      fstype: nfs
      state: mounted

posted @ 2022-06-30 19:46  Gabydawei  阅读(34)  评论(0编辑  收藏  举报