<s id="xcxsa"></s><rp id="xcxsa"></rp>

  • <span id="xcxsa"><pre id="xcxsa"><rt id="xcxsa"></rt></pre></span>
    <dd id="xcxsa"></dd><tbody id="xcxsa"></tbody>
        首頁 Ansible教程Ansible-Playbook企業案例

        Ansible-Playbook核心元素以及組件

        Ansible-Playbook中使用變量

        運維派隸屬馬哥教育旗下專業運維社區,是國內成立最早的IT運維技術社區,歡迎關注公眾號:yunweipai
        領取學習更多免費Linux云計算、Python、Docker、K8s教程關注公眾號:馬哥linux運維

        playbook 命令

        格式

        ansible-playbook <filename.yml> ... [options]

        常見選項

        -C --check          #只檢測可能會發生的改變,但不真正執行操作
        --list-hosts        #列出運行任務的主機
        --list-tags         #列出tag
        --list-tasks        #列出task
        --limit 主機列表      #只針對主機列表中的主機執行
        -v -vv  -vvv        #顯示過程

        范例

        ansible-playbook  file.yml  --check #只檢測
        ansible-playbook  file.yml  
        ansible-playbook  file.yml  --limit websrvs

        Playbook 初步

        利用 playbook 創建 mysql 用戶

        范例:mysql_user.yml

        ---
        - hosts: dbsrvs
          remote_user: root
        
          tasks:
            - {name: create group, group: name=mysql system=yes gid=306}
            - name: create user
              user: name=mysql shell=/sbin/nologin system=yes group=mysql uid=306 home=/data/mysql create_home=no      
        利用 playbook 安裝 nginx

        范例:install_nginx.yml

        ---
        # install nginx 
        - hosts: websrvs
          remote_user: root  
          tasks:
            - name: add group nginx
              user: name=nginx state=present
            - name: add user nginx
              user: name=nginx state=present group=nginx
            - name: Install Nginx
              yum: name=nginx state=present
            - name: web page
              copy: src=files/index.html dest=/usr/share/nginx/html/index.html
            - name: Start Nginx
              service: name=nginx state=started enabled=yes
        利用 playbook 安裝和卸載 httpd

        范例:install_httpd.yml

        ---
        #install httpd 
        - hosts: websrvs
          remote_user: root
          gather_facts: no
        
          tasks:
            - name: Install httpd
              yum: name=httpd state=present
            - name: Install configure file
              copy: src=files/httpd.conf dest=/etc/httpd/conf/
            - name: web html
              copy: src=files/index.html  dest=/var/www/html/
            - name: start service
              service: name=httpd state=started enabled=yes
        
        ansible-playbook   install_httpd.yml --limit 10.0.0.8

        范例:remove_httpd.yml

        #remove_httpd.yml
        ---
        - hosts: websrvs
          remote_user: root
        
          tasks:
            - name: remove httpd package
              yum: name=httpd state=absent
            - name: remove apache user 
              user: name=apache state=absent
            - name: remove config file
              file: name=/etc/httpd  state=absent
            - name: remove web html
              file: name=/var/www/html/index.html state=absent
        
        利用 playbook 安裝mysql

        范例:安裝mysql-5.6.46-linux-glibc2.12

        [root@ansible ~]#ls -l /data/ansible/files/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz 
        -rw-r--r-- 1 root root 403177622 Dec  4 13:05 /data/ansible/files/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
        
        [root@ansible ~]#cat /data/ansible/files/my.cnf 
        [mysqld]
        socket=/tmp/mysql.sock
        user=mysql
        symbolic-links=0
        datadir=/data/mysql
        innodb_file_per_table=1
        log-bin
        pid-file=/data/mysql/mysqld.pid
        
        [client]
        port=3306
        socket=/tmp/mysql.sock
        
        [mysqld_safe]
        log-error=/var/log/mysqld.log
        
        [root@ansible ~]#cat /data/ansible/files/secure_mysql.sh 
        #!/bin/bash
        /usr/local/mysql/bin/mysql_secure_installation <<EOF
        
        y
        magedu
        magedu
        y
        y
        y
        y
        EOF
        
        [root@ansible ~]#tree /data/ansible/files/
        /data/ansible/files/
        ├── my.cnf
        ├── mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
        └── secure_mysql.sh
        
        0 directories, 3 files
        
        [root@ansible ~]#cat /data/ansible/install_mysql.yml
        ---
        # install mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
        - hosts: dbsrvs
          remote_user: root
          gather_facts: no
        
          tasks:
            - name: install packages
              yum: name=libaio,perl-Data-Dumper,perl-Getopt-Long
            - name: create mysql group
              group: name=mysql gid=306 
            - name: create mysql user
              user: name=mysql uid=306 group=mysql shell=/sbin/nologin system=yes create_home=no home=/data/mysql
            - name: copy tar to remote host and file mode 
              unarchive: src=/data/ansible/files/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz dest=/usr/local/ owner=root group=root 
            - name: create linkfile  /usr/local/mysql 
              file: src=/usr/local/mysql-5.6.46-linux-glibc2.12-x86_64 dest=/usr/local/mysql state=link
            - name: data dir
              shell: chdir=/usr/local/mysql/  ./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
              tags: data
            - name: config my.cnf
              copy: src=/data/ansible/files/my.cnf  dest=/etc/my.cnf 
            - name: service script
              shell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
            - name: enable service
              shell: /etc/init.d/mysqld start;chkconfig --add mysqld;chkconfig mysqld on  
              tags: service
            - name: PATH variable
              copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh
            - name: secure script
              script: /data/ansible/files/secure_mysql.sh
              tags: script

        范例:install_mariadb.yml

        ---
        #Installing MariaDB Binary Tarballs
        - hosts: dbsrvs
          remote_user: root
          gather_facts: no
        
          tasks:
            - name: create group
              group: name=mysql gid=27 system=yes
            - name: create user
              user: name=mysql uid=27 system=yes group=mysql shell=/sbin/nologin home=/data/mysql create_home=no
            - name: mkdir datadir
              file: path=/data/mysql owner=mysql group=mysql state=directory
            - name: unarchive package
              unarchive: src=/data/ansible/files/mariadb-10.2.27-linux-x86_64.tar.gz dest=/usr/local/ owner=root group=root
            - name: link
              file: src=/usr/local/mariadb-10.2.27-linux-x86_64 path=/usr/local/mysql state=link 
            - name: install database
              shell: chdir=/usr/local/mysql   ./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
            - name: config file
              copy: src=/data/ansible/files/my.cnf  dest=/etc/ backup=yes
            - name: service script
              shell: /bin/cp  /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld
            - name: start service
              service: name=mysqld state=started enabled=yes
            - name: PATH variable
              copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh

        本文鏈接:http://m.abandonstatusquo.com/34658.html

        Ansible-Playbook核心元素以及組件

        Ansible-Playbook中使用變量

        網友評論comments

        發表評論

        您的電子郵箱地址不會被公開。

        暫無評論

        Copyright ? 2012-2022 YUNWEIPAI.COM - 運維派 京ICP備16064699號-6
        掃二維碼
        掃二維碼
        返回頂部
        十分钟免费观看视频高清下载