ansible自动运维

[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),然后返回加密后的密码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值