环境准备
主机名 | IP | 身份 |
---|---|---|
m01 | 10.0.0.61 | Ansible 控制端 |
web01 | 172.16.1.7 | Ansible 被控端 |
web03 | 172.16.1.9 | Ansible 被控端 |
安装Ansible
1 |
[root@m01 ~]# yum install -y ansible |
Ansible命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# ansible <host-pattern> [options] --version #ansible版本信息 -i #主机清单文件路径,默认是在/etc/ansible/hosts -m #使用的模块名称,默认使用command模块 -a #使用的模块参数,模块的具体动作 -k #提示输入ssh密码,而不使用基于ssh的密钥认证 -C #模拟执行测试,但不会真的执行 -T #执行命令的超时 #查看Ansible版本及模块路径 [root@m01 ~]# ansible --version ansible 2.9.10 config file = /etc/ansible/ansible.cfg configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python2.7/site-packages/ansible executable location = /usr/bin/ansible python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] |
Ansible配置文件读取顺序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
[root@m01 ~]# vim /etc/ansible/ansible.cfg # nearly all parameters can be overridden in ansible-playbook # or with command line flags. ansible will read ANSIBLE_CONFIG, # ansible.cfg in the current working directory, .ansible.cfg in # the home directory or /etc/ansible/ansible.cfg, whichever it # finds first [root@m01 ~]# rpm -ql ansible [root@m01 ~]# zcat /usr/share/man/man1/ansible-config.1.gz #要查看完整列表,请访问https://docs.ansibe.com/或使用ansibe-config命令。 For a full list check \fI\%https://docs.ansible.com/\fP\&. or use the \fIansible\-config\fP command. #/etc/ansible/ansible.cfg 配置文件,如果存在则使用 /etc/ansible/ansible.cfg \-\- Config file, used if present #~/.ansible.cfg 用户配置文件,覆盖默认配置(如果存在) ~/.ansible.cfg \-\- User config file, overrides the default config if present #\&/ansible.cfg 本地配置文件(在当前工作目录中)假定为(aqproject-specific)(aq,如果存在,则重写其余文件)。 \&./ansible.cfg \-\- Local config file (in current working directory) assumed to be \(aqproject specific\(aq and overrides the rest if present. #如上所述,ANSIBLE_CONFIG环境变量将覆盖所有其他环境变量。 As mentioned above, the ANSIBLE_CONFIG environment variable will override all others. #生效优先级 ANSIBLE_CONFIG >> $ANSIBLE_CONFIG/ansible.cfg >> ~/.ansible.cfg >> /etc/ansible/ansible.cfg |
配置Ansible
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
[root@m01 ~]# cat /etc/ansible/ansible.cfg #inventory = /etc/ansible/hosts #主机列表配置文件 #library = /usr/share/my_modules/ #库文件存放目录 #remote_tmp = ~/.ansible/tmp #临时py文件存放在远程主机目录 #local_tmp = ~/.ansible/tmp #本机的临时执行目录 #forks = 5 #默认并发数 #sudo_user = root #默认sudo用户 #ask_sudo_pass = True #每次执行是否询问sudo的ssh密码 #ask_pass = True #每次执行是否询问ssh密码 #remote_port = 22 #远程主机端口 host_key_checking = False #跳过检查主机指纹 log_path = /var/log/ansible.log #ansible日志 #普通用户提权操作 [privilege_escalation] #become=True #become_method=sudo #become_user=root #become_ask_pass=False |
配置主机清单
单主机配置
1)方式一
1 2 3 4 5 6 7 8 9 |
#ip+端口+用户+密码 [root@m01 ~]# vim /etc/ansible/hosts [web01] 172.16.1.7 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='1' [web03] 172.16.1.7 ansible_ssh_port=22 #测试主机清单 [root@m01 ~]# ansible '*' -m ping |
2)方式二
1 2 3 4 5 6 7 8 9 |
[root@m01 ~]# vim /etc/hosts 172.16.1.7 web01 172.16.1.9 web03 [root@m01 ~]# vim /etc/ansible/hosts [web01] web01 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='1' [web03] web03 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='1' |
3)方式三
1 2 3 4 5 6 7 8 9 |
[root@m01 ~]# vim /etc/ansible/hosts [web1] web01 [web3] web03 [web1:vars] ansible_ssh_pass='1' [web3:vars] ansible_ssh_pass='1' |
4)基于秘钥的方式
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#生成密钥对 [root@m01 ~]# ssh-keygen #推送秘钥 [root@m01 ~]# ssh-copy-id 172.16.1.7 [root@m01 ~]# ssh-copy-id 172.16.1.9 #配置 [root@m01 ~]# vim /etc/ansible/hosts [web1] web01 [web3] web03 |
主机组配置
1 2 3 4 5 6 7 8 9 10 11 12 |
[root@m01 ~]# vim /etc/ansible/hosts [web_group] web01 web03 [nfs_group] nfs ansible_ssh_pass='1' #查看组下面的机器 [root@m01 ~]# ansible 'web_group' --list-host hosts (2): web01 web03 |
定义多组操作
1)操作多个组
1 |
[root@m01 ~]# ansible 'web_group,nfs_group' -m ping |
2)定义包含组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[root@m01 ~]# vim /etc/ansible/hosts [web_group] web01 web03 [nfs_group] nfs ansible_ssh_pass='1' [mysql_group] db01 ansible_ssh_pass='1' db02 ansible_ssh_pass='1' [nfs_zu:children] web_group nfs_group |