1 Ansible 安装
pip install ansible
2 Ansible配置
-
ansible.cfg配置文件,按照默认配置方式配置,建议参考以下设置,其他项都注释掉
[defaults] host_file = /etc/ansible/hosts inventory = /etc/ansible/hosts library = /usr/share/ansible/module/ remote_port = 22 private_key_file = /root/.ssh/id_rsa gathering = smart gather_timeout = 10 system_warnings = False deprecation_warnings = False command_warnings = False [ssh_connection] ssh_args = -C -o ControlMaster=auto -o ControlPersist=5d [persistent_connection] connect_timeout = 600 connect_retries = 30 connect_interval = 1
-
ansible.cfg文件存放路径可以是playbook目录下、用户家目录下、/etc/ansible/目录下,优先级从高到低,执行ansible时,检查这些目录下的是否存在ansible.cfg文件,防止不必要的问题发生。
-
inventory文件,默认使用/etc/ansible/hosts,也可以放在任意位置,通过命令行参数-i $hosts_path/hosts 指定。配置方法
[linux] --> 主机组 rhel ansible_host=192.168.1.1 --> 主机和主机ip [linux:vars] --> 主机组变量(自定义,linux主机不用写) [Windows] win ansible_host=192.168.1.2 [Windows:vars]--> 主机组变量(windos指定用户,连接方式) ansible_user="Administrator" ansible_password="eds1234*" ansible_port=5985 ansible_connection="winrm" 以下两个变量设置winrm连接,若出现超时情况,增加timeout时间 ansible_winrm_operation_timeout_sec=600 ansible_winrm_read_timeout_sec=700
Ansible 使用–常用方法
- ansible 命令行方式
1. ansible rhel -m ping 测试连通
rhel --> hosts文件中的主机
-m ping 模块ping(更多模块参照官方module)
2. ansible rhle -m shell -a "ls /root/"
-a 指定模块参数
- ansible-playbook
1. 最简目录结构
- test.yml
- roles/
- test/
- tasks/
- main.yml
test.yml
---
- hosts: linux
roles:
- test
main.yml
---
- name: test debug
debug:
msg: "hello world"
执行命令
ansible-playbook test.yml
结果
[root@IoT-RH72J64-4 ~]# ansible-playbook test.yml
PLAY [linux] ****************************************************************************
TASK [test debug] ***********************************************************************
ok: [192.168.1.1] => {
"msg": "hello world"
}
PLAY RECAP ******************************************************************************
192.168.1.1 : ok=1 changed=0 unreachable=0 failed=0
2. 单个playbook写法:
test.yml
---
- hosts: linux
tasks:
- name: test debug
debug:
msg: "hello world"
3. anisble-plyabook 参数
1. ansible-playbook test.yml -i hosts -e "{'name': 'test'}"
-e 指定外部参数,json语法格式
2. ansible-playbook test.yml -i hosts -e "@var.yml"
-e 指定外部变量文件,yml语法格式
var.yml参照
---
name: test
path_list: ---列表
- /root/test1
- /root/test2
par_dict: ---字典
path1: /root/test1
path2: /root/test2
- ansible-galaxy
1. 参考官方连接,说明文档https://docs.ansible.com/ansible/latest/reference_appendices/galaxy.html#dependencies
官方仓库https://galaxy.ansible.com/search?vendor=false&order_by=-relevance&page_size=10
2. 注意事项
- galaxy 的方法目前只支持以role为单位的上传和下载
3. meta/main.yml写法(参照)
---
dependencies: [] -->依赖role,下载时自动下载
galaxy_info:
author: nec
description: meta test
company: nec
license: "license (BSD, MIT)"
min_ansible_version: 2.4
platforms:
- name: EL
versions:
- 6
- 7
galaxy_tags:
- test
- ansible-lint 需要下载安装,使用命令
pip install ansible-lint==3.4.21
使用方法
ansible-lint playbook.yml -vvv
- 更多使用方法参照官方连接
https://docs.ansible.com/