- 参数
-
参数 说明 -a ‘Arguments’, —args=’Arguments’ 命令行参数 -m NAME, —module-name=NAME 执行模块的名字,默认使用 command 模块,所以如果是只执行单一命令可以不用 -m参数 -i PATH, —inventory=PATH 指定库存主机文件的路径,默认为/etc/ansible/hosts. -u Username, —user=Username 执行用户,使用这个远程用户名而不是当前用户 -U —sud-user=SUDO_User sudo到哪个用户,默认为 root -k —ask-pass 登录密码,提示输入SSH密码而不是假设基于密钥的验证 -K —ask-sudo-pass 提示密码使用sudo -s —sudo sudo运行 -S —su 用 su 命令 -l —list 显示所支持的所有模块 -s —snippet 指定模块显示剧本片段 -f —forks=NUM 并行任务数。NUM被指定为一个整数,默认是5。 #ansible testhosts -a “/sbin/reboot” -f 10 重启testhosts组的所有机器,每次重启10台 —private-key=PRIVATE_KEY_FILE 私钥路径,使用这个文件来验证连接 -v —verbose 详细信息 all 针对hosts 定义的所有主机执行 -M MODULE_PATH, —module-path=MODULE_PATH 要执行的模块的路径,默认为/usr/share/ansible/ —list-hosts 只打印有哪些主机会执行这个 playbook 文件,不是实际执行该 playbook 文件 -o —one-line 压缩输出,摘要输出.尝试一切都在一行上输出。 -t Directory, —tree=Directory 将内容保存在该输出目录,结果保存在一个文件中在每台主机上。 -B 后台运行超时时间 -P 调查后台程序时间 -T Seconds, —timeout=Seconds 时间,单位秒s -P NUM, —poll=NUM 调查背景工作每隔数秒。需要- b -c Connection, —connection=Connection 连接类型使用。可能的选项是paramiko(SSH),SSH和地方。当地主要是用于crontab或启动。 —tags=TAGS 只执行指定标签的任务 例子:ansible-playbook test.yml –tags=copy 只执行标签为copy的那个任务 —list-tasks 列出所有将被执行的任务 -C, —check 只是测试一下会改变什么内容,不会真正去执行;相反,试图预测一些可能发生的变化 —syntax-check 执行语法检查的剧本,但不执行它 -l SUBSET, —limit=SUBSET 进一步限制所选主机/组模式 –limit=192.168.0.15 只对这个ip执行 —skip-tags=SKIP_TAGS 只运行戏剧和任务不匹配这些值的标签 —skip-tags=copy_start -e EXTRA_VARS, —extra-vars=EXTRA_VARS 额外的变量设置为键=值或YAML / JSON -l —limit 对指定的 主机/组 执行任务 —limit=192.168.0.10,192.168.0.11 或 -l 192.168.0.10,192.168.0.11 只对这个2个ip执行任务 - Inventory file for multiple servers
- vim /etc/ansible/hosts
-
# Application servers [app] 192.168.60.4 192.168.60.5 # Database server [db] 192.168.60.6 # Group 'multi' with all servers [multi:children] app db # Variables that will be applied to all servers [multi:vars] ansible_ssh_user=vagrant ansible_ssh_private_key_file=~/.vagrant.d/insecure_private_key
1.The first block puts both of our application servers into an ‘app’ group.
-
2. The second block puts the database server into a ‘db’ group.
-
3. The third block tells ansible to define a new group ‘multi’, with child groups, and we add in both the ‘app’ and ‘db’ groups.
-
4. The fourth block adds variables to the multi group that will be applied to all servers within multi and all its children
- ansible multi -a "hostname"
- ansible multi -a "hostname" -f 1 这一次是只是用了一个线程
- servers have disk space available for our application:
- ansible multi -a "df -h"
- make sure there is enough memory on our servers
- ansible multi -a "free -m"
- Make changes using Ansible modules
- ansible multi -s -m yum -a "name=ntp state=present" -s 的意思是sudo
- ansible multi -s -m service -a "name=ntpd state=started enabled=yes"
- ansible multi -s -a "service ntpd stop"
- ansible multi -s -a "ntpdate -q 0.rhel.pool.ntp.org"
- ansible multi -s -a "service ntpd start"
- Configure groups of servers, or individual servers
- ansible app -s -m yum -a "name=MySQL-python state=present"
- ansible app -s -m yum -a "name=python-setuptools state=present"
- ansible app -s -m easy_install -a "name=django"
- ansible app -a "python -c 'import django; print django.get_version()'"
- Configure the Database servers
- ansible db -s -m yum -a "name=mariadb-server state=present"
- ansible db -s -m service -a "name=mariadb state=started enabled=yes"
- ansible db -s -a "iptables -F"
-
ansible db -s -a "iptables -A INPUT -s 192.168.60.0/24 -p tcp \
-m tcp --dport 3306 -j ACCEPT" - ansible db -s -m yum -a "name=MySQL-python state=present"
-
ansible db -s -m mysql_user -a "name=django host=% password=12345 \
priv=*.*:ALL state=present"
- Make changes to just one server
- ansible app -s -a "service ntpd status"
- ansible app -s -a "service ntpd restart" --limit "192.168.60.4" limit后还可以用正则比如 --limit "*.4" --limit ~".*\.4"
- Manage users and groups
- ansible app -s -m group -a "name=admin state=present"
- ansible app -s -m user -a "name=johndoe group=admin createhome=yes"
- $ ansible app -s -m user -a "name=johndoe state=absent remove=yes"
- Manage files and directories
- Get information about a file
- ansible multi -m stat -a "path=/etc/environment"
- Copy a file to the servers
- ansible multi -m copy -a "src=/etc/hosts dest=/tmp/hosts"
- Retrieve a file from the servers
- ansible multi -s -m fetch -a "src=/etc/hosts dest=/tmp" 远程服务器拉取文件至本机,只能fetch文件,不能fetch目录,如果拉目录,先tar/zip 再拉到本机即可
- Create directories and files
- ansible multi -m file -a "dest=/tmp/test mode=644 state=directory"
-
ansible multi -m file -a "src=/src/symlink dest=/dest/symlink \
owner=root group=root state=link"
- Delete directories and files
- ansible multi -m file -a "dest=/tmp/test state=absent"
- Get information about a file
- Run operations in the background
- ansible multi -s -B 3600 -a "yum -y update"
- ansible multi -s -m async_status -a "jid=763350539037"
check on the status elsewhere using Ansible’s
async_status module - Fire-and-forget tasks
- ansible multi -B 3600 -P 0 -a "/path/to/fire-and-forget-script.sh"
- Check log files
- ansible multi -s -a "tail /var/log/messages"
- Manage cron jobs
- ansible multi -s -m cron -a "name='daily-cron-all-servers' \
hour=4 job='/path/to/daily-script.sh'
- ansible multi -s -m cron -a "name='daily-cron-all-servers' state=absent" 删掉
- ansible multi -s -m cron -a "name='daily-cron-all-servers' \
转载于:https://www.cnblogs.com/guxiaobei/p/8251127.html