- install mysql
--- - hosts: gale remote_user: root gather_facts: no tasks: - name: install packages yum: name=libaio,perl - {name: create group, group: name=mysql system=yes gid=306} - name: create user user: name=mysql system=yes group=mysql shell=/sbin/nologin home=/data/mysql uid=306 create_home=no - name: unarchive mysql unarchive: src=/data/files/mysql-5.6.46.tar.gz dest=/usr/local owner=mysql group=mysql - name: make link file: src=/usr/local/mysql-5.6.46 dest=/usr/local/mysql state=link - name: create data dir shell: chdir=/usr/local/mysql mysqld --initialize-insecure --datadir=/data/mysql --user=mysql tags: data - name: copy my.cnf copy: src=/data/my.cnf dest=/etc/my.cnf - name: enable service shell: systemctl start mysqld;systemctl enable mysql tags: service - name: config PATH copy: content=PATH=/usr/local/mysql/bin:$PATH dest=/etc/profile.d/mysql.sh - name: script script: /data/init.sh tags: script
- notify & handler
--- - hosts: web-server remote_user: root tasks: - name: install httpd yum: name=httpd state=present - name: copy config files copy: src=/config/httpd.conf dest=/etc/httpd/conf/ notify: - restart httpd - check httpd process - name: config service service: name=httpd state=started enabled=yes handlers: - name: restart httpd service: name=httpd state=restarted - name: check httpd process shell: killall -0 httpd &> /tmp/httpd.log ...
-
环境变量
- hosts: gale remote_user: root vars: - ip: "{{ ansible_default_ipv4['address'] }}" - username: user1 - groupname: group1 - filename: bbb # ansible-playbook -e has great priority vars_files: - vars.yaml tasks: - name: create log file file: path=/dd/{{ ansible_nodename }}.log state=touch owner=rabid group=rabid mode=7000 - name: create log file file: path=/dd/{{ ip }}.log state=directory - name: create group group: name={{ groupname }} state=present - name: create user user: name={{ username }} state=present - name: create file file: path=/data/{{ filename }} state=touch - name: create file file: path=/mnt/{{ a }} state=directory - name: create file file: path=/mnt/{{ b }} state=touch
--- a: aaa b: bbb ...
-
template
--- - hosts: gale remote_user: root tasks: - name: install nginx yum: name=nginx state=present - name: actualize nginx template template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf notify: reset service - name: config nginx service service: name=nginx state=started enabled=yes handlers: - name: reset service service: name=nginx state=restarted
for--- - hosts: gale remote_user: root gather_facts: no vars: - vhosts: - listen: 80 server_name: bb.io - listen: 8080 server_name: pp.io tasks: - name: actualize nginx template template: src=nginx.conf.j2 dest=/mnt/nginx.conf
template:yaml
--- - hosts: gale remote_user: root gather_facts: no vars: - ports: [ 80, 81, 82] tasks: - name: actualize nginx template template: src=nginx.conf.j2 dest=/mnt/nginx.conf
条件判断
--- - hosts: gale remote_user: root gather_facts: no vars: - vhosts: - web1: listen: 81 root: /var/www/nginx/web1 - web2: listen: 82 server_name: web2.b.io root: /var/www/nginx/web2 - web3: listen: 83 server_name: web3.b.io root: /var/www/nginx/web3 tasks: - name: actualize nginx template template: src=nginx.conf.j2 dest=/mnt/nginx.conf
-
条件测试
--- - hosts: gale2:u1 remote_user: root tasks: - name: shutdown RedHat flavored systems command: shutdown -h now when: ansible_os_family == 'RedHat'
根据ip地址筛选--- - hosts: gale2:u1 remote_user: root tasks: - name: orient copy files copy: src=/etc/rpc dest=/opt when: "ansible_default_ipv4['address'] == '192.168.8.12'"
-
with_items
--- - hosts: gale remote_user: root tasks: - name: augment group group: name={{ item }} state=present with_items: - nginx - mysql - apache - name: augment user user: name={{ item.user }} state=present groups=wheel with_items: - user: augment1 - user: augment2 - name: augment user user: name={{ item.user }} group={{ item.group }} state=present with_items: - {name: nginx, group: nginx} - {name: mysql, group: mysql} - {name: apache, group: apache}
-
roles
--- - hosts: all remote_user: root roles: - {role: nginx, username: nginx, when: ansible_distribution_major_version == '7'} --- - hosts: all remote_user: root roles: - {role: nginx, tags: [nginx, web], when: ansible_distribution_major_version == '6'} - {role: httpd, tags: [httpd, web]} - {role: mysql, tags: [mysql, db]} - {role: mariadb, tags: [mariadb, db]} ansible-playbook --tags=nginx,httpd,mysql nginx-role.yaml
- hosts: gale remote_user: root roles: - {role: httpd, tags: [httpd, web], when: ansible_distribution_major_version == '7'} - {role: nginx, tags: [nginx, web], when: ansible_distribution_major_version == '8'}