官术网_书友最值得收藏!

Command-line arguments

In this section, you will learn about the use of command-line arguments for playbook execution and how to employ some of the more commonly used ones to your advantage. We are already very familiar with one of these arguments, the --version switch, which we use to confirm that Ansible is installed (and which version is installed):

$ ansible 2.9.6
config file = None
configured module search path = ['/Users/james/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/Cellar/ansible/2.9.6_1/libexec/lib/python3.8/site-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.8.2 (default, Mar 11 2020, 00:28:52) [Clang 11.0.0 (clang-1100.0.33.17)]

Just as we were able to learn about the various configuration parameters directly through Ansible, we can also learn about the command-line arguments. Almost all of the Ansible executables have a --help option that you can run to display the valid command-line parameters. Let's try this out now:

  1. You can view all the options and arguments when you execute the ansible command line. Use the following command:
$ ansible --help

You will see a great deal of helpful output when you run the preceding command; an example of this is shown in the following code block (you might want to pipe this into a pager, such as less, so that you can read it all easily):

$ ansible --help
usage: ansible [-h] [--version] [-v] [-b] [--become-method BECOME_METHOD] [--become-user BECOME_USER] [-K] [-i INVENTORY] [--list-hosts] [-l SUBSET] [-P POLL_INTERVAL] [-B SECONDS] [-o] [-t TREE] [-k]
[--private-key PRIVATE_KEY_FILE] [-u REMOTE_USER] [-c CONNECTION] [-T TIMEOUT] [--ssh-common-args SSH_COMMON_ARGS] [--sftp-extra-args SFTP_EXTRA_ARGS] [--scp-extra-args SCP_EXTRA_ARGS]
[--ssh-extra-args SSH_EXTRA_ARGS] [-C] [--syntax-check] [-D] [-e EXTRA_VARS] [--vault-id VAULT_IDS] [--ask-vault-pass | --vault-password-file VAULT_PASSWORD_FILES] [-f FORKS]
[-M MODULE_PATH] [--playbook-dir BASEDIR] [-a MODULE_ARGS] [-m MODULE_NAME]
pattern

Define and run a single task 'playbook' against a set of hosts

positional arguments:
pattern host pattern

optional arguments:
--ask-vault-pass ask for vault password
--list-hosts outputs a list of matching hosts; does not execute anything else
--playbook-dir BASEDIR
Since this tool does not use playbooks, use this as a substitute playbook directory.This sets the relative path for many features including roles/ group_vars/ etc.
--syntax-check perform a syntax check on the playbook, but do not execute it
--vault-id VAULT_IDS the vault identity to use
--vault-password-file VAULT_PASSWORD_FILES
vault password file
--version show program's version number, config file location, configured module search path, module location, executable location and exit
-B SECONDS, --background SECONDS
run asynchronously, failing after X seconds (default=N/A)
-C, --check don't make any changes; instead, try to predict some of the changes that may occur
-D, --diff when changing (small) files and templates, show the differences in those files; works great with --check
-M MODULE_PATH, --module-path MODULE_PATH
prepend colon-separated path(s) to module library (default=~/.ansible/plugins/modules:/usr/share/ansible/plugins/modules)
-P POLL_INTERVAL, --poll POLL_INTERVAL
set the poll interval if using -B (default=15)
-a MODULE_ARGS, --args MODULE_ARGS
module arguments
-e EXTRA_VARS, --extra-vars EXTRA_VARS
set additional variables as key=value or YAML/JSON, if filename prepend with @

  1. We could take one example from the preceding code to build on our previous use of ansible; so far, we have almost exclusively used it to run ad hoc tasks with the -m and -a parameters. However, ansible can also perform useful tasks such as telling us about the hosts in a group within our inventory. We could explore this using the production-inventory file we used earlier in this chapter:
$ ansible -i production-inventory --list-host appservers_emea_zone

When you run this, you should see the members of the appservers_emea_zone inventory group listed. Although perhaps a little contrived, this example is incredibly valuable when you start working with dynamic inventory files and you can no longer just cat your inventory file to the terminal to view the contents:

$ ansible -i production-inventory --list-host appservers_emea_zone
hosts (2):
appserver1-emea.example.com
appserver2-emea.example.com

The same is true for the ansible-playbook executable file, too. We have already seen a few of these in the previous examples of this book and there's more that we can do. For example, earlier, we discussed the use of ssh-agent to manage multiple SSH authentication keys. While this makes running playbooks simple (as you don't have to pass any authentication parameters to Ansible), it is not the only way of doing this. You can use one of the command-line arguments for ansible-playbook to specify the private SSH key file, instead, as follows:

$ ansible-playbook -i production-inventory site.yml --private-key ~/keys/id_rsa

Similarly, in the preceding section, we specified the remote_user variable for Ansible to connect with in the playbook. However, command-line arguments can also set this parameter for the playbook; so, rather than editing the remote_user line in the playbook, we could remove it altogether and instead have run it using the following command-line string:

$ ansible-playbook -i production-inventory site.yml --user danieloh

The ultimate aim of Ansible is to make your life simpler and to remove mundane day-to-day tasks from your list. As a result, there is no right or wrong way to do this—you can specify your private SSH key using a command-line argument or make it available using ssh-agent. Similarly, you can put the remote_user line in your playbook or user the --user parameter on the command line. Ultimately, the choice is yours, but it is important to consider that if you are distributing a playbook to multiple users and they all have to remember to specify the remote user on the command line, will they actually remember to do it? What will the consequences be if they don't? If the remote_user line is present in the playbook, will that make their lives easier and be less prone to error because the user account has been set in the playbook itself?

As with the configuration of Ansible, you will use a small handful of the command-line arguments frequently and there will be many that you may never touch. The important thing is that you know they are there and how to find out about them, and you can make informed decisions about when to use them. Let's proceed to the next section, where we will look in a bit more detail at ad hoc commands with Ansible.

主站蜘蛛池模板: 嘉禾县| 长宁县| 大城县| 日照市| 前郭尔| 苍山县| 昌江| 乳山市| 阳曲县| 桓台县| 石河子市| 堆龙德庆县| 波密县| 敦化市| 海盐县| 嵊泗县| 贡山| 新津县| 广东省| 娄烦县| 称多县| 延津县| 常州市| 贵德县| 松阳县| 永登县| 兖州市| 时尚| 渝中区| 宜兴市| 泸州市| 志丹县| 枣庄市| 延长县| 修水县| 大方县| 娱乐| 侯马市| 蚌埠市| 湘潭市| 达尔|