当前位置: 首页 > news >正文

ansible模块+playbook

scripts模块

script模块用于在远程机器上执行本地脚本。

[root@bb ~]# vim test000.sh 
[root@bb ~]# cat test000.sh 
#!/bin/bash
mkdir /tmp/three
touch /tmp/three/test
echo 'i am echo,at mttt' > /tmp/three/test
echo 'well done'
[root@bb ~]# sh test000.sh 
mkdir: 无法创建目录"/tmp/three": 文件已存在
well done
# ansible group02 -m script -a './test000.sh'
# ls /tmp/
111                                                                      three
a.txt                                                                    xxx
a.txt.4331.2024-08-16@17:23:26~                                          xxx2
systemd-private-18e460b4dc5b47458e28ad6b292e1a98-chronyd.service-ZPvmft

--用ansible搭建nfs服务

[root@m0 ~]# ansible group02 -m file -a 'path=/static state=directory'
[root@m0 ~]# ansible group02 -m file -a 'path=/static/test state=touch'
​
[root@m0 ~]# ansible group02 -m command -a 'yum -y install nfs-utils'
​
[root@s0 ~]# rpm -qa | grep nfs
libnfsidmap-0.25-19.el7.x86_64
nfs-utils-1.3.0-0.68.el7.2.x86_64
​
[root@m0 ~]# ansible group02 -m yum -a 'name=rpcbind state=latest'
​
[root@s0 ~]# rpm -qa | grep rpcbind
rpcbind-0.2.0-49.el7.x86_64
​
[root@m0 ~]# vim /etc/exports
​
/static *(ro,rsync)
​
[root@m0 ~]# ansible group02 -m copy -a 'src=/etc/exports dest=/etc/exports'
​
[root@m0 ~]# ansible group02 -m service -a 'name=rpcbind state=started enabled=yes'
[root@m0 ~]# ansible group02 -m service -a 'name=nfs state=started enabled=yes'
[root@m0 ~]# yum -y install nfs-utils
[root@m0 ~]# mkdir /nfs
​
[root@m0 ~]# mount -t nfs 192.168.2.112:/static /nfs/
[root@m0 ~]# mount -t nfs 192.168.2.111:/static /nfs/
[root@m0 ~]# mount -t nfs 192.168.2.110:/static /nfs/
mount.nfs: Operation not permitted
[root@m0 ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   17G  4.3G   13G   26% /
devtmpfs                 476M     0  476M    0% /dev
tmpfs                    488M     0  488M    0% /dev/shm
tmpfs                    488M  7.7M  480M    2% /run
tmpfs                    488M     0  488M    0% /sys/fs/cgroup
/dev/sr0                 8.8G  8.8G     0  100% /mnt
/dev/sda1               1014M  130M  885M   13% /boot
tmpfs                     98M     0   98M    0% /run/user/0
192.168.2.110:/static     17G  2.1G   15G   13% /nfs
192.168.2.112:/static     17G  2.1G   15G   13% /nfs
192.168.2.111:/static     17G  2.1G   15G   13% /nfs
​
[root@s0 ~]# ls /static/
test
​
[root@s1 ~]# ls /static/
test
​
[root@s2 ~]# ls /static/
test

playbook

playbook剧本是保存在控制机的yml文件

Playbook常⻅语法

hosts: ⽤于指定要执⾏任务的主机,其可以是⼀个或多个由冒号分隔主机组。

remote_user: ⽤于指定远程主机上的执⾏任务的⽤户 。

- hosts: group1 
remote_user: root
tasks: 任务列表, 按顺序执⾏任务.

如果⼀个host执⾏task失败, 整个tasks都会回滚, 修正playbook中的错误, 然后重新执⾏即可

tasks:
- name: ensure apache is at the latest version 
   yum: name=httpd,httpd-devel state=latest
- name: write the apache config file 
   copy: src=/etc/httpd/conf/httpd.conf
   dest=/etc/httpd/conf/httpd.conf
handlers: 类似task,但需要使⽤notify通知调⽤。

不管有多少个通知者进⾏了notify,等到play中的所有task执⾏完 成之后,handlers也只会被执⾏⼀次。

handlers最佳的应⽤场景是⽤来重启服务,或者触发系统重启操作,除此以外很少⽤到了。

notify: 
- restart apache
   - name: ensure apache is running (and enable it at boot)
      service: name=httpd state=started enabled=yes
handlers:
   - name: restart apache
      service: name=httpd state=restarted

--用剧本安装vsftpd

# vim test001.yml
​
---
-       hosts: group02remote_user:  roottasks:-       name: 安装vsftpdyum: name=vsftpd state=latest
# ansible-playbook ./test001.yml
​
PLAY [group02] *********************************************************************
​
TASK [Gathering Facts] *************************************************************
ok: [192.168.2.111]
ok: [192.168.2.110]
ok: [other]
​
TASK [安装vsftpd] ********************************************************************
ok: [other]
ok: [192.168.2.111]
ok: [192.168.2.110]
​
PLAY RECAP *************************************************************************
192.168.2.110              : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.2.111              : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
other                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
​

--用剧本卸载和安装vsftpd,且启动服务

# vim test001.yml
---
-       hosts: group02remote_user:  roottasks:-       name: 卸载vsftpdyum: name=vsftpd        state=absent
​-       name: 安装vsftpdyum: name=vsftpd state=latest
​-       name: 启动服务service:        name=vsftpd state=started enabled=yes
# ansible-playbook ./test001.yml
​
PLAY [group02] *********************************************************************
​
TASK [Gathering Facts] *************************************************************
ok: [192.168.2.111]
ok: [other]
ok: [192.168.2.110]
​
TASK [卸载vsftpd] ********************************************************************
changed: [other]
changed: [192.168.2.111]
changed: [192.168.2.110]
​
TASK [安装vsftpd] ********************************************************************
changed: [other]
changed: [192.168.2.111]
changed: [192.168.2.110]
​
TASK [启动服务] ************************************************************************
changed: [192.168.2.111]
changed: [192.168.2.110]
changed: [other]
​
PLAY RECAP *************************************************************************
192.168.2.110              : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.2.111              : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
other                      : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
​

--剧本的格式

---
-  hosts:   组名/别名/ip/域名remote_user:   roottasks:- name: 任务说明模块: key0=value0service: name=vfstpd state=started  enabled=yes- name:  修改配置文件command: sed ....notify:-  abcdefghandler:- name: abcdefgservice: name=vfstpd state=restarted

--将httpd的端口号80改为8080

# vim test002.yml
---
-       hosts:       group01remote_user: roottasks:-       name: 将控制主机的repo文件复制到被控制主机copy:  src=/etc/yum.repos.d  dest=/etc/
​-       name: 安装httpdyum:  name=httpd  state=present
​-       name: 修改配置文件command:  sed -i '/^Listen/s/80/8080/g' /etc/httpd/conf/httpd.conf
​-       name: 修改默认的资源文件command: echo 'xxxxxxx' > /var/www/html/index.html
​-       name: 启动httpd服务service: name=httpd state=started

--使用剧本在不同主机上同时创建不同的文件

# vim /etc/ansible/hosts
s1 ansible_ssh_host=192.168.1.17
ansible_ssh_port=22 ansible_ssh_user=root
ansible_ssh_pass=7s2 ansible_ssh_host=192.168.1.18
ansible_ssh_port=22 ansible_ssh_user=root
ansible_ssh_pass=7---
-       hosts: s1remote_user: roottasks:-       name: 创建一个文件file: path=/tmp/xxxxxx.txt state=touch
​
-       hosts: s2remote_user: roottasks:-       name: 也创建一个文件file: path=/tmp/yyyyy.txt state=touch

--使用剧本搭建nfs服务

# vim test003.yml ---
-       hosts:    s1remote_user:    roottasks:-       name:   安装nfs-utilsyum:    name=nfs-utils  state=present-       name:   安装rpcbindyum:    name=rpcbind    state=present-       name:   创建共享目录file:   path=/static  state=directory-       name:   配置文件shell:  echo '/static  *(ro,rsync)' > /etc/exports-       name:   启动服务service:        name=nfs        state=started   enabled=yes-       name:   启动服务rpcbindservice:        name=rpcbind    state=started enabled=yes-       hosts:  s2remote_user:    roottasks:-       name:   安装nfs-utilsyum:    name=nfs-utils  state=latest-       name:   创建挂载目录file:   path=/nfs       state=directory-       name:   挂载nfs文件command:        mount -t nfs 192.168.1.17:/static /nfs


http://www.mrgr.cn/news/4894.html

相关文章:

  • C++STL初阶(12):stack和queue的初阶实现
  • leetcode-494. 目标和
  • 【LeetCode:3】无重复字符串的最长子串(Java)
  • 【精品实战项目】深度学习预测、深度强化学习优化、附源码数据手把手教学
  • 设计模式的七大原则
  • 06 Oracle数据是怎么存储的
  • 数据库机器上停service360safe
  • BUG——imx6u开发_结构体导致的死机问题(未解决)
  • 动态规划(二)
  • 03:电容的充放电特性及应用举例
  • VMware vSphere Replication 虚拟机备份及迁移实践
  • LeetCode面试题Day13|LC383 赎金信、LC290 单词规律
  • Java数组07:稀疏数组
  • 算法-矩阵置零(73)
  • C++ //练习 17.14 编写几个正则表达式,分别触发不同错误。运行你的程序,观察编译器对每个错误的输出。
  • 使用 Vue 2 搭建大屏可视化系统
  • 当黑神话遇上AI:悟空背后的策划逆袭战
  • uni-app 开发华为鸿蒙HarmonyOS NEXT初体验
  • Qt/C++地图标注点的添加删除移动旋转/指定不同图标和动图/拿到单击信号
  • ansible-自动化运维