前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ansible使用

ansible使用

原创
作者头像
雪人
发布2023-01-04 18:22:12
4650
发布2023-01-04 18:22:12
举报
文章被收录于专栏:DataOpsDataOps

一、简介

Ansible 是一个开源的、自动化运维的强大工具,早前被红帽收购,通过它可实现实现批量系统配置、批量程序部署、批量运行命令等功能。

Ansible 不需要在远程主机上安装client/agents,因为它是基于ssh协议来和远程主机通讯的。因此,使用 Ansible 的前提是,在管理主机可以通过SSH协议远程登录所管服务器。

Ansible 上手容易,学习简单,是每位运维人员必备技能之一。

二、安装

2.1 yum方式在线安装

在线安装需要提前配置好 epel,然后 yum install 即可:

代码语言:txt
复制
yum install epel-release -y
yum install -y ansible

2.2 pip方式在线安装

另外,由于 Ansible 是用python开发的,也可基于pip来安装配置ansible,如下:

代码语言:txt
复制
首先安装pip
yum install python-pip
然后使用pip国内源,更新pip自身
pip install --upgrade --trusted-host mirrors.aliyun.com -i http://mirrors.aliyun.com/pypi/simple/ pip
再使用pip国内源,安装ansible
pip install --trusted-host mirrors.aliyun.com -i http://mirrors.aliyun.com/pypi/simple/ ansible

2.3 离线安装(略)

三、使用

3.1 Ansible 目录结构

代码语言:txt
复制
/etc/ansible
├── ansible.cfg 默认配置文件,配置ansible工作特性,建议在每个项目目录下创建独有的配置文件
├── hosts       主机清单
└── roles       存放角色目录

3.2 一般设置参数为不检查key

vim /etc/ansible/ansible.cfg

host_key_checking = False

3.3 设置hosts示例

代码语言:txt
复制
vim /etc/ansible/hosts
[webservers]
7.7.7.13 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='passwd'

3.4 主机连通性测试

ansible webservers -i /etc/ansible/hosts -m ping

代码语言:txt
复制
[root@node1:8 /etc/ansible/roles/httpd/tasks]# ansible webservers -i /etc/ansible/hosts -m ping
7.7.7.13 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

四、最佳实践:httpd配置

4.1 在roles目录下执行,创建httpd目录结构

ansible-galaxy init httpd

4.2 准备httpd相关文件

将准备好的httpd.conf文件copy到/etc/ansible/roles/httpd/files/ 下,并修改端口为8080

代码语言:txt
复制
cp /etc/httpd/conf/httpd.conf /etc/ansible/roles/httpd/files/
vim httpd.conf
Listen 8080

4.3 在files目录下创建index.html文件

代码语言:txt
复制
vim index.html
<h1>This is a ansible playbook test for roles !</h1>

4.4 创建task任务,在tasks/main.yml中调用

在tasks目录下创建如下文件

代码语言:txt
复制
cat > install.yml << EOF
- name: install httpd package
  yum: name=httpd
EOF

cat > config.yml << EOF
- name: config file
  copy: src=httpd.conf dest=/etc/httpd/conf/ backup=yes
  notify: restart
EOF

cat > index.yml << EOF
- name: index.html
  copy: src=index.html dest=/var/www/html/
EOF

cat > service.yml << EOF
- name: start service
  service: name=httpd state=started enabled=yes
EOF

main.yml中按照运行的顺序排列,注意名称和之后调用的要一致:

代码语言:txt
复制
cat >> main.yml << EOF
- include: install.yml
- include: config.yml
- include: index.yml
- include: service.yml
EOF

4.5 编写handlers

修改handlers目录下的main.yml

代码语言:txt
复制
cat >> main.yml << EOF

- name: restart 
  service: name=httpd state=restarted
EOF

4.6 编写playbook文件

代码语言:txt
复制
cat > httpd_role.yml << EOF
- hosts: webservers
  remote_user: root
  roles: 
    - role: httpd
EOF

4.7 执行playbook

预测试:

ansible-playbook -C httpd_role.yml

代码语言:txt
复制
[root@node1:8 /etc/ansible/roles/httpd/tasks]# ansible-playbook -C httpd_role.yml

PLAY [webservers] **************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************
ok: [7.7.7.13]

TASK [install httpd package] ***************************************************************************************************************
changed: [7.7.7.13]

TASK [httpd : config file] *****************************************************************************************************************
changed: [7.7.7.13]

TASK [httpd : index.html] ******************************************************************************************************************
changed: [7.7.7.13]

TASK [httpd : start service] ***************************************************************************************************************
changed: [7.7.7.13]

RUNNING HANDLER [httpd : restart] **********************************************************************************************************
changed: [7.7.7.13]

PLAY RECAP *********************************************************************************************************************************
7.7.7.13                   : ok=6    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

执行:

ansible-playbook httpd_role.yml

代码语言:txt
复制
[root@node1:8 /etc/ansible/roles/httpd/tasks]# ansible-playbook httpd_role.yml

PLAY [webservers] **************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************
ok: [7.7.7.13]

TASK [install httpd package] ***************************************************************************************************************
changed: [7.7.7.13]

TASK [httpd : config file] *****************************************************************************************************************
changed: [7.7.7.13]

TASK [httpd : index.html] ******************************************************************************************************************
changed: [7.7.7.13]

TASK [httpd : start service] ***************************************************************************************************************
changed: [7.7.7.13]

RUNNING HANDLER [httpd : restart] **********************************************************************************************************
changed: [7.7.7.13]

PLAY RECAP *********************************************************************************************************************************
7.7.7.13                   : ok=6    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

4.8 测试是否成功

curl 7.7.7.13:8080

代码语言:txt
复制
[root@node1:8 /etc/ansible/roles/httpd/tasks]# curl 7.7.7.13:8080
<h1>This is a ansible playbook test for roles !</h1>

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、简介
  • 二、安装
    • 2.1 yum方式在线安装
      • 2.2 pip方式在线安装
        • 2.3 离线安装(略)
        • 三、使用
          • 3.1 Ansible 目录结构
            • 3.2 一般设置参数为不检查key
              • 3.3 设置hosts示例
                • 3.4 主机连通性测试
                • 四、最佳实践:httpd配置
                  • 4.1 在roles目录下执行,创建httpd目录结构
                    • 4.2 准备httpd相关文件
                      • 4.3 在files目录下创建index.html文件
                        • 4.4 创建task任务,在tasks/main.yml中调用
                          • 4.5 编写handlers
                            • 4.6 编写playbook文件
                              • 4.7 执行playbook
                                • 4.8 测试是否成功
                                领券
                                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档