其他分享
首页 > 其他分享> > Ansible流程控制

Ansible流程控制

作者:互联网

Ansible流程控制

1.条件语句(判断)

	在各大编程语言中,流程控制、条件判断都是必不可少的,在使用Ansible的过程中,条件判断使用频率高。条件判断通俗点讲就是当满足什么条件时就执行哪些语句

2.ansible获取主机名

ansible_hostname:只显示第一个‘.’前面的名字
[root@m01 ~]# ansible web01 -m setup -a 'filter=ansible_hostname'
        "ansible_hostname": "locahost"
ansible_fqdn:获取完整的主机名
[root@m01 ~]# ansible web01 -m setup -a 'filter=ansible_fqdn'
        "ansible_fqdn": "locahost.local"

3.条件判断语法

tasks: 
  - name: "shut down Debian flavored systems" 
  	command: /sbin/shutdown -t now 
  	when: ansible_facts['os_family'] == "Debian"
# 或

tasks: 
  - name: "shut down Ubuntu flavored systems" 
    command: apt-get install apache2 
    when: ansible_os_family == "Ubuntu"

案例

通过判断系统对软件包进行安装

- hosts: rsync
  tasks:
    - name: "shut down RedHat flavored systems"
      command: yum install -y unzip
      when:  ansible_facts['os_family'] == "RedHat"

通过判断主机来安装服务和推送文件

- hosts: all
  tasks:
    - name: 安装rsync和nfs服务
      yum: 
        name: 
          - rsync
          - nfs-utils
      when: ansible_hostname == 'backup' or ansible_hostname == 'nfs'
    - name: 推送rsync文件
      template: 
        src: /root/wordpress_ansible/rsync/rsyncd.conf
        dest: /etc/
      when: ansible_hostname == 'backup'
## 判断中的或与非
# and:与
# or:或
# !:非

判断是否安装nginx包

- hosts: web_group
  tasks: 
  - name: 查看nginx目录
    shell: "ls -l /etc/nginx"
    ## 通过register将命令执行结果保存到变量,然后通过when语句进行判断
    register: ng   
  - name: 判断是否安装nginx
    shell: "cd /opt && rpm-Uvh *.rpm"
    when: ng.rc != 0
    
# 模糊匹配
- hosts: all
  tasks: 
  - name: 查看nginx目录
    shell: "ls -l /etc/nginx"
    when: ansible_hostname is match 'web*'

playbook循环语句

列表循环

# 启动多个服务
- hosts: all
  tasks: 
  - name: 重新启动nginx和php
    service: 
      name: "{{ item }}"
      state: restarted
    with_items: 
      - nginx
      - php-fpm
    when: ansible_hostname is match 'web*'

字典循环

- hosts: all
  tasks: 
  - name: 重新启动nginx和php
    service: 
      name: "{{ item }}"
      state: restarted
    with_items: 
      - nginx
      - php-fpm
    when: ansible_hostname is match 'web*'
  - name: nginx and php 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"}
    when: ansible_hostname is match 'web*'

优化剧本

- 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
  - 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*'
- 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

- hosts: db01
  tasks:
  - 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'

标签:控制,name,dest,when,流程,hostname,ansible,nginx,Ansible
来源: https://www.cnblogs.com/zlyj/p/16428069.html