Ansible 流程控制

playbook条件语句

rsync配置

[root@m01 rsync]# cat rsync.yml 
- hosts: rsync
  tasks:
    - name: Stop firewalld
      systemd:
        name: firewalld
        state: stopped
        enabled: no

    - name: Install rsync Server
      yum:
        name: rsync
        state: present
      when: ansible_fqdn == "backup"

    - name: config rsync
      copy:
        src: ./rsync.txt
        dest: /etc/rsyncd.conf
      when: ansible_fqdn == "backup"

    - name: Create Group
      group:
        name: www
        gid: 666
        state: present

    - name: Create User
      user:
        name: www
        uid: 666
        group: www
        shell: /sbin/nologin
        create_home: false


    - name: Create password file
      copy:
        content: rsync_backup:123456
        dest: /etc/rsync.passwd
        owner: root
        group: root
        mode: 0600
      when: ansible_fqdn == "backup"

    - name: Crate rsync Dir
      file:
        path: /backup
        state: directory
        owner: www
        group: www
        mode: 0755
      when: ansible_fqdn == "backup"

    #添加条件
    - name: Start rsync Server
      systemd:
        name: rsyncd
        state: started
        enabled: yes
      when: ansible_fqdn == "backup"

    #分组添加条件
    - name: Install Rsync Server
      yum:
        name: rsync
        state: present
      when: (ansible_fqdn == "web01") or (ansible_fqdn == "web02")

    #列表格式添加条件
    - name: Create client Password File
      copy:
        content: 123456
        dest: /etc/rsync.passwd
        mode: 0600
      when:
        - ansible_fqdn == "web01"
        - ansible_fqdn == "web02"

添加判断条件判断服务是否安装(通过register将命令执行结果保存至变量,然后通过when语句进行判断)

       #执行一个shell,得到的结果,注册一个变量
       - name: Get php Status
      shell: "rpm -qa | grep php"
      register: get_php_status

    #调用变量,输出内容
    - name: Get php Status
      debug:
        msg: ""

    #调用变量的结果,rc不等于0的时候执行语句
    - name: Install PHP Server
      shell: cd /tmp && rpm -ivh /tmp/*rpm
      when: get_php_status.rc != 0

使用条件判断系统

#判断系统剧本
[root@m01 lnmp]# vim test_xitong.yml
- hosts: web_group
  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"

#执行命令
[root@m01 lnmp]# ansible-playbook test_xitong.yml 

PLAY [web_group] ********************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************
ok: [web02]
ok: [web01]

TASK [Install CentOS apache] ********************************************************************************************
changed: [web01]
changed: [web02]

TASK [Install Ubuntu apache] ********************************************************************************************
skipping: [web01]
skipping: [web02]

PLAY RECAP **************************************************************************************************************
web01                      : ok=2    changed=1    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
web02                      : ok=2    changed=1    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   

[root@m01 lnmp]#

使用条件运算

#当企业里有很多版本的系统时,先判断版本是否支持服务在进行操作
tasks:
  - shell: echo "only on Red Hat 6, derivatives, and later"
    when: ansible_facts['os_family'] == "RedHat" and ansible_facts['lsb']['major_release']|int >= 7

使用条件语句判断CentOS版本

#判断系统剧本
[root@m01 lnmp]# vim test_xitong.yml
- hosts: web_group
  tasks:
    - name: start CentOS 6 apache
      shell: "/etc/init.d/httpd start"
      when: (ansible_distribution == "CentOS") and (ansible_distribution_major_version == "6")

    - name: start CentOS 7 apache
      shell: "systemctl start httpd"
      when: (ansible_distribution == "CentOS") and (ansible_distribution_major_version == "7")

playbook循环语句

1.定义变量循环安装服务

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

- hosts: web_group
  tasks:
    - name: ensure a list of packages installed
      yum: 
        name: "" 
        state: present
      with_items:
        - httpd
        - httpd-tools

2.定义变量循环启动

    - name: Start nginx php maraidb
      systemd:
        name: ""
        state: started
        enabled: yes
      with_items:
        - nginx
        - php-fpm
        - mariadb

3.字典定义变量

    #创建用户组
    - name: Create group
      group:
        name: ""
        gid: ""
        state: present
      with_items:
        - { name: "www", gid: "666" }
        - { name: "lhd", gid: "888" }

    #创建用户
    - name: Create user
      user:
        name: ""
        uid: ""
        group: ""
        shell: /sbin/nologin
        create_home: false
      with_items:
        - { name: "www", uid: "666" }
        - { name: "lhd", uid: "888" }

    #创建目录
    - name: Create Code dir
      file:
        path: ""
        state: directory
        owner: ""
        group: ""
        mode: 0755
      with_items:
        - { path: "/code", owner: "www" }
        - { path: "/data", owner: "lhd" }

4.利用字典定义方式优化lnmp剧本

- hosts: web01
  tasks:
    - name: Stop firewalld
      systemd:
        name: firewalld
        state: stopped
        enabled: no

    - name: Create group
      group:
        name: ""
        gid: ""
        state: present
      with_items:
        - { name: "www", gid: "666" }
        - { name: "lhd", gid: "888" }

    - name: Create user
      user:
        name: ""
        uid: ""
        group: ""
        shell: /sbin/nologin
        create_home: false
      with_items:
        - { name: "www", uid: "666" }
        - { name: "lhd", uid: "888" }

    - name: Create Code dir
      file:
        path: ""
        state: directory
        owner: ""
        group: ""
        mode: 0755
      with_items:
        - { path: "/code", owner: "www" }
        - { path: "/data", owner: "lhd" }  

    - name: Install wordpress
      unarchive:
        src: ""
        dest: ""
        owner: ""
        group: ""
        mode: 0755
      with_items:
        - { src: "./wordpress.tar.gz", dest: "/code", owner: "www" }
        - { src: "./php.tar.gz", dest: "/tmp", owner: "root" }

    - name: Copy Nginx Server
      copy:
        src: ./nginx-1.16.1-1.el7.ngx.x86_64.rpm
        dest: /tmp 

    - name: Install Nginx Server
      yum:
        name: file:///tmp/nginx-1.16.1-1.el7.ngx.x86_64.rpm
        state: present

    - name: Get php Status
      shell: "rpm -qa | grep php"
      register: get_php_status
      ignore_errors: yes

    - name: Install PHP Server
      shell: yum localinstall -y /tmp/*.rpm
      when: get_php_status.rc != 0

    - name: Copy Nginx Server
      copy:
        src: ""
        dest: ""
      with_items:
        - { src: "./nginx.conf", dest: "/etc/nginx" }
        - { src: "./blog.linux.com.conf", dest: "/etc/nginx/conf.d" }
        - { src: "./www.conf", dest: "/etc/php-fpm.d" }

    - name: Install mariadb
      yum: 
        name: mariadb-server,MySQL-python
        state: present

    - name: Start nginx php maraidb
      systemd:
        name: ""
        state: started
        enabled: yes
      with_items:
        - nginx
        - php-fpm
        - mariadb

    - name: Create mysql user
      mysql_user:
        name: "www"
        password: "123456"
        priv: "*.*:ALL"
        state: present

    - name: Create root password
      mysql_user:
        name: "root"
        password: "123456"
        priv: "*.*:ALL"
        state: present

    - name: Create databas for wordpress
      mysql_db:
        login_user: "www"
        login_password: "123456"
        name: wordpress
        state: present
Copyright © 高程程 all right reserved,powered by Gitbook修订于: 2021-05-18 21:14:35

results matching ""

    No results matching ""