一、tasks:任務,即調用模塊完成的操做 二、variables:變量 三、templates:模板 四、handlers:處理器,當條件知足時執行操做,一般前面使用notify聲明。 五、roles:角色,分門別類管理playbook的一種方式,後面將詳細介紹roles的用法。
vim /opt/test.yamlphp
- hosts: webserver //定義的主機組,即應用的主機 vars: //定義變量 http_port: 80 //此處自定義變量中變量寫在yaml文件,也可寫在ansible/hosts文件中應用主機後 max_clients: 200 remote_user: root //遠程用戶爲root tasks: //執行的任務 - name: ensure apache is at the latest version //下面即將執行的步驟名稱 yum: pkg=httpd state=latest - name: write the apache config file //下面即將執行的步驟名稱 template: src=/srv/httpd.j2 dest=/etc/httpd.conf //模板文件,src爲路徑爲控制端,dest爲被控制端 notify: //聲明須要執行的操做 - restart apache //操做名稱 - name: ensure apache is running service: name=httpd state=started handlers: //處理器,處理名稱須要對應notify的名稱,才能自動執行 - name: restart apache service: name=httpd state=restarted
ansible-playbook test.yaml --syntax-check #檢查yaml文件的語法是否正確 ansible-playbook test.yaml --list-task #檢查tasks任務 ansible-playbook test.yaml --list-hosts #檢查生效的主機 ansible-playbook test.yaml --start-at-task='Copy Nginx.conf' #指定從某個task開始運行
--- #表示該文件是YAML文件,非必須 - hosts: webserver #指定主機組,能夠是一個或多個組。 remote_user: root #指定遠程主機執行的用戶名 還能夠爲每一個任務定義遠程執行用戶: --- - hosts: mysql remote_user: root tasks: - name: test connection ping: remote_user: mysql #指定遠程主機執行tasks的運行用戶爲mysql
ansible-playbook ping.yaml mysql
--- - hosts: mysql remote_user: root become: yes #2.6版本之後的參數,以前是sudo,意思爲切換用戶運行 become_user: mysql #指定sudo用戶爲mysql
Handlers也是一些task的列表,和通常的task並無什麼區別。是由通知者進行的notify,若是沒有被notify,則Handlers不會執行,假如被notify了,則Handlers被執行,無論有多少個通知者進行了notify,等到play中的全部task執行完成以後,handlers也只會被執行一次。web
- hosts: webserver remote_user: root tasks: - name: install httpd package yum: name=httpd state=latest - name: install configuration file for httpd copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf notify: //聲明後面Handlers的名稱 -restart httpd //注意該名稱爲notify的子集,須要靠空格隔開 - name: start httpd service service: enabled=true name=httpd state=started handlers: - name: restart httpd service: name=httpd state=restarted 也能夠引入變量 --- - hosts: webserver remote_user: root vars: //引入變量 - package: httpd //變量名稱 - service: httpd tasks: - name: install httpd package yum: name={{package}} state=latest //引入變量方式 - name: install configuration file for httpd copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf notify: -restart httpd - name: start httpd service service: enabled=true name={{service}} state=started handlers: - name: restart httpd //handlers執行的操做必須是notify中聲明的操做名稱,若此處執行的操做名稱notify中沒有聲明,那麼handlers不會執行 service: name={{service}} state=restarted
在引入變量過程當中,若針對一個組裏不一樣主機須要引入的變量值不相同時,好比IP地址等,則須要在hosts文件中,在不一樣主機後面跟上變量名稱與值,yaml文件中執行操做時引入變量名稱便可。sql
vim /etc/ansible/hostsshell
[webserver] 192.168.144.110 httpd_port="192.168.144.110:80" server_name="www.yun01.com:80" 192.168.144.111 httpd_port="192.168.144.111:80" server_name="www.yun02.com:80"
Jinja是基於Python的模板引擎。template類是jinja的另外一個重要組件,能夠看作是編譯過的模板文件,用來生產目標文件,傳遞Python的變量給模板去替換模板中的標記。apache
cp /etc/httpd/conf/httpd.conf /opt/httpd.conf.j2 複製模板,注意文件格式爲j2結尾
vim /opt/httpd.conf.j2vim
Listen {{httpd_port}} //爲須要修改的部分設定變量 ... ServerName {{server_name}}
vim /etc/ansible/hosts安全
[webserver] 192.168.144.110 httpd_port="192.168.144.110:80" server_name="www.yun01.com:80" 192.168.144.111 httpd_port="192.168.144.111:80" server_name="www.yun02.com:80"
vim /opt/httpd.yaml服務器
- hosts: webserver //針對webserver組執行 vars: package: httpd //變量名稱 server: httpd tasks: - name: install httpd yum: name={{package}} state=latest - name: write the apache config file template: src=/opt/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf //注意模板文件位置 notify: restart httpd - name: start httpd service: name={{server}} enabled=true state=started handlers: - name: restart httpd service: name={{server}} state=restarted
vi when.yml --- - hosts: mysql remote_user: root tasks: - name: "shutdown CentOS" command: /sbin/shutdown -h now when: ansible_distribution == "CentOS"
vi when.yml --- - hosts: mysql remote_user: root tasks: - name: "shut down CentOS 7 systems" command: /sbin/shutdown -r now when: - ansible_distribution == "CentOS" - ansible_distribution_major_version == "7"
vi when.yml --- - hosts: mysql remote_user: root tasks: - name: "shut down CentOS 6 and Debian 7 systems" command: /sbin/shutdown -t now when: (ansible_distribution == "CentOS" and ansible_distribution_major_version == "6") or (ansible_distribution == "Debian" and ansible_distribution_major_version == "7")
vi when.yml --- - hosts: all vars: exist: "True" tasks: - name: creaet file command: touch /tmp/test.txt when: exist | match("True") - name: delete file command: rm -rf /tmp/test.txt when: exist | match("False")
--- - hosts: webserver remote_user: root tasks: - name: "Install Packages" yum: name={{ item }} state=latest with_items: - httpd - mysql-server - php
--- - hosts: webserver remote_user: root tasks: - name: "Add users" user: name={{ item.name }} state=present groups={{ item.groups }} with_items: - { name:'test1', groups:'wheel'} - { name:'test2', groups:'root'}
vi hosts.yml --- - hosts: webserver remote_user: root tasks: - name: Copy hosts file copy: src=/etc/hosts dest=/etc/hosts tags: - only - name: touch file file: path=/opt/hosts state=touch
執行命令:ide
ansible-playbook hosts.yml --tags="only"
ansible-playbook hosts.yml
vi hosts.yml --- - hosts: webserver remote_user: root tasks: - name: Copy hosts file copy: src=/etc/hosts dest=/etc/hosts tags: - only - name: copy test file copy: src=/etc/test.txt dest=/opt/test.txt - name: touch file file: path=/opt/hosts state=touch tags: - always
執行命令:
ansible-playbook hosts.yml --tags="only"