
[defaults]
inventory=/home/student/ansible/inventory (定义inventoryd的位置)
remote_user=student (以哪个用户身份登录到远程服务器执行任务)
[privilege_escalation]
become=True (是否提权)
become_ask_pass=False (提权是否需要验证密码)
become_method=sudo (提权后使用哪个用户执行)
become_user=root (提权使用什么方法)
ansible-navigator (-m stdout 直接显示在终端,不进入交互式模块)
(2)在受控节点执行命令(不加-m 选项时默认command)
①command(默认)依赖于python
[student@workstation ansible]$ ansible all -m command -a "hostname"
[student@workstation ansible]$ ansible all -a "hostname"
[student@workstation ansible]$ ansible all -a "ls /etc/passwd | wc -l"
###报错,不支持管道,重定向等操作
②shell 依赖于python
[student@workstation ansible]$ ansible all -m shell -a 'hostname'
[student@workstation ansible]$ ansible all -m shell -a "ls /etc/passwd | wc -l"
③raw
[student@workstation ansible]$ ansible all -m raw -a 'hostname'
检查是否出现语法的错误
[student@workstation ansible]$ ansible-navigator run group.yml --syntax-check -m stdout
运行剧本
[student@workstation ansible]$ ansible-navigator run group.yml -m stdout
创建组
创建文件:
copy
replace
get_url
yum_repository
dnf
service
___
user
append选项 添加组或附加组时追加不覆盖
练习:
在node1创建一个用户Tom,用户id指定为1234,用户的密码设置为redhat,以sha512哈希格式进行加密,并设置30天后密码过期
验证:
①[student@workstation ansible]$ ansible node1 -a 'id Tom'
②[student@workstation ansible]$ ssh Tom@node1
询问密码
[Tom@node1 ~]$ logout
Connection to node1 closed.
③[student@workstation ansible]$ ansible node1 -a 'chage -l Tom'
拓展:
password选项:在ansible中,一般会通过过滤器处理数据。如果应用过滤器,需要在变量名,‘字符串’ 后面加上| 竖线后面加上应用的过滤器名称(default)
{{ pw_developer | password_hash('sha512') }}:这个表达式使用了Ansible的过滤器password_hash,它接受一个密码和一个加密方法(这里是SHA-512),然后返回加密后的密码。