Ansible playbook
- install mysql
12345678910111213141516171819202122232425262728---
- 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
123456789101112131415161718192021---
- 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
...
-
环境变量
12345678910111213141516171819202122232425- 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
1234---
a: aaa
b: bbb
...
-
template
123456789101112131415---
- 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
for1234567891011121314---
- 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
12345678910---
- 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
条件判断
123456789101112131415161718192021---
- 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
-
条件测试
12345678---
- hosts: gale2:u1
remote_user: root
tasks:
- name: shutdown RedHat flavored systems
command: shutdown -h now
when: ansible_os_family ==
'RedHat'
根据ip地址筛选12345678---
- 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
12345678910111213141516171819202122---
- 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
1234567891011121314151617---
- 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
123456- 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'
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2020-07-31 OpenSSH for windows
2020-07-31 SQLserver -- 备份脚本