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'