vim test.yml
【- name: test
hosts: westos
tasks:
– name: check hostname
shell: hostname
】
ansible-playbook test.yml
ansible westos –list —显示主机列表
ansible westos -m shell -a ‘hostname’ -k —提示输入ssh连接密码,默认key认证
ansible westos -m shell -a ‘touch /mnt/file’ -k
ansible westos -m shell -a ‘touch /mnt/test’ -C —预执行
ansible westos -m shell -a ‘touch /mnt/test’ -T 2 —执行命令超时时间
ansible-doc -s shell
cd .ansible/
vim ansible.cfg
【注释掉true,检测完需要恢复】
ansible westos -m shell -a ‘whoami’ -u westos -K —-提示输入sudo密码
ansible westos -m shell -a ‘whoami’ -u devops -K
ansible westos -m shell -a ‘whoami’ -u devops -k -b —执行sudo切换身份操作
ansible westos -m shell -a ‘whoami’ -u devops -k -b –become-user=root —指定sudo用户身份
====================================================
模块
1、command
ansible westos -m command -a ‘hostname’ —不用写cmd,就可执行
ansible westos -m command -a ‘touch /mnt/file1’ —-创建file1
ansible westos -m command -a ‘ hostname creates=/mnt/file1’—如果存在则不运行
ansible westos -m command -a ‘ hostname removes=/mnt/file1’—如果存在则运行
ansible westos -m command -a ‘chdir=/mnt pwd’ —–执行命令前提前进入指定目录
2、shell(和command类似)
不同点:
ansible westos -m shell -a ‘rm -fr /mnt/*’ —只有shell里可以删除command不能删除
ansible westos -m shell -a ‘ps’ —-查看执行环境,默认为sh
ansible westos -m shell -a ‘executable=/bin/bash ps ax | grep $$’ –指定执行环境,
3、script(受控主机,远程执行脚本)
4、copy(从主控机复制到受控机)
ansible westos -m copy -a ‘src=./westos.sh dest=/mnt/westos’ 把westos.sh复制到受控机的/mnt 起名为westos
ansible westos -m copy -a ‘src=./westos.sh dest=/mnt/westos mode=700’
把westos.sh复制到受控机的/mnt 起名为westos 并赋予权限700
ansible westos -m copy -a ‘src=./westos.sh dest=/mnt/westos mode=700 group=westos owner=westos’
把westos.sh复制到受控机的/mnt 起名为westos 并赋予权限700,组改为westos 文件所有人为westos
ansible westos -m copy -a ‘content=”hello westos” dest=/mnt/westos backup=yes’
直接编写内容给到受控机/mnt,起名westos,在受控主机备份一份
5、fetch(从受控机复制到主控机)
6、file
ansible westos -m file -a ‘path=/mnt/westos1 state=directory mode=700 recurse=yes’
在受控机里建立一个递归目录westos1,其权限为700
ansible westos -m file -a ‘path=/mnt/westos1 state=touch’
在受控机里建立文件westos1
ansible westos -m file -a ‘src=/mnt/westos1 dest=/mnt/westos2 state=hard’
建立westos1和westos2的硬链接
ansible westos -m file -a ‘src=/mnt/westos1 dest=/mnt/westos3 state=link’
建立westos1和westos3的软链接
7、unarchive/archive(解压/压缩)
ansible westos -m archive -a ‘path=/etc dest=/mnt/etc.tar.gz format=gz owner=westos mode=777’
主控机将/etc压缩到受控机的/mnt 拥有者为westos 满权限
tar jcf usr.tar.bz2 /usr/sbin/
将主控机/usr/sbin压缩
ansible westos -m unarchive -a ‘src=./usr.tar.bz2 dest=/opt owner=westos copy=yes’
将主控机 usr.tar.bz2解压到受控机的/opt
ansible westos -m unarchive -a ‘src=/mnt/etc.tar.gz dest=/opt remote_src=yes’
将受控机etc.tar.gz解压到受控机的/opt
9、软件仓库源
ansible westos -m file -a ‘path=/etc/yum.repos.d/westos.repo state=absent’
把原先的仓库删除
ansible westos -m yum_repository -a ‘file=name name=AppStream description=”rhel8.2AppStream” baseurl=http://172.25.254.250/rhel8.2/AppStream gpgcheck=no enabled=yes gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release ‘
给受控机里配置AppStream
ansible westos -m dnf -a ‘name=httpd state=absent autoremove=yes’
删除受控机httpd的依赖性
ansible westos -m dnf -a ‘name=httpd state=latest enablerepo=AppStream’
从AppStream中删除受控机httpd的依赖性
要从 址上下载受控机也需要 络
ip route add default via 172.25.254.250
echo nameserver 114.114.114.114 > /etc/resolv.conf
查看隐藏组件 dnf group list –hidden
10、service/firewalld
ansible westos -m service -a ‘name=httpd state=started enabled=yes’
把受控机安装好的httpd开启
ansible westos -m firewalld -a ‘zone=public service=http permanent=yes state=enabled immediate=yes’
把受控机的火墙开启
zone 火墙的域
sevice 服务名
permanent 永久生效
state 状态
immediate 立即生效
11、user
ansible westos -m user -a ‘name=test’
建立用户test
ansible westos -m user -a ‘name=test uid=6666’
使用户test的uid为6666
ansible westos -m user -a ‘name=test group=westos’
使用户test的组改为westos
ansible westos -m user -a ‘name=test groups=westos’
使用户test的附加组改为westos
ansible westos -m user -a ‘name=test groups=72’
更改test的附加组为72
ansible westos -m user -a ‘name=test groups=westos append=yes’
在附加组72再加附加组westos
ansible westos -m user -a ‘name=test comment=”test user”‘
给用户test加解释
openssl passwd -6
生成密码密钥
ansible westos -m user -a “name=test password=’$6$DifJmZTR.7w6DFOX$/K8xYETJhQtdnAtMmNXMfX.M30p04uGhExWlp2N7aVQJA4JzZKhwm3GsKoTBN4r4FpG/0l6qlAH/gkFSfXEAP1′ generate_ssh_key=yes”
给用户密码生成密钥
ansible westos -m user -a ‘name=test state=absent’
删除用户
12、group
ansible westos -m group -a ‘name=test’
给受控机新建组名为test
13、lineinfile
在受控机里/mnt建立文件内容如下:
hello westos
hello test
hello linux
主控机:
ansible westos -m lineinfile -a ‘path=/mnt/westos line=”hello westos” ‘
给文件里加hello westos,因为文件里有,所以不执行
ansible westos -m lineinfile -a ‘path=/mnt/westos regexp=”^hello” line=”hello westos” ‘
替换带有hello为hello westos ,这里替换了最后一行
ansible westos -m lineinfile -a ‘path=/mnt/westos regexp=”test” line=”westos lee” ‘
把带有test的替换为westos lee
ansible westos -m lineinfile -a ‘path=/mnt/westos regexp=”(h.{4}).*(w.{5})” line=”1″ backrefs=yes ‘
将h后有四个字符w后 有五个字符的内容 只保留第一段
ansible westos -m lineinfile -a ‘path=/mnt/westos line=”#########ok########”‘
给最后一行添加#########ok########
ansible westos -m lineinfile -a ‘path=/mnt/westos line=”#########ok########” insertafter=EOF’
给最后一行添加#########ok########
ansible westos -m lineinfile -a ‘path=/mnt/westos line=”#########ok########” insertbefore=BOF’
给第一行添加#########ok########
ansible westos -m lineinfile -a ‘path=/mnt/westos line=”#########ok########” insertbefore=test’
给test前加#########ok########
playbook
vim test.yml
{
– name: test
hosts: westos
tasks:
– lineinfile:
path: /mnt/westos —–传递到/mnt/westos
line: |+ —–换行
123
456
789
}
ansible-playbook test.yml
14、 replace/setup/debug
ansible westos -m replace -a ‘path=/mnt/westos regexp=”hello” replace=”who” backup=yes’
把hello代替为who
ansible westos -m setup
查看远程主机的基本信息
}
ansible-playbook test.yml
文章知识点与官方知识档案匹配,可进一步学习相关知识云原生入门技能树首页概览8697 人正在系统学习中
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!