https://ansible-tran.readthedocs.io/en/latest/docs/playbooks_intro.html Ansible中文網址html
什麼是Ansible Playbookpython
功能列表mysql
與Adhoc關係linux
支持的特性web
hosts行的內容是一個或多個組或主機patternsredis
remote_user就是賬號名:sql
--- - hosts: test remote_user: root tasks: - name: hello world shell: ls /root
Playbook基本結構shell
變量定義:數據庫
變量位置編程
Inventory
Playbook
每個 task 必須有一個名稱 name,這樣在運行 playbook 時,從其輸出的任務執行信息中能夠很好的辨別出是屬於哪一ra個 task 的. 若是沒有定義 name,‘action’ 的值將會用做輸出信息中標記特定的 task.
--- - hosts: test remote_user: root vars: com: ls /root tasks: - name: hello world shell: "{{ com }}"
YAML語法要求若是值以{{ foo }}開頭的話咱們須要將整行用雙引號包起來.這是爲了確認你不是想聲明一個YAML字典
系統變量
Ansible經常使用模塊
經常使用參數配置:
when語句
tasks: - name: "shutdown Debian flavored systems" command: /sbin/shutdown -t now when: ansible_os_family == "Debian"
bool值
vars: epic: true tasks: - shell: echo "This certainly is epic" when: epic - shell: echo "This certainly is epic" when: not epic
with_items循環語句
- name: add several users user: name={{ item }} state=present groups=wheel with_items: - testuser1 - testuser2
with_nested 嵌套循環
- name: users access control mysql_user: name={{ item[0] }} priv={{ item[1] }}.*:All append_privs=yes password=foo with_nested: - ['alice','bob'] - ['clientdb','employeedb','providerdb']
有條件的循環
tasks: - command: echo {{item}} with_items: [0,2,4,6,8,10] when: item > 5
Playbook實戰
需求分析
--- - hosts: test remote_user: root become: true # root用戶能夠省去這部 tasks: - name: install python for centos yum: name: "{{ item }}" state: installed with_items: - python-devel - python-setuptools when: ansible_distribution == 'CentOS' - name: install python for ubuntu apt: name: "{{ item }}" state: lastest update_cache: yes with_items: - libpython-dev - python-setuptools when: ansible_distribution == 'Ubuntu' - name: install pip shell: easy_install pip - name: pip install flask and redis pip: name: "{{ item }}" with_items: - flask - redis
出現報錯,報錯信息寫的很明白,版本問題在網上也沒有找到答案,只能根據不一樣版本先這麼寫,後面再看看資料補充
[DEPRECATION WARNING]: Invoking "yum" only once while using a loop via squash_actions is deprecated. Instead of using a loop to supply multiple items and specifying `name: "{{ item }}"`, please use `name: ['python-devel', 'python-setuptools']` and remove the loop. This feature will be removed in version 2.11. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
ansible-playbook --version ansible-playbook 2.8.4
--- - hosts: test remote_user: root become: true tasks: - name: install python for centos yum: name: ['python-devel','python-setuptools'] state: installed when: ansible_distribution == 'CentOS' - name: install python for ubuntu apt: name: ['libpython-dev','python-setuptools'] state: lastest update_cache: yes when: ansible_distribution == 'Ubuntu' - name: install pip shell: easy_install pip - name: pip install flask and redis pip: name: ['flask','redis']
zabbix安裝需求
--- - hosts: test become: true tasks: - name: install zabbix rpm source yum: name: http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-2.el7.noarch.rpm state: installed when: ansible_distribution == 'CentOS' - name: donwload ubuntu deb get_url: url: http://repo.zabbix.com/zabbix/3.4/ubuntu/pool/main/z/zabbix-release/zabbix-release_3.4-1+xenial_all.deb dest: /tmp/zabbix.deb when: ansible_distribution == 'Ubuntu' - name: install zabbix source apt: deb: /tmp/zabbix.deb when: ansible_distribution == 'Ubuntu' - name: install centos zabbix package yum: name: ['zabbix-server-mysql','zabbix-proxy-mysql','zabbix-web-mysql'] state: installed when: ansible_distribution == 'CentOS' - name: install ubuntu zabbix package yum: name: zabbix-agent update_cache: yes when: ansible_distribution == 'Ubuntu' - name: modify zabbix config replace: path: /etc/zabbix/zabbix_server.conf regexp: DBUser=zabbix replace: DBUser=root when: ansible_distribution == 'CentOS' - name: import zabbix sql table shell: zcat /usr/share/doc/zabbix-server-mysql-3.4.7/create.sql.gz | mysql -uroot zabbix when: ansible_distribution == 'CentOS' - name: disable selinux selinux: state: disabled when: ansible_distribution == 'CentOS' - name: start zabbix-server systemd: name: zabbix-server state: started when: ansible_distribution == 'CentOS' - name: start zabbix-agent systemd: name: zabbix-agent state: started when: ansible_distribution == 'CentOS'