ansible

3.1 配置

#vim /etc/ansible/hosts       //定义主机,支持IP和域名,支持分组

[local]

127.0.0.1

[nginx]

192.168.0.10                 //客户端IP地址

#vim /etc/ansible/ansible.cfg    //简单配置,需要根据自己的实际环境修改

[defaults]

# some basic default values...

hostfile      = /etc/ansible/hosts

library       = /usr/share/ansible

remote_tmp     = $HOME/.ansible/tmp

pattern       = *

forks         = 5

sudo_user      = root

#ask_sudo_pass = True

#ask_pass     = True

transport     = smart

remote_port    = 2211

### 配置说明:

hostfile=/etc/ansible/hosts #指定默认hosts配置的位置

host_key_checking = False   #不进行host_key检查,省去目标key发生变化时输入(yes/no)的步骤

ask_pass=True               # 每次执行ansible命令是否询问ssh密码

ask_sudo_pass=True          # 每次执行ansible命令时是否询问sudo密码

log_path= /var/log/ansible.log    # ansible用户时需要:chown -R ansible:ansible ansible.log

#visudo

设置sudo    这样就不用咨询sudo密码了

3.2测试ansible的使用

$ansible all -m ping     //使用ping模块测试,以下显示成功,可以继续其他操作

127.0.0.1 | success >> {

}

192.168.0.10 | success >> {

}

3.3模块测试

1、ansible命令

语法ansible <host-pattern> [options]

ansible <pattern_goes_here> -m<module_name> -a <arguments>

选项

-i 设备列表路径,可制定一些动态路径

-f 并行任务数

–private-key 私钥路径

-m 模块名

-M 模块夹在路径

-a 参数

-k 登陆密码

-K sudo密码

-t 输出结果保存路径

-B 后台运行超时时间

-P 调查后台程序时间

-u 执行用户

-U sudo用户

-l 限制设备范围

-s是此用户sudo无需输入密码

2、体验命令操作

(1)用户类操作

$ ansible webtest -m group -a"name=jjgame state=present" -s

$ ansible webtest -m user -a"name=utest groups=jjgame state=present" -s

$ ansible webtest -m user -a"name=utest state=absent remove=yes" -s  

(2)服务安装管理

$ ansible local -a 'sudo apt-get -y installnginx'

$ ansible local -a "sudo/etc/init.d/nginx stop"

$ ansible local -a "sudo/etc/init.d/nginx start"

$ ansible webtest -m service -a "name=nginx state=running" -s  

(3)其他测试过的

$ ansible datacenter -a 'ls -l /root' -s

$ ansible datacenter -a 'mv/root/old.tar.gz /root/new.tar.gz' -s

$ ansible datacenter -m file -a'path=/root/new.tar.gz' -s    //查看文件属性

$ ansible datacenter -a 'rm -rf/root/directory' -s    //删除目录

$ ansible all -m command -a "/bin/echohello" --ask-pass

$ ansible all -m ping --ask-pass -u root

$ ansible all -m script -a"/root/hequan/shell/t.sh" -k

$ ansible all -m copy -a"src=/root/hequan/shell/t.sh dest=/tmp/ mode=755 owner=rootgroup=root" -k -u root

$ ansible all -m file -a"dest=/tmp/t.sh mode=755 owner=root group=root" -k -u root

$ ansible all -i /etc/ansible/serverlist -mshell -a "/tmp/t.sh" -k -u root

$ ansible webtest -m get_url -a"url=http://192.168.0.8/sa/ sh.tar.gz dest=/tmp/" -s

$ ansible webtest -a "sudo tar zxvf/tmp/ sh.tar.gz -C /tmp" -s  //不指定解压目录,会解压到/home/ansible目录下

$ ansible webtest -a "sudo/tmp/lansa_sh/t.sh" –s

原文地址:https://www.cnblogs.com/liqing1009/p/5017043.html