ansible简单配置

ansible node1,node2,node3 --list-hosts : 列出匹配到的主机

在/ect/ansible/hosts里面加入主机清单,在组外面的是单台主机,分组控制比较方便,用ansible命令发送指令

node6 : 不属于任何组需要写在主机清单最上面

[组名]

node1

node2

node3

[组名]

node4

node5

ansible all --list-hosts : 查看匹配到的主机清单

ansible ungrouped --list-hosts : 查看不属于任何组的主机

[db:children] : 嵌套组

组名1

组名2

ansible ???.example.com --list-hosts : 列出任何三个开头的主机

ansible '*.example.com' --list-hosts : 列出任何开头的主机

ansible '*.example.com,!*.lab.example.com' --list-hosts : 列出除了所有开头的.lab.example.com的.example.com 的主机

ansible 'prod,172*,*lab*' --list-hosts : 列出prod组里的172开头的或包含lab的主机

ansible 'db,&london' --list-hosts : 匹配到属于db组和london组的主机

ansible all --limit db1.example.com --list-hosts : 执行失败的主机指定在db1.example.com上再执行

ansible all --limit @hosts.txt --list-hosts : 把执行失败的主机在再执行一遍,必须要在主机清单中

ansible all -i hosts.txt --list-hosts : 把执行失败的主机在再执行一遍,不必非要在主机清单中

ansible node1 -m shell -a 'pwd' -u admin -k : 指定被控端用户admin

ansible配置文件/etc/ansible/ansible.cfg

remote_user = devop : 指定被控端连接的用户

[privilege_escalation]

become=True : 是否要提权

become_method=sudo : 用到sudo提权

become_user=root : 提权到root身份

become_ask_pass : 提权时,是否交互提示密码验证

定义ansible的配置文件

vim /etc/ansible/ansible.cfg

inventory = /etc/ansible/inventory #定义主机清单的路径

#remote_tmp = ~/.ansible/tmp #定义被控端的脚本临时存放目录

#local_tmp = ~/.ansible/tmp #定义主控端的脚本临时存放目录

forks = 5 #定义并发数

remote_user = devops #指定被控端连接的用户

ask_pass = True #密码密钥,默认时密钥验证

remote_port = 22 #被控端主机的端口号

host_key_checking = False #自动接受被控端的公钥

[privilege_escalation]

become=True #是否要提权

become_method=sudo #用sudo提权

become_user=root #提权到root用户

become_ask_pass=False #提权时不需要输入密码

#定义ansible的配置文件的优先级

1.ANSIBLE_CONFIG定义的配置文件路径,优先级最高

ANSIBLE_CONFIG=/var/lib/ansible/ansible.cfg

2.其次是当前目录下面的ansible.cfg

./ansible.cfg

3.其次是~/.ansible.cfg

4.最后是/etc/ansible/ansible.cfg

#ansible帮助文档

1.ansible官方文档

2.ansible内置帮助

ansible-doc -l 查看所有的模块

ansible-doc 模块名 查找该模块的参数

相关推荐
一切皆文件linux6 小时前
网站集群批量管理-密钥认证与Ansible模块
ansible
让美好继续发生1 天前
ansible学习
学习·ansible
有谁看见我的剑了?2 天前
ansible学习之 Facts
ansible
peanutfish2 天前
Chapter 4 RH294 RHEL Automation with Ansible
linux·ansible·yaml
henan程序媛2 天前
jenkins项目发布基础
运维·gitlab·ansible·jenkins
pyliumy4 天前
ansible 配置
大数据·ansible
运维小白。。4 天前
自动化运维工具 Ansible
运维·自动化·ansible
weixin_438197384 天前
ansible之playbook\shell\script模块远程自动安装nginx
linux·服务器·ansible
peanutfish4 天前
Chapter 5 RH294 RHEL Automation with Ansible
linux·ansible·yaml
人类群星闪耀时5 天前
自动化运维的利器:Ansible、Puppet和Chef详解
运维·自动化·ansible