1 安装ansible
centos安装:
1 | # yum -y install epel-release //更新本地安装库 |
2 ansible的配置文件
/etc/ansible/ansible.cfg
主配置文件/etc/ansible/hosts
Inventory/usr/bin/ansible-doc
帮助文件/usr/bin/ansible-playbook
指定运行任务文件
3 定义一个Linux集群组
(1)修改/etc/ansible/hosts
1 | # cd /etc/ansible/ |
(2)指定相应的配置文件
大部分时候,对/etc/ansible/hosts
文件的修改,需要root
权限,而往往工作中我们都是使用的普通用户进行登录,所以一般在自己的文件夹中单独创建一个hosts文件,然后在执行命令的时候指定解析自己的hosts文件即可,文件修改语法不变,执行命令如下:
-i {配置文件路径}
1
# ansible webserver -i ~/.ansible/hosts -m command -a 'ls ~'
4 密钥连接
在进行批量管理的时候,通常需要密码认证,这个时候有两种方式:
每次执行命令都输入一次密码(比较麻烦)
-k
,是小写的,然后输入密码即可
1
# ansible webserver -i ~/.ansible/hosts -m command -a 'ls ~' -k
使用ssh方式将密钥传输到指定的主机
1
2
3
4
5//首先生成ssh密钥
# ssh-genkey -t rsa //连续按enter即可
//将同ssh密钥拷贝到远程主机
# ssh-copy-id -i .ssh/id_rsa.pub zhangsan@192.168.1.10 //-i表示指定的ssh密钥
# ssh-copy-id zhangsan@192.168.1.10 //或者直接在ssh密钥的文件夹
5 基础教程用法
ping
enable this config,
record_host_keys=Flase
. And add the host’s ip address in the file(/etc/ansible/hosts
).1
2$ ssh-keygen$ ssh-copy-id [ip_address]
$ ansible -m pingansible-playbook
: execute a script.1
2$ ansible-playbook hello.yml
$ cat hello.ymlansible-vault
: encrypt or decrypt a file, to ensure the secrity of the file.1
2$ ansible-vault encrypt hello.yml
$ ansible-vault decrypt hello.ymlansible-console
: A interact command line of ansible.
Ansible Module:
use module:
- command: execute only system command
- shell: execute the command via shell process
- raw: execute the low level command
1
2$ ansible [host group] -m [module name] -a "[command line]"
$ ansible webserver -m command/shell -a "ls /etc"Change default module:
change config file(/etc/ansible/ansible.cfg), enable the line and change it.
1
module_name = command // change it to shellmodule_name = shell
script
: Run a script on the remote hosts1
$ ansible [host group] -m script -a "test.sh"
copy
: Copy files from current host to remote hosts.1
$ ansible [host group] -m copy -a "src=/etc/systemfig dest=data/os.txt" owner=[user name] mode=600$ ansible webserver -m copy -a "src=/home/centos/pig/1.txt dest=/home/root123/2.txt owner=root123 mode=600"
fetch
: Fetch a file from remote hosts. Thismust
be a file, not a directory. Likecopy
.1
$ ansible all -m fetch -a "src=/etc/os-release dest=/home/centos/pig/tem"
file
: change file’s attributes, create a empty file.1
$ ansible webserver -m file -a "path=/home/root123/new.txt state=touch"
unarchive
: unzip a file.1
$ ansible webserver -m unarchive -a "src=./etc.tar.gz dest=/home/root123/ owner=root123 mode=600" // push the zip file to the remote hosts and unzip it.
6 ansible转义
ansible
中想执行 lvm vgs | grep ceph | awk '{print $1}' | xargs -I {} lvm vgremove -f {}
命令
排查结果发现 lvm vgs | grep ceph | awk '{print $1}' 中 awk '{print $1}'
失效
最终发现是 $
符出了问题,添加转义字符 \
后,ok
1 | ansible all -i inventories/production/infrastructure -m shell -a "lvm vgs | grep ceph | awk '{print \$1}'| xargs -I {} lvm vgremove -f {}" --become |
Reference
- https://segmentfault.com/a/1190000038230424
- Ansible中文权威指南
- automate-with-ansible
- automate-with-ansible in simple chinese
- https://blog.csdn.net/Man_In_The_Night/article/details/93464540
写在最后
欢迎大家关注鄙人的公众号【麦田里的守望者zhg】,让我们一起成长,谢谢。