Joyber 的博客 - ansible
type: channel
link: https://blog.qqvbc.com/tag/ansible/
items: ansible 个人使用习惯笔记
items: ansible playbook 使用各种循环 学习笔记
items: ansible 运维工具 可远程执行多台服务器命令

ansible 个人使用习惯笔记
type: item
date: 2020-11-14T11:07:00+08:00
description: 语法检查别名alias ansible-playbook-check='ansible-playbook --syntax-check'查看模块帮助，例如查看file模块的帮助文档ansible-doc file查看可用模块：自动进入less模块，可/搜索ansible-doc -l

ansible playbook 使用各种循环 学习笔记
type: item
date: 2020-11-09T11:14:00+08:00
description: playbook中可以写多种循环：with_list with_items with_indexed_items with_flattened with_together with_dict with_sequence with_subelements with_nested / with_cartesian with_random_choice列表 with_list这个例子会分别执行['protected/yiic1', '0755'], ['protected/yiic2', '0766'],['protected/yiic3', '0777']- name: change files mode file: path: '{{ destdir }}/{{ item.0 }}' mode: '{{ item.1 }}' with_list: - ['protected/yiic1', '0755'] - ['protected/yiic2', '0766'] - ['protected/yiic3', '0777']标准循环(with_items)：注意例2的结果将会分别执行1,2,3,'a','b'#例1 - name: add several users user: name={{ item }} state=present groups=wheel with_items: - testuser1 - testuser2 #例2 - name: add several users user: name={{ item }} state=present groups=wheel with_items: - [1,2,3] - ['b','c']嵌套循环(with_nested / with_cartesian)：可以实现 mkdir -p /testdir/testdir/{a,b,c}/{test1,test2} 这样的效果- file: state: directory path: "/testdir/testdir/{{ item.0 }}/{{ item.1 }}" with_nested: - [ a, b, c ] - [ test1, test2 ]对哈希表使用循环--- users: alice: name: Alice Appleworth telephone: 123-456-7890 bob: name: Bob Bananarama telephone: 987-654-3210 ... tasks: - name: Print phone records debug: msg="User {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})" with_dict: "{{users}}"对文件列表使用循环--- - hosts: all tasks: # first ensure our target directory exists - file: dest=/etc/fooapp state=directory # copy each file over that matches the given pattern - copy: src={{ item }} dest=/etc/fooapp/ owner=root mode=600 with_fileglob: - /playbooks/files/fooapp/*对并行数据集使用循环(一个数组对应另一个数组)--- alpha: [ 'a', 'b', 'c', 'd' ] numbers: [ 1, 2, 3, 4 ] #如果你想得到’(a, 1)’和’(b, 2)’之类的集合.可以使用’with_together’: tasks: - debug: msg="{{ item.0 }} and {{ item.1 }}" with_together: - "{{alpha}}" - "{{numbers}}"对子元素使用循环--- users: - name: alice authorized: - /tmp/alice/onekey.pub - /tmp/alice/twokey.pub mysql: password: mysql-password hosts: - "%" - "127.0.0.1" - "::1" - "localhost" privs: - "*.*:SELECT" - "DB1.*:ALL" - name: bob authorized: - /tmp/bob/id_rsa.pub mysql: password: othe…

ansible 运维工具 可远程执行多台服务器命令
type: item
date: 2019-11-07T14:58:00+08:00
description: 新手学习教程：https://www.w3cschool.cn/automate_with_ansible官方文档：https://docs.ansible.com/安装：yum install -y ansible 配置文件：/etc/ansible/hosts (例子：https://www.w3cschool.cn/automate_with_ansible/automate_with_ansible-1khc27p1.html)#ansible_ssh_host #ansible_ssh_port #ansible_ssh_user #ansible_ssh_pass #ansible_ssh_private_key_file [dev] 192.168.0.200 ansible_ssh_port=123456 [online] pc1 ansible_ssh_host=192.168.0.200 ansible_ssh_user=root ansible_ssh_port=123456 若有对 Control Machine 本机操作的需求，建议于 /etc/ansible/hosts 补上 local 的设定。# For root user. $ /bin/echo -e "[local]\nlocalhost ansible_connection=local" >> /etc/ansible/hosts # For sudo user. $ sudo su -c '/bin/echo -e "[local]\nlocalhost ansible_connection=local" >> /etc/ansible/hosts'Hello World当已上的设置都完成了，您可以试著在终端机里用 Ansible 呼叫本机印出 Hello World。$ ansible dev -m command -a 'echo Hello World.' localhost | SUCCESS | rc=0 >> Hello World.欢迎来到 Ansible 的世界！:D如果遇到第一次连接会检查是否有链接过的记录时，可能需要把下面这个设置启用（不检查）：Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host. 配置：/etc/ansible/ansible.confhost_key_checking = False 实操技巧：使用剧本的时候 -e ：# 多个变量间用空格隔开，遇到值有空格时用引号 ansible-playbook xxx.yml -e "var1=val1 var2=val2 var3='val val'" # 将参数写到文件中，再引用，文件内容格式为json: {"a":"123", "b": "321"} ansible-playbook xxx.yml -e "@path/to/file" # 刷本中写变量，-e 传入的变量会覆盖的， xxx.yml： - name: xxx hosts: xxx vars_files: - xxx.json vars: a: 123 b: 321上传文件的时候，可能需要上传多个文件，怎么写：tasks: - name: upload cert files copy: src={{ item.src }} dest={{ item.dest }} with_items: - { src: '/root/site/{{ mark }}.key', dest: '/data/cert/{{ mark }}.key' } - { src: '/root/site/{{ mark }}.pem', dest: '/data/cert/{{ mark }}.pem' }