• 1. 安装
  • 2. 配置
    • 2.1. ansible.cfg
    • 2.2. hosts
  • 3. ansible的命令
  • 4. ansible-playbook

    1. 安装

    以centos为例。

    1. yum install -y ansible

    2. 配置

    默认配置目录在/etc/ansible/,主要有以下两个配置:

    • ansible.cfg:ansible的配置文件
    • hosts:配置ansible所连接的机器IP信息

    2.1. ansible.cfg

    2.2. hosts

    1. # This is the default ansible 'hosts' file.
    2. #
    3. # It should live in /etc/ansible/hosts
    4. #
    5. # - Comments begin with the '#' character
    6. # - Blank lines are ignored
    7. # - Groups of hosts are delimited by [header] elements
    8. # - You can enter hostnames or ip addresses
    9. # - A hostname/ip can be a member of multiple groups
    10. # Ex 1: Ungrouped hosts, specify before any group headers.
    11. # green.example.com
    12. # blue.example.com
    13. # 192.168.100.1
    14. # 192.168.100.10
    15. # Ex 2: A collection of hosts belonging to the 'webservers' group
    16. # [webservers]
    17. # alpha.example.org
    18. # beta.example.org
    19. # 192.168.1.100
    20. # 192.168.1.110
    21. # If you have multiple hosts following a pattern you can specify
    22. # them like this:
    23. # www[001:006].example.com
    24. # Ex 3: A collection of database servers in the 'dbservers' group
    25. # [dbservers]
    26. #
    27. # db01.intranet.mydomain.net
    28. # db02.intranet.mydomain.net
    29. # 10.25.1.56
    30. # 10.25.1.57
    31. # Here's another example of host ranges, this time there are no
    32. # leading 0s:
    33. # db-[99:101]-node.example.com
    34. [k8s]
    35. 192.168.201.52
    36. 192.168.201.53
    37. 192.168.201.54
    38. 192.168.201.55
    39. 192.168.201.56
    40. 192.168.201.57

    3. ansible的命令

    命令格式为:ansible [options]

    • host-pattern:即hosts文件中配置的集群名称
    • options:命令操作符

    例如:ansible k8s -a ‘uname -r’

    1. [root@k8s-master ansible]# ansible k8s -a 'uname -r'
    2. 172.16.201.56 | SUCCESS | rc=0 >>
    3. 4.16.11-1.el7.elrepo.x86_64
    4. 172.16.201.55 | SUCCESS | rc=0 >>
    5. 4.16.11-1.el7.elrepo.x86_64
    6. 172.16.201.54 | SUCCESS | rc=0 >>
    7. 4.16.11-1.el7.elrepo.x86_64
    8. 172.16.201.53 | SUCCESS | rc=0 >>
    9. 4.16.11-1.el7.elrepo.x86_64
    10. 172.16.201.52 | SUCCESS | rc=0 >>
    11. 4.16.11-1.el7.elrepo.x86_64
    12. 172.16.201.57 | SUCCESS | rc=0 >>
    13. 4.16.11-1.el7.elrepo.x86_64

    具体的命令信息:

    1. Usage: ansible <host-pattern> [options]
    2. Define and run a single task 'playbook' against a set of hosts
    3. Options:
    4. -a MODULE_ARGS, --args=MODULE_ARGS
    5. module arguments
    6. --ask-vault-pass ask for vault password
    7. -B SECONDS, --background=SECONDS
    8. run asynchronously, failing after X seconds
    9. (default=N/A)
    10. -C, --check don't make any changes; instead, try to predict some
    11. of the changes that may occur
    12. -D, --diff when changing (small) files and templates, show the
    13. differences in those files; works great with --check
    14. -e EXTRA_VARS, --extra-vars=EXTRA_VARS
    15. set additional variables as key=value or YAML/JSON, if
    16. filename prepend with @
    17. -f FORKS, --forks=FORKS
    18. specify number of parallel processes to use
    19. (default=5)
    20. -h, --help show this help message and exit
    21. -i INVENTORY, --inventory=INVENTORY, --inventory-file=INVENTORY
    22. specify inventory host path or comma separated host
    23. list. --inventory-file is deprecated
    24. -l SUBSET, --limit=SUBSET
    25. further limit selected hosts to an additional pattern
    26. --list-hosts outputs a list of matching hosts; does not execute
    27. anything else
    28. -m MODULE_NAME, --module-name=MODULE_NAME
    29. module name to execute (default=command)
    30. -M MODULE_PATH, --module-path=MODULE_PATH
    31. prepend colon-separated path(s) to module library
    32. (default=[u'/root/.ansible/plugins/modules',
    33. u'/usr/share/ansible/plugins/modules'])
    34. -o, --one-line condense output
    35. --playbook-dir=BASEDIR
    36. Since this tool does not use playbooks, use this as a
    37. subsitute playbook directory.This sets the relative
    38. path for many features including roles/ group_vars/
    39. etc.
    40. -P POLL_INTERVAL, --poll=POLL_INTERVAL
    41. set the poll interval if using -B (default=15)
    42. --syntax-check perform a syntax check on the playbook, but do not
    43. execute it
    44. -t TREE, --tree=TREE log output to this directory
    45. --vault-id=VAULT_IDS the vault identity to use
    46. --vault-password-file=VAULT_PASSWORD_FILES
    47. vault password file
    48. -v, --verbose verbose mode (-vvv for more, -vvvv to enable
    49. connection debugging)
    50. --version show program's version number and exit
    51. Connection Options:
    52. control as whom and how to connect to hosts
    53. -k, --ask-pass ask for connection password
    54. --private-key=PRIVATE_KEY_FILE, --key-file=PRIVATE_KEY_FILE
    55. use this file to authenticate the connection
    56. -u REMOTE_USER, --user=REMOTE_USER
    57. connect as this user (default=None)
    58. -c CONNECTION, --connection=CONNECTION
    59. connection type to use (default=smart)
    60. -T TIMEOUT, --timeout=TIMEOUT
    61. override the connection timeout in seconds
    62. (default=10)
    63. --ssh-common-args=SSH_COMMON_ARGS
    64. specify common arguments to pass to sftp/scp/ssh (e.g.
    65. ProxyCommand)
    66. --sftp-extra-args=SFTP_EXTRA_ARGS
    67. specify extra arguments to pass to sftp only (e.g. -f,
    68. -l)
    69. --scp-extra-args=SCP_EXTRA_ARGS
    70. specify extra arguments to pass to scp only (e.g. -l)
    71. --ssh-extra-args=SSH_EXTRA_ARGS
    72. specify extra arguments to pass to ssh only (e.g. -R)
    73. Privilege Escalation Options:
    74. control how and which user you become as on target hosts
    75. -s, --sudo run operations with sudo (nopasswd) (deprecated, use
    76. become)
    77. -U SUDO_USER, --sudo-user=SUDO_USER
    78. desired sudo user (default=root) (deprecated, use
    79. become)
    80. -S, --su run operations with su (deprecated, use become)
    81. -R SU_USER, --su-user=SU_USER
    82. run operations with su as this user (default=None)
    83. (deprecated, use become)
    84. -b, --become run operations with become (does not imply password
    85. prompting)
    86. --become-method=BECOME_METHOD
    87. privilege escalation method to use (default=sudo),
    88. valid choices: [ sudo | su | pbrun | pfexec | doas |
    89. dzdo | ksu | runas | pmrun | enable ]
    90. --become-user=BECOME_USER
    91. run operations as this user (default=root)
    92. --ask-sudo-pass ask for sudo password (deprecated, use become)
    93. --ask-su-pass ask for su password (deprecated, use become)
    94. -K, --ask-become-pass
    95. ask for privilege escalation password
    96. Some modules do not make sense in Ad-Hoc (include, meta, etc)

    4. ansible-playbook

    1. Usage: ansible-playbook [options] playbook.yml [playbook2 ...]
    2. Runs Ansible playbooks, executing the defined tasks on the targeted hosts.
    3. Options:
    4. --ask-vault-pass ask for vault password
    5. -C, --check don't make any changes; instead, try to predict some
    6. of the changes that may occur
    7. -D, --diff when changing (small) files and templates, show the
    8. differences in those files; works great with --check
    9. -e EXTRA_VARS, --extra-vars=EXTRA_VARS
    10. set additional variables as key=value or YAML/JSON, if
    11. filename prepend with @
    12. --flush-cache clear the fact cache for every host in inventory
    13. --force-handlers run handlers even if a task fails
    14. -f FORKS, --forks=FORKS
    15. specify number of parallel processes to use
    16. (default=5)
    17. -h, --help show this help message and exit
    18. -i INVENTORY, --inventory=INVENTORY, --inventory-file=INVENTORY
    19. specify inventory host path or comma separated host
    20. list. --inventory-file is deprecated
    21. -l SUBSET, --limit=SUBSET
    22. further limit selected hosts to an additional pattern
    23. --list-hosts outputs a list of matching hosts; does not execute
    24. anything else
    25. --list-tags list all available tags
    26. --list-tasks list all tasks that would be executed
    27. -M MODULE_PATH, --module-path=MODULE_PATH
    28. prepend colon-separated path(s) to module library
    29. (default=[u'/root/.ansible/plugins/modules',
    30. u'/usr/share/ansible/plugins/modules'])
    31. --skip-tags=SKIP_TAGS
    32. only run plays and tasks whose tags do not match these
    33. values
    34. --start-at-task=START_AT_TASK
    35. start the playbook at the task matching this name
    36. --step one-step-at-a-time: confirm each task before running
    37. --syntax-check perform a syntax check on the playbook, but do not
    38. execute it
    39. -t TAGS, --tags=TAGS only run plays and tasks tagged with these values
    40. --vault-id=VAULT_IDS the vault identity to use
    41. --vault-password-file=VAULT_PASSWORD_FILES
    42. vault password file
    43. -v, --verbose verbose mode (-vvv for more, -vvvv to enable
    44. connection debugging)
    45. --version show program's version number and exit
    46. Connection Options:
    47. control as whom and how to connect to hosts
    48. -k, --ask-pass ask for connection password
    49. --private-key=PRIVATE_KEY_FILE, --key-file=PRIVATE_KEY_FILE
    50. use this file to authenticate the connection
    51. -u REMOTE_USER, --user=REMOTE_USER
    52. connect as this user (default=None)
    53. -c CONNECTION, --connection=CONNECTION
    54. connection type to use (default=smart)
    55. -T TIMEOUT, --timeout=TIMEOUT
    56. override the connection timeout in seconds
    57. (default=10)
    58. --ssh-common-args=SSH_COMMON_ARGS
    59. specify common arguments to pass to sftp/scp/ssh (e.g.
    60. ProxyCommand)
    61. --sftp-extra-args=SFTP_EXTRA_ARGS
    62. specify extra arguments to pass to sftp only (e.g. -f,
    63. -l)
    64. --scp-extra-args=SCP_EXTRA_ARGS
    65. specify extra arguments to pass to scp only (e.g. -l)
    66. --ssh-extra-args=SSH_EXTRA_ARGS
    67. specify extra arguments to pass to ssh only (e.g. -R)
    68. Privilege Escalation Options:
    69. control how and which user you become as on target hosts
    70. -s, --sudo run operations with sudo (nopasswd) (deprecated, use
    71. become)
    72. -U SUDO_USER, --sudo-user=SUDO_USER
    73. desired sudo user (default=root) (deprecated, use
    74. become)
    75. -S, --su run operations with su (deprecated, use become)
    76. -R SU_USER, --su-user=SU_USER
    77. run operations with su as this user (default=None)
    78. (deprecated, use become)
    79. -b, --become run operations with become (does not imply password
    80. prompting)
    81. --become-method=BECOME_METHOD
    82. privilege escalation method to use (default=sudo),
    83. valid choices: [ sudo | su | pbrun | pfexec | doas |
    84. dzdo | ksu | runas | pmrun | enable ]
    85. --become-user=BECOME_USER
    86. run operations as this user (default=root)
    87. --ask-sudo-pass ask for sudo password (deprecated, use become)
    88. --ask-su-pass ask for su password (deprecated, use become)
    89. -K, --ask-become-pass
    90. ask for privilege escalation password