就是将很多的ad-hoc,以yaml格式的形式集合到了一起
hello world
--- - hosts: test remote_user: root vars: com: /root tasks: - name: hello world shell: ls {{ com }}
vars
自定义变量,引用的时候需要使用"{{}}"
,注意都使用双引号吧,避免报错tasks
是用来指定需要执行的任务
系统变量
{{ ansible_devices.sda.model }}
条件语句
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: peic - shell: echo "This certainly is epic" when: not peic
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[2] }}.*.:ALL append_privs=yes password=foo with_nested: - ['alice','bob'] - ['clientdb','employeedb','providerdb']
- 有条件的循环
tasts: - command: echo {{ item }} with_items: [0,2,4,6,8,10] when: item > 5
实战
编写一个安装Python flask
环境的yml
--- - 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
zabbix
有server
端,以及agent
端- 两台机器,一台
centos
,一台ubuntu
hosts
文件编写
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
文件编写
--- - 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
文件,参数是deb
,state
参数没有installed
状态的。name
参数指定不到一个url,需要get_url
将deb
下载下来,dest
指定目录,来安装
后记
若是很多的tasks写在一个yaml文件里面,太臃肿,不好维护,怎么解决呢?那么下次说说它的roles~~peace yo~
本文作者为olei,转载请注明。
来看看
好久没更新了[aru_70]
@沉萧先生嗯,有点忙,这阵子准备辞职,忙复习 忙面试 更新先放下了..
@olei祝你成功。加油
本人链接信息中存在色情分享信息,不好意思,这个不给通过。。。
萌新看不懂,但还是要评论
虽然不知道说的是什么,但看起来好厉害的样子!