愿你出走半生,归来仍是少年

Kratos
专注于用户阅读体验的响应式博客主题
  1. 首页
  2. 运维知识
  3. 正文

ansible使用

2022年12月23日 3146点热度 0人点赞 0条评论

Table of Contents

Toggle
    • 一、简介
    • 二、安装
      • 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 测试是否成功

一、简介

Ansible 是一个开源的、自动化运维的强大工具,早前被红帽收购,通过它可实现实现批量系统配置、批量程序部署、批量运行命令等功能。
Ansible 不需要在远程主机上安装client/agents,因为它是基于ssh协议来和远程主机通讯的。因此,使用 Ansible 的前提是,在管理主机可以通过SSH协议远程登录所管服务器。
Ansible 上手容易,学习简单,是每位运维人员必备技能之一。

二、安装

2.1 yum方式在线安装

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

yum install epel-release -y
yum install -y ansible

2.2 pip方式在线安装

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

首先安装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 目录结构

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

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

vim /etc/ansible/ansible.cfg
host_key_checking = False

3.3 设置hosts示例

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

[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

cp /etc/httpd/conf/httpd.conf /etc/ansible/roles/httpd/files/
vim httpd.conf
Listen 8080

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

vim index.html
<h1>This is a ansible playbook test for roles !</h1>

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

在tasks目录下创建如下文件

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中按照运行的顺序排列,注意名称和之后调用的要一致:

cat >> main.yml << EOF
- include: install.yml
- include: config.yml
- include: index.yml
- include: service.yml
EOF

4.5 编写handlers

修改handlers目录下的main.yml

cat >> main.yml << EOF

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

4.6 编写playbook文件

cat > httpd_role.yml << EOF
- hosts: webservers
  remote_user: root
  roles: 
    - role: httpd
EOF

4.7 执行playbook

预测试:
ansible-playbook -C httpd_role.yml

[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

[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

[root@node1:8 /etc/ansible/roles/httpd/tasks]# curl 7.7.7.13:8080
<h1>This is a ansible playbook test for roles !</h1>
标签: 暂无
最后更新:2022年12月23日

liking

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

归档

  • 2026 年 6 月
  • 2026 年 3 月
  • 2026 年 2 月
  • 2025 年 12 月
  • 2025 年 11 月
  • 2025 年 4 月
  • 2025 年 2 月
  • 2025 年 1 月
  • 2024 年 12 月
  • 2024 年 10 月
  • 2024 年 7 月
  • 2024 年 5 月
  • 2024 年 4 月
  • 2023 年 11 月
  • 2023 年 10 月
  • 2023 年 8 月
  • 2023 年 7 月
  • 2023 年 5 月
  • 2023 年 3 月
  • 2023 年 2 月
  • 2022 年 12 月
  • 2022 年 11 月
  • 2022 年 10 月
  • 2022 年 9 月
  • 2022 年 8 月
  • 2022 年 6 月
  • 2022 年 5 月
  • 2022 年 3 月
  • 2022 年 2 月
  • 2022 年 1 月
  • 2021 年 12 月
  • 2021 年 11 月
  • 2021 年 10 月
  • 2021 年 8 月
  • 2021 年 7 月
  • 2021 年 6 月
  • 2021 年 5 月
  • 2021 年 4 月
  • 2021 年 3 月
  • 2021 年 2 月
  • 2021 年 1 月
  • 2020 年 12 月
  • 2020 年 11 月
  • 2020 年 10 月
  • 2020 年 9 月
  • 2020 年 8 月
  • 2020 年 7 月
  • 2020 年 6 月
  • 2020 年 5 月
  • 2020 年 3 月
  • 2020 年 2 月
  • 2019 年 12 月
  • 2019 年 11 月
  • 2019 年 10 月
  • 2019 年 9 月
  • 2019 年 8 月
  • 2019 年 7 月
  • 2019 年 6 月
  • 2019 年 5 月
  • 2019 年 4 月
  • 2019 年 2 月
  • 2018 年 12 月
  • 2018 年 11 月
  • 2018 年 8 月
  • 2018 年 7 月
  • 2018 年 6 月
  • 2018 年 5 月
  • 2018 年 4 月
  • 2018 年 3 月
  • 2018 年 2 月
  • 2018 年 1 月
  • 2017 年 12 月
  • 2017 年 11 月
  • 2017 年 8 月
  • 2017 年 6 月
  • 2017 年 5 月
  • 2017 年 3 月
  • 2016 年 9 月
  • 2016 年 8 月
  • 2016 年 7 月
  • 2016 年 6 月
  • 2016 年 5 月
  • 2015 年 10 月
  • 2014 年 10 月
  • 2014 年 8 月
  • 2014 年 3 月
  • 2013 年 12 月
  • 2013 年 8 月
  • 2013 年 7 月
  • 2013 年 6 月
  • 2013 年 4 月
  • 2012 年 11 月
  • 2012 年 7 月
  • 2012 年 6 月
  • 2012 年 5 月
  • 2012 年 2 月
  • 2011 年 10 月
  • 2011 年 8 月
  • 2011 年 3 月
  • 2010 年 11 月
  • 2010 年 10 月
  • 2010 年 9 月
  • 2010 年 8 月
  • 2010 年 7 月
  • 2010 年 5 月
  • 2010 年 4 月
  • 2010 年 1 月
  • 2009 年 11 月
  • 2009 年 8 月
  • 2009 年 5 月
  • 2009 年 3 月
  • 2009 年 2 月
  • 2008 年 11 月
  • 2008 年 10 月
  • 2008 年 8 月
  • 2008 年 4 月
  • 2007 年 4 月

分类

  • BigData
  • kubernetes
  • linux
  • MongoDB
  • MySQL
  • Oracle
  • PostgreSQL
  • 备份恢复
  • 安装配置
  • 性能优化
  • 技术杂谈
  • 未分类
  • 梦回红楼
  • 测试
  • 生活点滴
  • 网站建设
  • 自制工具
  • 轻松一刻
  • 运维知识
  • 问题处理

COPYRIGHT © 2026 愿你出走半生,归来仍是少年. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang