1、ansible
控制端安装
yum -y install epel-release ansible libselinux-python
配置域名解析
192.21.131.81 master 192.21.131.99 node1
创建密钥链接
控制节点设置密钥:ssh-keygen
分发至工作节点:ssh-copy-id node1

此处自定义ansible 配置目录方便管理
[root@localhost ~]# mkdir ~/ansible
[root@localhost ~]# vim ~/ansible/ansible.cfg
[defaults] inventory = ~/ansible/hosts #forks = 5 #ssh机器最大并发数 #ask_pass =True #是否使用密钥认证,否则使用密码 #host_key_checking = False #是否检验密钥
以上是定义主机组,不同服务定义不同主机组!!!!
命令解析:ping 模块 (模块即python脚本)
查看所有主机:
cd ~/aisible #进入自定义的配置目录
[root@localhost ansible]# ansible all --list-hosts
hosts (1):
node1
[root@localhost ansible]# ansible node1 -m ping #ping单个主机,多个主机逗号隔开
node1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@localhost ansible]# ansible test -m ping #ping组,多个组逗号隔开
node1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
命令解析:command 模块:执行所有shell 命令 (模块即python脚本)
[root@localhost ansible]# ansible test -m command -a "ls " node1 | CHANGED | rc=0 >> anaconda-ks.cfg [root@localhost ansible]# ansible test -a "ls " #默认使用command node1 | CHANGED | rc=0 >> anaconda-ks.cfg

命令解析:shell 模块


[root@localhost ansible]# ansible node1 -m command -a "ps -ef | wc -l" node1 | FAILED | rc=1 >> error: garbage option Usage: ps [options] Try 'ps --help <simple|list|output|threads|misc|all>' or 'ps --help <s|l|o|t|m|a>' for additional help text. For more details see ps(1).non-zero return code [root@localhost ansible]# ansible node1 -m shell -a "ps -ef | wc -l" node1 | CHANGED | rc=0 >> 120
##### -a "chdir=/tmp touch xxxxx" 切换目录创建文件

命令解析:script 模块 执行脚本模块
[root@localhost ansible]# mkdir script #创建脚本目录便于管理 ansible.cfg hosts script [root@localhost ansible]# pwd /root/ansible [root@localhost ansible]# ansible node1 -m script -a "./script/test.sh" #-a 传输的是参数脚本路径 node1 | CHANGED => { "changed": true, "rc": 0, "stderr": "Shared connection to node1 closed.\r\n", "stderr_lines": [ "Shared connection to node1 closed." ], "stdout": "", "stdout_lines": [] } [root@localhost ansible]# ansible node1 -m shell -a "ls " node1 | CHANGED | rc=0 >> anaconda-ks.cfg ssss #######################脚本执行后生成新的文件
命令解析:file 模块 #幂等性保证命令执行多变后,写入文件配置数据不会重复



命令解析:copy 模块

幂等性测试:如图copy 一次后,再次copy 实际没有执行copy 文件操作,但是日志输出正常


命令解析:fetch 模块

命令解析:lineinfile|replace 模块



命令解析:user 模块


命令解析:yum_repository模块

playbook:
[root@localhost ansible]# cat test.yml --- - hosts: test tasks: - name: test---test ping: [root@localhost ansible]# ansible-playbook test.yml PLAY [test] **************************************************************************************************************************************** TASK [Gathering Facts] ***************************************************************************************************************************** ok: [node1] TASK [test---test] ********************************************************************************************************************************* ok: [node1] PLAY RECAP ***************************************************************************************************************************************** node1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

Roles:

[root@localhost ansible]# ansible-galaxy init ~/ansible/roles/issue - Role /root/ansible/roles/issue was created successfully [root@localhost ansible]# ls ansible.cfg hosts roles script test.yml [root@localhost ansible]# ls roles/ issue [root@localhost ansible]# ls roles/issue/ defaults files handlers meta README.md tasks templates tests vars
浙公网安备 33010602011771号