标签:utils ges backup reg mod 表示 xxx yam stat
第一步:安装软件
ansible 172.16.1.41 -m yum -a "name=rsync state=installed"
第二步:编写文件
ansible 172.16.1.41 -m copy -a "src=/server/scripts/rsyncd.conf dest=/etc/"
第三步:创建用户组和用户
ansible 172.16.1.41 -m group -a "name=www gid=666"
ansible 172.16.1.41 -m user -a "name=www create_home=no shell=/sbin/nologin group=www uid=666"
第四步:创建目录
ansible 172.16.1.41 -m file -a "dest=/backup state=directory owner=www group=www"
第五步:创建密码文件
ansible 172.16.1.41 -m copy -a "content='rsync_backup:oldzhang dest=/etc/rsync.password mode=600"
第六步:启动服务
ansible 172.16.1.41 -m service -a "name=rsyncd state=started enabled=yes"
第一步:安装软件
ansible 172.16.1.41 -m yum -a "name=rsync state=installed"
playbook 翻译过来就是“剧本”, 那 playbook 组成如下
play: 定义的是主机的角色
task: 定义的是具体执行的任务
playbook: 由一个或多个 play 组成,一个 play 可以包含多个 task 任务
简单理解为: 使用不同的模块完成一件事情
1.合理的信息缩进,两个空格表示一个缩进关系
2.一定不要使用tab
标题一
_ _ 标题二
_ _ _ _ 标题三
所有冒号后面都要加上空格
hosts: 172.16.1.41
tasks:
yum: name=rsync state=installed
使用短横线构成列表信息,短横线后面需要有空格
- 老张
男
- 爱好
游泳
剧本文件拓展名为xxx.yaml
1.方便识别文件是一个剧本文件
2.文件编写时会有颜色提示
练习: 写一个剧本,使用yum/copy/service模块安装部署启动rsync服务
[root@m01 /server/scripts]# cat rsync_install.yaml
- hosts: 172.16.1.41
tasks:
- name: 01-add group
group: name=www gid=666
- name: 02-add user
user: name=www create_home=no shell=/sbin/nologin group=www uid=666
- name: 03-install rsync
yum: name=rsync state=installed
- name: 04-copy rsync conf
copy: src=/server/scripts/rsyncd.conf dest=/etc/
- name: 05-create passwd conf
copy: content='rsync_backup:oldboy' dest=/etc/rsync.passwd mode=600
- name: 06-create backup dir
file: path=/backup state=directory owner=www group=www
- name: 07-create backup dir
file: path=/data state=directory owner=www group=www
- name: 08-start rsyncd service
service: name=rsyncd state=started
- name: 09-enabled rsyncd service
systemd: name=rsyncd enabled=yes
NFS服务端:
[root@m01 /server/scripts]# cat nfs_server_install.yaml
- hosts: nfs_server
tasks:
- name: 01-add group
group: name=www gid='666'
- name: 02-add user
user: name=www create_home=no shell=/sbin/nologin group=www uid=666
- name: 03-install nfs service
yum: name=nfs-utils state=latest
- name: 04-copy nfs exports
copy: src=/server/scripts/exports dest=/etc/
- name: 05-create data dir
file: path=/data state=directory owner=www group=www
- name: 06-start rpcbind
service: name=rpcbind state=started
- name: 07-start nfs
service: name=nfs state=started
- name: 08-enable rpcbind
systemd: name=rpcbind enabled=yes
- name: 09-enable nfs
systemd: name=nfs enabled=yes
nfs客户端
[root@m01 /server/scripts]# cat nfs_client_install.yaml
- hosts: nfs_client
tasks:
- name: 01-add group
group: name=www gid=666
- name: 02-add user
user: name=www create_home=no shell=/sbin/nologin group=www uid=666
- name: 03-install nfs service
yum: name=nfs-utils state=latest
- name: 04-create data dir
file: path=/data state=directory owner=www group=www
- name: 05-start rpcbind
service: name=rpcbind state=started
- name: 06-enable rpcbind
systemd: name=rpcbind enabled=yes
- name: 07-mount data
mount: path=/data src=172.16.1.31:/data fstype=nfs opts=defaults state=mounted
ansible-playbook --syntax-check nfs_client_install.yaml
ansible-playbook -C nfs_client_install.yaml
ansible-playbook nfs_client_install.yaml
我们已经体验了使用剧本来安装服务,但是上述的简单ansible剧本存在一定的局限性
1.全部写成一行虽然看起来整洁,但是有一些特性没办法使用
2.比如同时需要创建多个目录,启动多个服务,需要重复写多条语句
3.参数不直观,不好修改
4.剧本里写的是启动服务,如果配置文件发生变化,重复执行不会重启服务
不过没有关系,等学习了下面的高级特性,然后我们可以换一种写法
官方网址:
https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html
使用情景:
1.需要创建多个目录
2.需要启动多个服务
具体实现:
1.同时创建2个目录/data和/backup
[root@m01 /server/scripts/test]# cat loops.yaml
- hosts: 172.16.1.41
tasks:
- name: 01-create dir data and backuo
file:
path: "{{ item }}"
state: directory
owner: www
group: www
loop:
- /data
- /backup
2.同时启动2个服务
[root@m01 /server/scripts/test]# cat service.yaml
- hosts: 172.16.1.31
tasks:
- name: 01-start rpcbind nfs service
service:
name: "{{ item }}"
state: started
loop:
- rpcbind
- nfs
官方网址:
https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html
使用情景:
1.自定义某个名称,在任务中会多次引用
2.从主机收集的系统信息中提取某个变量并引用,例如网卡信息
具体实现:
1.自定义一个文件名变量,创建文件时引用
[root@m01 /server/scripts/test]# cat vars.yaml
- hosts: 172.16.1.41
vars:
file_name: oldzhang
tasks:
- name: 01-use vars create dir
file:
path: "/root/{{ file_name }}"
state: directory
owner: www
group: www
2.使用变量获取主机的eth1地址
[root@m01 /server/scripts/test]# cat ip.yaml
- hosts: 172.16.1.41
tasks:
- name: 01-get ip address
shell: "echo {{ ansible_facts.eth1.ipv4.address }} > /root/ip_eth1.txt"
3.在主机hosts中指定变量
[root@m01 ~]# tail -5 /etc/ansible/hosts
[backup]
172.16.1.41
[backup:vars]
file_name="oldzhang"
使用情景:将配置文件的状态注册成一个变量,方便其他任务引用
具体实现:
1.将配置文件的状态注册成一个服务变量并打印出来
[root@m01 /server/scripts/test]# cat register.yaml
- hosts: 172.16.1.41
tasks:
- name: 01-register rsync status
shell: netstat -lntp|grep rsync
register: rsync_port
- name: 02-out rsync status
debug:
msg: "{{ rsync_port.stdout_lines }}"
打印多个信息
- hosts: nfs
tasks:
- name: 01-echo hostname
shell: echo $(hostname)
register: nfs_hostname
- name: debug nfs_hostname
debug:
msg: "{{ item }}"
loop:
- "{{ nfs_hostname.stdout }}"
- "{{ nfs_hostname.cmd }}"
官方文档:
https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html?highlight=handlers#handlers-running-operations-on-change
使用情景:如果配置文件发生了变化,就重启服务,否则什么都不操作
具体实现:
[root@m01 /server/scripts/test]# cat handlers.yaml
- hosts: 172.16.1.41
tasks:
- name: 01-if nfs conf changed,then restart nfs service
copy:
src: exports
dest: /etc/
notify: Restart_Nfs_Server
handlers:
- name: Restart_Nfs_Server
service:
name: nfs
state: restarted
使用情景:从我们指定的任务开始执行,而不是从头到尾执行一遍
具体实现:
- hosts: 172.16.1.41
tasks:
- name: 01-add group
group: name=www gid=666
tags: 01-add-group
- name: 02-add user
user: name=www create_home=no shell=/sbin/nologin group=www uid=666
tags: 02-add-user
- name: 03-install rsync
yum: name=rsync state=installed
tags: 03-install-rsync
- name: 04-copy rsync conf
copy: src=/server/scripts/rsyncd.conf dest=/etc/
tags: 04-copy-conf
- name: 05-create passwd conf
copy: content='rsync_backup:oldboy' dest=/etc/rsync.passwd mode=600
tags: 05-create-passwd
- name: 06-create backup dir
file: path=/backup state=directory owner=www group=www
tags: 06-create-backup
- name: 07-create backup dir
file: path=/data state=directory owner=www group=www
tags: 07-create-data
- name: 08-start rsyncd service
service: name=rsyncd state=started
tags: 08-start-rsyncd
- name: 09-enabled rsyncd service
systemd: name=rsyncd enabled=yes
tags: 09-enable
调用标签:
1.打印出playbook里要执行的所有标签
[root@m01 /server/scripts/test]# ansible-playbook --list-tags tags2.yml
playbook: tags2.yml
play #1 (172.16.1.41): 172.16.1.41 TAGS: []
TASK TAGS: [01-add-group, 02-add-user, 03-install-rsync, 04-copy-conf, 05-create-passwd, 06-create-backup, 07-create-data, 08-start-rsyncd, 09-enable]
2.指定运行某个标签
ansible-playbook -t 05-create-passwd tags2.yml
3.指定运行多个标签,使用逗号隔开
ansible-playbook -t 05-create-passwd,06-create-backup tags2.yml
3.指定不运行某个标签
ansible-playbook --skip-tags=05-create-passwd tags2.yml
4.指定不运行多个标签
ansible-playbook --skip-tags=05-create-passwd,06-create-backup tags2.yml
00.检查剧本拼写规范
ansible-playbook --syntax-check check.yaml
01.检查这个任务执行的主机对象
ansible-playbook --list-host check.yaml
02.检查这个剧本需要执行哪些任务
ansible-playbook --list-tasks check.yaml
03.检查这个剧本执行哪些tag
ansible-playbook --list-tags check.yaml
04.模拟执行剧本
ansible-playbook -C check.yaml
脚本实例:
- hosts: rsync_server
vars:
rsync_conf_path: '/server/scripts/rsync_yaml/rsyncd.conf'
tasks:
- name: 01-install rsync
yum:
name: rsync
state: installed
- name: 02-backup & copy
copy:
src: "{{ rsync_conf_path }}"
dest: /etc/
backup: yes
notify:
- restart rsyncd
- name: 03-create user group
group:
name: www
gid: 666
- name: 04-create user user
user:
name: www
create_home: no
shell: /sbin/nologin
group: www
uid: 666
- name: 05-create backup dir
file:
dest: "{{ item }}"
state: directory
owner: www
group: www
loop:
- /backup
- /data
- name: 06-create passwd
copy:
content: 'rsync_backup:oldzhang'
dest: /etc/rsync.passwd
mode: '0600'
- name: 09-start rsynd
service:
name: rsyncd
state: started
enabled: yes
tags: 09_start_rsynd
handlers:
- name: restart rsyncd
service:
name: rsyncd
state: restarted
标签:utils ges backup reg mod 表示 xxx yam stat
原文地址:https://www.cnblogs.com/skyy/p/12448013.html