1 安装ansible
yum install epel-release -y
yum install ansible –y
2
-i 指定文件,不指定的话,默认为这个文件/etc/ansible/host
-u 指定用户,不指定的话默认为root
-m 指定模块 -a 指定模块的参数
-k 提示输入远程主机的密码
[root@moban ansible]# cat /etc/ansible/hosts
[test]
192.168.132.129
[test2]
[root@moban ansible]#
[root@moban ansible]# ansible -i /etc/ansible/hosts test -u root -m command -a 'ls /home' -k ####test是定义的组名
SSH password:
192.168.132.129 | SUCCESS | rc=0 >>
[root@moban ansible]#
[root@moban ~]# ansible all -m 'ping' ##all 指的是所有组内的所有IP
192.168.132.129 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n",
"unreachable": true
}
[root@moban ~]# ansible all -m 'ping' -k
SSH password:
192.168.132.129 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[root@moban ~]#
ansible_ssh_user 用于指定用于管理远程主机的账号
ansible_ssh_host 用于指定被管理的主机
ansible_ssh_port 用于指定ssh的端口
ansible_ssh_private_key_file 指定key文件
host_key_checking=False 当第一次连接远程主机时,会提示输入yes/no,跳过此环节。
3
[root@moban ansible]# cat hosts
[test]
192.168.132.129 ansible_ssh_user=appuser ansible_ssh_port=8821
192.168.132.130
192.168.132.131
[test2]
[root@moban ansible]#ansible all -m ping –sudo
4
[root@moban ansible]#
[root@moban ansible]# cat hosts
[mfs:children]
mfs_master
mfs_logger
mfs_node
mfs_client
[mfs_master]
192.168.132.129 host_key_checking=False
[mfs_logger]
192.168.132.130 host_key_checking=False
[mfs_node]
192.168.132.131 host_key_checking=False
[mfs_client]
[root@moban ansible]# ansible mfs -m ping -k
SSH password:
192.168.132.130 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.132.131 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.132.129 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[root@moban ansible]# pwd
/etc/ansible
[root@moban ansible]#
1 command 模块不支持 | 管道,所以需要改为shell 模块
[root@moban ~]# ansible -i /etc/ansible/hosts mfs_node -m command -a 'ps -ef|grep ssh' -k
SSH password:
192.168.132.131 | FAILED | rc=1 >>
ERROR: Unsupported SysV option.
********* simple selection ********* ********* selection by list *********
-A all processes -C by command name
-N negate selection -G by real group ID (supports names)
-a all w/ tty except session leaders -U by real user ID (supports names)
-d all except session leaders -g by session OR by effective group name
-e all processes -p by process ID
-q by process ID (unsorted & quick)
T all processes on this terminal -s processes in the sessions given
a all w/ tty, including other users -t by tty
g OBSOLETE -- DO NOT USE -u by effective user ID (supports names)
r only running processes U processes for specified users
x processes w/o controlling ttys t by tty
*********** output format ********** *********** long options ***********
-o,o user-defined -f full --Group --User --pid --cols --ppid
-j,j job control s signal --group --user --sid --rows --info
-O,O preloaded -o v virtual memory --cumulative --format --deselect
-l,l long u user-oriented --sort --tty --forest --version
-F extra full X registers --heading --no-heading --context
--quick-pid
********* misc options *********
-V,V show version L list format codes f ASCII art forest
-m,m,-L,-T,H threads S children in sum -y change -l format
-M,Z security data c true command name -c scheduling class
-w,w wide output n numeric WCHAN,UID -H process hierarchynon-zero return code
[root@moban ~]#
[root@moban ~]#
[root@moban ~]# ansible -i /etc/ansible/hosts mfs_node -m shell -a 'ps -ef|grep ssh' -k
SSH password:
192.168.132.131 | SUCCESS | rc=0 >>
root 1664 1 0 17:53 ? 00:00:00 /usr/sbin/sshd
root 3371 1664 0 18:14 ? 00:00:00 sshd: root@pts/0
root 3450 1664 0 18:16 ? 00:00:00 sshd: root@pts/3
root 3456 1 0 18:16 ? 00:00:00 ssh: /root/.ansible/cp/4134e47ae7 [mux]
root 3850 3375 15 18:17 pts/0 00:00:00 /usr/bin/python2.6 /usr/bin/ansible -i /etc/ansible/hosts mfs_node -m shell -a ps -ef|grep ssh -k
root 3860 3850 14 18:17 pts/0 00:00:00 /usr/bin/python2.6 /usr/bin/ansible -i /etc/ansible/hosts mfs_node -m shell -a ps -ef|grep ssh -k
root 3932 3860 0 18:17 pts/0 00:00:00 sshpass -d12 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/4134e47ae7 -tt 192.168.132.131 /bin/sh -c '/usr/bin/python '"'"'~None/.ansible/tmp/ansible-tmp-1530094677.19-16976646478347/command.py'"'"' && sleep 0'
root 3933 3932 0 18:17 pts/2 00:00:00 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/4134e47ae7 -tt 192.168.132.131 /bin/sh -c '/usr/bin/python '"'"'~None/.ansible/tmp/ansible-tmp-1530094677.19-16976646478347/command.py'"'"' && sleep 0'
root 3949 3948 0 18:17 pts/3 00:00:00 /bin/sh -c ps -ef|grep ssh
root 3951 3949 0 18:17 pts/3 00:00:00 grep ssh
[root@moban ~]#
2
[root@moban ~]# ansible all -m copy -a 'src=/etc/yum.repos.d/163.repo dest=/etc/yum.repos.d/163.repo'
[root@moban ~]# ansible mmmfs_node -m yum -a 'name=httpd state=installed'