ansible入门(安装 简单使用(常用模块 and 简单的playbook例子)),ansibleplaybook


Ansible

说明

无需agent(使用SSH)  模块化基于python  paramikoSSH   


安装(centos)

yum install ansible -y

 

配置文件

[root@base ~]# ls/etc/ansible/

ansible.cfg  hosts roles

 

将服务端的SSH公钥 添加到客户端中(ssh-copy-id agenthost)

 

 在远程主机上执行命令

[root@base ~]# ansiblebase2 -m command -a 'ifconfig'

 [WARNING]: provided hosts list is empty, onlylocalhost is available

没有定义主机清单

[root@base ansible]# vim/etc/ansible/hosts

[testhost]

base

base2


批量执行命令例子

[root@base ~]# ansible all -a 'ifconfig'   

执行成功会返回SUCCESS 与rc=0 (!=0的都是失败)



user模块

批量创建用户

ansible testhost -m user-a "name=song state=present"

id song

批量删除用户

ansible testhost -m user-a "name=song state=absent"

 

 

 

 

cron模块

添加cron任务

[root@base ~]# ansible all-m cron -a 'name="ntp" minute="*/10"job="/usr/sbin/ntpdate 203.107.6.88 &> /dev/null "'

 删除cron任务

[root@base ~]# ansible all-m cron -a 'name="ntp" state=absent'

 

 

 

copy模块

指定源文件  目标路径  和目标文件权限

[root@10 ~]# ansible tbds-m copy -a 'src=/etc/fstab dest=/tmp/fstab.bak mode=777'

 

 

flie模块

设置文件属性

创建目录(path和name的效果一样  上层目录若不存在会自动创建==mkdir -p )

[root@10 ~]# ansible tbds-m file -a 'path=/tmp/testdir state=directory'

[root@10 ~]# ansible tbds-m file -a 'name=/tmp/testdir state=directory'

 

创建链接(ln -s

[root@10 ~]# ansible tbds-m file -a 'name=/tmp/fstab.bak.link src=/tmp/fstab.bak state=link'

 

 

ping模块检查主机存活

[root@10 ~]# ansible tbds-m ping

 

 

yum模块

安装wget(present   latest absent)

[root@10 ~]# ansible all-m yum -a 'name=wget state=present'

 

Service模块

重启sshd服务(statestarted/stopped/restarted/reloaded   enabled:yes/no 开机自启

[root@10 ~]# ansible tbds-m service -a 'name=sshd state=restarted enabled=yes'

 

 

shell模块

因为command模块只支持非常简单的命令  若命令中带有|管道符是执行不了的

[root@10 ~]# ansible all-a 'echo test.123 | passwd --stdin song'

 

 [root@10 ~]# ansible all -m shell -a 'echotest.123 | passwd --stdin song'

 

 

script模块

将本机的sh脚本在远程主机上执行

[root@10 ~]# ansible tbds-m script -a '/root/test.sh'

 

setup模块

收集主机信息

[root@10 ~]# ansible tbds-m setup

 

 

**********************************************************************

 

 

playbook任务编排

安装httpd

 [root@localhost ~]# cat http.yml 
---
- hosts: all
  sudo: yes


  tasks:
   - name: "install apached"
     yum: name={{ item }} state=present
     with_items:
        - httpd
        - httpd-devel
   - name: "start httpd"
     service: name=httpd state=started enabled=yes

 

 

 

 

 


相关内容

    暂无相关文章