Ansible-playbook小记

2018-12-27 16:23:24 浏览数 (1)

就是将很多的ad-hoc,以yaml格式的形式集合到了一起

hello world

代码语言:javascript复制
---
- hosts: test
  remote_user: root
  vars:
    com: /root
  tasks:
  - name: hello world
    shell: ls {{ com }}
  • vars自定义变量,引用的时候需要使用"{{}}",注意都使用双引号吧,避免报错
  • tasks是用来指定需要执行的任务

系统变量

代码语言:javascript复制
{{ ansible_devices.sda.model }}

条件语句

  • when语句
代码语言:javascript复制
tasks:
- name: "shutdown Debian flavored systems"
  command: /sbin/shutdown -t now
  when: ansible_os_family == "Debian"
  • bool
代码语言:javascript复制
vars:
  epic: true
tasks:
- shell: echo "This certainly is epic"
  when: peic
- shell: echo "This certainly is epic"
  when: not peic
  • with_items循环语句
代码语言:javascript复制
- name: add_several users
  user: name={{ item }} state=present groups=wheel
  with_items:
    - testuser1
    - testuser2
  • with_nested嵌套循环
代码语言:javascript复制
- name: users access control
  mysql_user: name={{ item[0] }}
              priv={{ item[2] }}.*.:ALL
              append_privs=yes
              password=foo
  with_nested:
      - ['alice','bob']
      - ['clientdb','employeedb','providerdb']
  • 有条件的循环
代码语言:javascript复制
tasts:
   - command: echo {{ item }}
   with_items: [0,2,4,6,8,10]
   when: item > 5

实战

编写一个安装Python flask环境的yml

代码语言:javascript复制
---
- hosts: test
  remote_user: root
  become: true
  tasks:
  - name: install python for ubuntu
    apt:
      name: "{{ item }}"
      state: latest
      update_cache: yes
    with_items:
      - python-dev
      - python-setuptools
    when: ansible_distribution == 'Ubuntu'
  - name: install python for centos
    yum:
      name: "{{ item }}"
      state: installed
    with_items:
      - python-devel
      - python-setuptools
    when: ansible_distribution == 'CentOS'
  - name: install pip
    shell: easy_install pip
  - name: pip install flask and redis
    pip:
      name: "{{ item }}"
    with_items:
      - flask
      - redis

ansible_distribution系统变量用来检测机器是哪种操作系统

playbook编写zabbix

  • zabbixserver端,以及agent
  • 两台机器,一台centos,一台ubuntu
  • hosts文件编写
代码语言:javascript复制
centos ansible_ssh_port=22 ansible_ssh_host=xx.xx.xx.1 ansible_ssh_user=root
ubuntu ansible_ssh_port=22 ansible_ssh_host=xx.xx.xx.2 ansible_ssh_user=simon

[test]
centos
ubuntu
  • yml文件编写
代码语言:javascript复制
---
- hosts: test
  remote_user: root
  become: true
  tasks:
  - name: install zabbix rpm source
    yum:
      name: https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
      state: installed
    when: ansible_distribution == 'CentOS'
  - name: download zabbix deb for ubuntu
    get_url:
      url: https://repo.zabbix.com/zabbix/4.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_4.0-2 xenial_all.deb
      dest: /tmp/zabbix.deb
    when: ansible_distribution == 'Ubuntu'
  - name: install zabbix deb for ubuntu
    apt:
      # name: /tmp/zabbix.deb
      deb: /tmp/zabbix.deb
      # state: installed
  - name: install zabbix server -> centos
    yum:
      name: "{{ item }}"
      state: installed
    with_items:
      - zabbix-server-mysql
      - zabbix-proxy-mysql
      - zabbix-web-mysql
    when: ansible_distribution == 'CentOS'
  - name: install zabbix agent -> ubuntu
    apt:
      name: zabbix-agent
      update_cache: yes
      # state: installed
    when: ansible_distribution == 'Ubuntu'
  - name: config zabbix server
    replace:
      path: /etc/zabbix/zabbix_server.conf
      regexp: DBUser=zabbix
      replace: DBUser=root
    when: ansible_distribution == 'CentOS'
  - name: import db format for server
    shell: zcat /usr/share/doc/zabbix-server-mysql-4.0/create.sql.gz | mysql -uroot -p 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 == 'Ubuntu'

apt模块,本地的deb文件,参数是debstate参数没有installed状态的。name参数指定不到一个url,需要get_urldeb下载下来,dest指定目录,来安装

后记

若是很多的tasks写在一个yaml文件里面,太臃肿,不好维护,怎么解决呢?那么下次说说它的roles~~peace yo~

本文作者为olei,转载请注明。

ansible ansible-playbook

0 人点赞