ansible基础

olivee 4年前 ⋅ 1088 阅读

官方帮助文档: https://docs.ansible.com/ansible/2.9/index.html

1. 安装

1.1 执行安装命令

yum install -y http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

yum info ansible

yum install ansible

1.2 测试安装结果:

在/etc/ansible/hosts中追加目标IP: 如:

192.168.4.57
192.168.4.58

1.3 测试ping

ansible 192.168.4.58 -m ping -k 如果报错“ Please add this host's fingerprint to your known_hosts file to manage this host.”,则需要先ssh登录一下,这样会自动把秘钥保存到~/.ssh/known_hosts 文件中。下次再执行就不报错了。

1.4 设置互信

ssh-keygen
for host in 192.168.4.56  192.168.4.57  192.168.4.58 ; do ssh-copy-id -i ~/.ssh/id_rsa.pub $host;  done

2. ansible.cfg配置

ansible.cfg配置文件说明,参考: http://www.ansible.com.cn/docs/intro_configuration.html

3. /etc/ansible/hosts配置

对于/etc/ansible/hosts配置文件的说明详细参考: http://www.ansible.com.cn/docs/intro_inventory.html#inventoryformat

4. ansible中的模块

4.1 列出所有的模板

ansible-doc -l

所有模块:https://docs.ansible.com/ansible/2.9/modules/list_of_all_modules.html

4.2 查看某个模块的帮助文档

ansible-doc <模块名> # 如  ansible-doc command

4.3 常用模块

4.3.1 raw -- Executes a low-down and dirty command

帮助: https://docs.ansible.com/ansible/2.9/modules/raw_module.html

4.3.2 command -- Execute commands on targets

帮助: https://docs.ansible.com/ansible/2.9/modules/command_module.html

4.3.2 shell -- Execute shell commands on targets

帮助: https://docs.ansible.com/ansible/2.9/modules/shell_module.html

4.3.2 ping -- Try to connect to host, verify a usable python and return pon...

帮助: https://docs.ansible.com/ansible/2.9/modules/ping_module.html

4.3.2 copy -- Copy files to remote locations

帮助: https://docs.ansible.com/ansible/2.9/modules/copy_module.html

4.3.2 selinux -- Change policy and state of SELinux

帮助: https://docs.ansible.com/ansible/2.9/modules/selinux_module.html

4.3.2 file -- Manage files and file properties

帮助: https://docs.ansible.com/ansible/2.9/modules/file_module.html

4.3.2 unarchive -- Unpacks an archive after (optionally) copying it from the loca...

帮助: https://docs.ansible.com/ansible/2.9/modules/unarchive_module.html

4.3.2 debug -- Print statements during execution

帮助: https://docs.ansible.com/ansible/2.9/modules/debug_module.html

4.3.2 fail -- Fail with custom message

帮助: https://docs.ansible.com/ansible/2.9/modules/fail_module.html

4.3.2 set_stats -- Set stats for the current ansible run

帮助: https://docs.ansible.com/ansible/2.9/modules/set_stats_module.html

4.3.2 include_vars -- Load variables from files, dynamically within a task

帮助: https://docs.ansible.com/ansible/2.9/modules/include_vars_module.html

5. include其它tasks文件

http://www.ansible.com.cn/docs/playbooks_roles.html

6. 变量

定义g_nfs_hosts变量,查看nfs组的主机数

g_nfs_hosts: "{{ groups.nfs | default([]) }}"

定义g_all_hosts变量,是g_master_hosts及其其它变量的和:

g_all_hosts: "{{ g_master_hosts | union(g_node_hosts) | union(g_etcd_hosts)
                 | union(g_new_etcd_hosts) | union(g_lb_hosts) | union(g_nfs_hosts)
                 | union(g_new_node_hosts)| union(g_new_master_hosts)
                 | default([]) }}"

6.1 变量的优先级

  1. extra vars (在命令行中使用 -e)优先级最高 如 ansible-playbook release.yml --extra-vars "hosts=vipers user=starbuck"
  2. 然后是在inventory中定义的连接变量(比如 ansible_ssh_user ) /etc/ansible/hosts
  3. 接着是大多数的其它变量(命令行转换,play中的变量,included的变量,role中的变量等)
  vars:
    favcolor: blue
  或
  vars_files:
    - /vars/external_vars.yml
  或
  tasks:
  - name: xxxx
    include_vars: xxxxxxx.yaml
  1. 然后是在inventory定义的其它变量 /etc/ansible/hosts
  2. 然后是由系统发现的facts
  3. 然后是 "role默认变量", 这个是最默认的值,很容易丧失优先权

6.2 特殊的变量

如 groups hostvars group_names

https://docs.ansible.com/ansible/2.9/reference_appendices/special_variables.html http://www.ansible.com.cn/docs/playbooks_variables.html

6.3 变量的计算

参考jinja的builtin-filters语法: https://jinja.palletsprojects.com/en/2.11.x/templates/#builtin-filters

7. yaml中的关键字:

https://docs.ansible.com/ansible/2.9/reference_appendices/playbooks_keywords.html

8. shell日志输出

参考:https://www.csdn.net/gather_2b/MtTaYg2sNDcxMTUtYmxvZwO0O0OO0O0O.html

    - name: "启动{{item.port}}端口的服务"
      shell: ( {{item.start_cmd}} )
      async: 10
      poll: 0
      args:
        chdir: /app/{{item.folder}}
        warn: no
      register: cmd

    - debug:
        var: cmd

或:

  # We have to use the shell module because we can't set env vars with the command module.
  - name: "Place PV into oc"
    shell: "KUBECONFIG=/etc/origin/master/admin.kubeconfig oc create -f {{ pv_template | quote }}"
    register: oc_output

  - debug: var=oc_output

  - fail:
      msg: "Failed to add {{ pv_template }} to master."
    when: oc_output.rc != 0

8. inventory配置文件说明

参考: https://docs.ansible.com/ansible/2.9/user_guide/intro_inventory.html

9. 默认个的组 - Default groups

There are two default groups: all and ungrouped. The all group contains every host. The ungrouped group contains all hosts that don’t have another group aside from all. Every host will always belong to at least 2 groups (all and ungrouped or all and some other group). Though all and ungrouped are always present, they can be implicit and not appear in group listings like group_names