步骤一: 安装Ansible
首先,我们需要安装Ansible工具。Ansible可以在各个操作系统上运行,所以你可以根据自己的需求选择合适的安装方式。
在Ubuntu上安装Ansible sudo apt update sudo apt install ansible
在CentOS上安装Ansible sudo yum install epel-releasesudo yum install ansible
步骤二: 配置Ansible主机 在安装Ansible之后,我们需要配置Ansible主机,以便能够远程执行命令。
首先,我们需要在Ansible主机上生成SSH密钥对。这将允许我们无需密码登录目标主机。
在root默认目录下执行 即可:
ssh-keygen
接下来,将公钥复制到目标主机上。替换
和
为目标主机的用户名和主机。
ssh-copy-id username>@hostname>
ssh-copy-id root@122.51.215.xx
[root@CICD ~]# ssh-copy-id root@39.105.206.xx
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@39.105.206.xx's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@39.105.206.xx'"
and check to make sure that only the key(s) you wanted were added.
现在,我们可以通过以下命令测试是否可以远程执行命令。
ansible all -m ping
如果一切正常,你将看到类似以下输出。
hostname> | SUCCESS => { "changed": false, "ping": "pong" }
步骤三: 处理报错、修改配置 [root@CICD ~]# ansible all -m ping
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not
match 'all'
如果提示以上信息,则需要修改主机列表配置文件
vim /etc/ansible/hosts
[my_servers]
39.105.206.21 ansible_ssh_user=root
再次进行测试:
[root@CICD ~]
39.xx.206.21 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
步骤四: 准备Ansible Playbook 在开始安装Docker之前,我们需要准备一个Ansible Playbook。Ansible Playbook是一个YAML文件,描述了安装Docker的过程。
在你喜欢的编辑器中创建一个名为install_docker.yml
的文件,并将以下代码复制到文件中。
--- - name: Install Docker hosts: all become: true tasks: - name: Update apt package cache apt: update_cache: yes - name: Install Docker dependencies apt: name: ['apt-transport-https', 'ca-certificates', 'curl', 'software-properties-common'] - name: Add Docker GPG key apt_key: url: state: present - name: Add Docker repository apt_repository: repo: deb [arch=amd64] bionic stable state: present - name: Install Docker apt: name: docker-ce state: present
上述Playbook的作用是:
更新apt软件包缓存。
安装Docker的依赖项。
添加Docker GPG密钥。
添加Docker软件源。
安装Docker。
步骤五: 运行Ansible Playbook 现在,我们准备好运行Ansible Playbook来安装Docker。
使用以下命令运行Playbook。
ansible-playbook install_docker.yml
版权声明:本文内容来自51CTO:mob64ca12dab0a2,遵循CC 4.0 BY-SA版权协议上原文接及本声明。
本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
原文链接:https:
如有涉及到侵权,请联系,将立即予以删除处理。
在此特别鸣谢原作者的创作。
此篇文章的所有版权归原作者所有,与本公众号无关,商业转载建议请联系原作者,非商业转载请注明出处。