ansible安装
epel源下载(从网络搜索)
这里用拷贝方式下载到/mnt
wget http://172.25.254.250/ansible/ansible-2.9.11-1.el8.noarch.rpm
wget http://172.25.254.250/ansible/sshpass-1.06-9.el8.x86_64.rpm
dnf install sshpass-1.06-9.el8.x86_64.rpm -y
dnf install ansible-2.9.11-1.el8.noarch.rpm -y
ansible --version -----查看ansible安装信息(包括python)
=============================================
ssh-keygen ----生成密钥
vim ansi.sh
AUTOSSH()
{
/usr/bin/expect << EOF
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.25.254.$I
expect{
"yes/no" { send "yes\r";exp_continue }
"password" { send "westos\r" }
}
expect eof
EOF
}
for I in 204 203
do
AUTOSSH
done
=================================================
清单
cd /etc/ansible ----配置文件在/etc/ansible
vim hosts
{
[westos]
172.25.254.[200:203]
[westos1]
172.25.254.204
[westosall:children]
westos
westos1
[node]
nodea.westos.org
nodeb.westos.org
}
ansible westos --list-hosts 显示westos的清单
ansible all --list-hosts 显示所有清单
ansible westos1 --list 显示westos1的清单
ansible upgrouped --list
ansible 172* --list 显示以172开头的清单
ansible '~node' --list 显示以ndoe开头的清单
ansible 'westos:westos1' --list 显示是在westos或westos1里的清单
ansible 'westos:&westos1' --list 显示是在westos和westos1里的清单
vim /mnt/westos
{
[westos]
172.25.254.[200:203]
}
ansible -i /mnt/westos westos --list 显示/mnt/westos里的清单
ansible 'westos:!westos1' --list 显示在westos里但不在westos1里的清单
ansible '~(node|172)' --list 显示ndoe开头或者172开头的清单
==================================================
ansible westos -m ping -k ####ping一下[westos]中的用户
ansible westos -m shell -a 'touch /mnt/file1' -k ###用shell模块建立/mnt/file1
ansible westos -m shell -a 'whoami' -k ###查看登陆用户 默认为root
ansible westos -m shell -a 'whoami' -k -u westos ###用westos登陆查看用户为westos
================================================
构建用户级ansible操作环境
nodea(104)中:
useradd devops
su - devops -----切换到用户devops
mkdir .ansible
cd .ansible/
vim inventory
【
[westos]
172.25.254.204
】
vim ansible.cfg
【
[defaults] ###基本信息设定
inventory = ~/.ansible/inventory ###清单调用地址
host_key_checking = False ###连接是否需要输入yes(false为不需要,注释掉就需要输入yes)
remote_user = devops ###受管主机上显示的用户名称
module_name = shell ###开启模块为shell,默认为command
[privilege_escalation] ###身份信息设定 -------等到下面全部设置完成后再把注释去掉
#become=True ###连接后是否自动切换用户
#become_method=sudo ###设定切换用户方式
#become_user=root ###在受管主机中切换到的用户
#become_ask_pass=False ###是否需要为切换方式设定密码
】
ansible westos -m shell -a 'useradd devops' -k -u root ####在westos清单中的主机上添加devops用户并设置密码为westos
ansible westos -m shell -a 'echo westos | passwd --stdin devops' -k -u root
ansible westos -m shell -a 'echo "devops ALL=(root) NOPASSWD: ALL" >> /etc/sudoers' -k -u root ####超级用户登陆devops免密登陆
ansible westos -m shell -a 'whoami' -k ###查看用户是否为devops
ansible westos -m shell -a 'ls /home/devops/.ssh' -k ###查看是否有此目录
ansible westos -m shell -a 'mkdir -p /home/devops/.ssh' -k ####若无,建立目录(进入204主机,切换到devops用户,ls -all /home/devops 就能看到.ssh隐藏文件)
ssh-keygen ###生成私钥
ansible westos -m shell -a 'chmod 700' /home/devops/.ssh/ -k
ansible westos -m copy -a 'src=/home/devops/.ssh/id_rsa.pub dest=/home/devops/.ssh/authorized_keys mode=0600 owner=devops group=devops' -k ###复制私钥到authorized_keys下
测试:ssh -l devops 172.25.254.204 使用devops用户登陆204主机不需要密码(westos清单中的主机都可以)