ansible使用

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

一、简介

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>

liking

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

文章评论