Ansible流程控制
阿新 • • 發佈:2020-07-25
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
# 再配合判斷語句的使用來實現對不同主機配置檔案的推送