Ansible 基础入门
安装
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum makecache
yum install -y ansible
查看ansible版本
[root@k8s-master ~]# ansible --version
ansible 2.9.21
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, Nov 16 2020, 22:23:17) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
通过config file 可以得出默认配置文件的目录
/etc/ansible/ansible.cfg # ansible 配置文件
/etc/ansible/hosts # inventorys目录,以后被管节点的服务器都会放在这里
ansible.cfg
[defaults]
# some basic default values...
#inventory = /etc/ansible/hosts #主机列表配置文件
#library = /usr/share/my_modules/ #库文件存放目录
#remote_tmp = $HOME/.ansible/tmp #临时python命令存放在远程主机的目录
#local_tmp = $HOME/.ansible/tmp #本地主机临时命令执行目录
#forks = 5 #默认并发数
#poll_interval = 15
#sudo_user = root #默认sudo用户
#ask_sudo_pass = True #每次执行ansible命令是否询问ssh密码
#ask_pass = True
#transport = smart
#remote_port = 22
#module_lang = C
#module_set_locale = True
#host_key_checking = False #检查对应服务器的host_key,建议取消注释
#log_path=/var/log/ansible.log #日志文件,建议启用
module_name = shell # 模块默认模块是 command
hosts文件
[java]
172.16.10.60 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
[apache]
web01 172.16.10.21 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
web02 172.16.10.21 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
[nginx]
nginx-1 172.16.30.1
nginx-2 172.16.30.2
[web:children]
apache
nginx
[nginx:vars]
ansible_ssh_user="user"
ansible_ssh_port=22
ansible_password=111111
ansible_ssh_private_key_file=~/.ssh/keys/deploy