一、配置文件修改:

1、备份原配置文件:

cp /etc/ansible/hosts /etc/ansible/hosts.bak

2、修改hosts配置文件:

cat <<EOF>>/etc/ansible/hosts
[test]
10.10.10.1
10.10.10.2
10.10.10.3
[test1]
10.10.10.11
10.10.10.12
[test:vars]
ansible_ssh_user=test
ansible_ssh_pass=
ansible_su_pass=
ansible_sudo_user=test
ansible_sudo_pass=
[all:vars]
ansible_ssh_user=test
ansible_ssh_pass=
ansible_su_pass=
ansible_sudo_user=test
ansible_sudo_pass= EOF

注:

1、可以不设置免密码登录,配置好ansible_ssh_user和ansible_ssh_pass即可

2、若想用登录用户test启用sudo权限,登录目标服务器:

执行visudo命令,最后一行加入 'test ALL=(ALL) ALL'即可。

echo 'test ALL=(ALL)       ALL' >>/etc/sudoers

( 不建议echo 'test ALL=(ALL)       NOPASSWD: ALL' >>/etc/sudoers ; sudo免密码,普通用户权限过大,有安全隐患)

二、测试

1、登录用户测试

[root@localhost vmuser]# ansible test -m ping
10.10.10.1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
10.10.10.2 | SUCCESS => {
"changed": false,
"ping": "pong"
}
10.10.10.3 | SUCCESS => {
"changed": false,
"ping": "pong"
}

2、sudo权限测试

[root@localhost vmuser]# ansible test1 -m shell -a "fdisk -l |head -n2" -s
10.10.10.11 | SUCCESS | rc= >> Disk /dev/sda: 2000.4 GB, bytes 10.10.10.12 | SUCCESS | rc= >> Disk /dev/sda: 2000.4 GB, bytes

3、su权限测试:

[root@localhost vmuser]# ansible all -S -R root -m shell -a "/sbin/fdisk -l |head -n2"
[DEPRECATION WARNING]: The su command line option has been deprecated in favor of the "become" command line arguments. This feature will be removed in version 2.6.
Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
10.10.10.1 | SUCCESS | rc= >> Disk /dev/sda: 2000.4 GB, bytes 10.10.10.2 | SUCCESS | rc= >> Disk /dev/sda: 2000.4 GB, bytes 10.10.10.3 | SUCCESS | rc= >> Disk /dev/sda: 2000.4 GB, bytes 10.10.10.11 | SUCCESS | rc= >> Disk /dev/sda: 2000.4 GB, bytes 10.10.10.12 | SUCCESS | rc= >> Disk /dev/sda: 2000.4 GB, bytes

最新文章

  1. C#开发微信门户及应用(1)--开始使用微信接口
  2. Windows安装mysql-5.7.17-winx64.zip方式
  3. 自定义 placeholder 文本颜色
  4. Android消息机制
  5. Nginx反向代理模板
  6. IOS开发-CALayer和UIView详细汇总
  7. [转]Google Cloud Messaging: Overview
  8. [IOS UIalert模版]
  9. iOS 轻量级的数据库leveldb
  10. &quot;this class is not key value coding-compliant for the key ...&quot;问题的解决
  11. python排序(选择, 插入)
  12. ebtables使用
  13. Java多线程概念
  14. 1、Django系列之web应用与http协议
  15. Core Animation之基础介绍
  16. “全栈2019”Java异常第十章:throw与throws区别详解
  17. 记一次处理IE引起的上网异常处理
  18. P1001 A+B Problem (树链剖分)
  19. shell脚本编程的10个最佳实践
  20. (二十)linux中i2c的ioctl,write,read函数的使用

热门文章

  1. leetcode322
  2. BASIC GIT WORKFLOW
  3. 如何将R中的data frame对象的数据导入到DB
  4. JDK Timer &amp; TimerTask
  5. LevelDB源码分析-Get
  6. 如何在js中使用递归
  7. 吴裕雄 python深度学习与实践(15)
  8. Jekins在Tomcat上的安装和配置
  9. 替换php remi源
  10. 【转】IO多路复用机制详解