ansible 简单模块简单服务的安装

准备工作
可以通过ssh-copy-id分发密钥
rm -rf /root/.ssh/id*
ssh-keygen -t dsa -f /root/.ssh/id_dsa -N “”
for ip in 31 41 7
do
sshpass -pcentos ssh-copy-id -i /root/.ssh/id_dsa.pub “172.16.1.$ip -p22”
##注可以根据-p适应更改后的端口
done
安装ansible
yum install epel-release -y
yum install ansible
然后可以在/etc/ansible中配置hosts文件
[web]
172.16.1.41 ansible_user=root ansible_password=centos
172.16.1.31 ansible_user=root ansible_password=centos

在管理主机上执行命令属于adhoc
ansible web -m command -a “hostname” command无法用分号隔开
ansible web -m shell -a “ls;cd”

ansible 软件模块
ansible [管理主机,组机组信息(主机地址)all] -m指定模块名称 -m 模块名称 -a 相关参数
ansible剧本
1 模块 command
ansible-doc -l|wc -l
[root@localhost ansible]# ansible 172.16.1.31 -m command -a “chdir=/tmp pwd”
172.16.1.31 | CHANGED | rc=0 >>
/tmp
ansible 172.16.1.41 -m command -a “creates=/etc/rsyncd.conf hostname” 文件存在就不执行
ansible 172.16.1.41 -m command -a “removes=/etc/rsyncd.conf hostname” 文件存在就执行
rsync命令rsync -avz /etc/hosts [email protected]::backup/host
查看文档
参数():free_form–执行command必须有linux的命令

shell模块与command 类似
3模块 command shell
ansible-doc -l|wc -l
[root@localhost ansible]# ansible 172.16.1.31 -m command -a “chdir=/tmp pwd”
172.16.1.31 | CHANGED | rc=0 >>
/tmp
ansible 172.16.1.41 -m shell -a “creates=/etc/rsyncd.conf hostname” 文件存在就不执行
ansible 172.16.1.41 -m shell -a “removes=/etc/rsyncd.conf hostname” 文件存在就执行
rsync命令rsync -avz /etc/hosts [email protected]::backup/host
查看文档
参数():free_form–执行command必须有linux的命令
docs.ansible.com

4 script模块
本地的脚本在远端可以执行 专门运行脚本的模块
ansible 172.16.1.41 -m script -a “/server/scripts/yum.sh”
shell必须远端存在的
ansible 172.16.1.41 -m shell -a “sh /server/scripts/yum.sh”
yum对应的模块

文件操作
copy
参数:backup对数据进行备份
src 定义推送的参数信息
dest 远程推送的文件的目录信息
owner 属主
group 属组
mode 文件权限755
[root@localhost ansible]# ansible 172.16.1.41 -m copy -a “src=/server/scripts/yum.sh dest=/tmp”

file 模块
ansible 172.16.1.41 -m file -a “dest=/tmp/yum.sh owner=root group=oldboy mode=755”

state指定创建文件目录
ansible172.16.1.41 -m file -a “dest=/tmp/1.txt state=touch” 创建文件
创建目录
ansible172.16.1.41 -m file -a “dest=/tmp/1.txt state=directory” 创建文件

yum 模块
name
state installed present latest 安装 卸载 absent removed
ansible 172.16.1.41 -m yum -a “name=iftop state=installed”
ansible 172.16.1.41 -m yum -a “name=iftop state=absent”
系统服务类型
service
name 指定管理服务名称
state:stopped restarted reload
enabled 服务开机自启动 yes no
ansible 172.16.1.41 -m service -a “name=crond state=stopped enabled=no”
ansible 172.16.1.41 -m service -a “name=crond state=started enabled=no”

cron --定时任务模块

* * * * *分时日月周
minute=0-59 * */n * *,  
加名称使得定时任务唯一

ansible 172.16.1.41 -m cron -a "name=oldboy01 minute=0-59 hour=0 job='/bin/bash /server/scripts/test.sh &>/dev/null'"

ansible 172.16.1.41 -m cron -a "name=oldboy01 minute=0-59 hour=0 job='/bin/bash /server/scripts/test.sh &>/dev/null'  state=absent"
注释定时任务
ansible 172.16.1.41 -m cron -a "name=oldboy01   job='/bin/bash /server/scripts/test.sh &>/dev/null'  disable=yes"
取消定时任务
ansible 172.16.1.41 -m cron -a "name=oldboy01 minute=0-59 hour=0 job='/bin/bash /server/scripts/test.sh &>/dev/null'  disable=no"

ansible剧本

1   - 用法列表显示的内容
	水果信息
		-苹果
		-香蕉
		-西瓜		
2 :用法说明
   姓名:张三
   性别:男
   人员信息:
  - 运维人员:sa
   -开发人员:dev
3 空格的语法说明
 对编写的内容进行分级时,需要两个空格分级
软件安装步骤:
		-服务端安装步骤:
		  第一个里程碑:检查安装
		  第一个里程碑:编写配置文件内容
		-客户安装步骤

```yaml
#nfs install
- hosts: 172.16.1.41
  tasks:
    - name: install rpc service
      yum: name=rpcbind state=installed
    - name: install nfs service
      yum: name=nfs-utils state=installed
    - name: copy config file
      copy: dest=/etc/ src=/etc/ansible/conf/nfs_conf/exports
    - name: nfsnobody create
      user: name=nfsnobody state=present createhome=no shell=/sbin/nologin
    - name: create be mounted dir
      file: dest=/data state=directory owner=nfsnobody group=nfsnpbody          
#install rsync
- hosts: 172.16.1.41
  tasks:
    - name: step01:install rsync
      yum: name=rsync state=installed
    - name: step02:edit rsync conf file
      copy: src=/etc/ansible/conf/rsync_conf/rsyncd.conf dest=/etc/
    - name: step03:create rsync user
      user: name=rsync state=present createhome=no shell=/sbin/nologin
    - name: step04:create auth file
      copy: src=/etc/ansible/conf/rsync_conf/rsync.password dest=/etc/ mode=600
    - name: step05:create backup dir
      file: dest=/backup state=directory owner=rsync group=rsync
    - name: step06:boot rsync server
      shell: rsync --daemon creates=/var/run/rsyncd.pid
- hosts: 172.16.1.31
  tasks:
    - name: step-1:create auth file
      copy: src=/etc/ansible/conf/rsync_conf/rsync_client.password dest=/etc/rsync.password mode=600

                                                          

你可能感兴趣的:(ansible,yaml)