Writing a string to file using Ad-Hoc Commands in Ansible - ansible

I'm a beginner with Ansible and trying to write a string to a file with an Ad-Hoc command I'm trying to play around with the replace module. The file I'm trying to write to is /etc/motd/.
ansible replace --sudo /etc/motd "This server is managed by Ansible"
Any help would be appreciated thanks!

Have a look at the lineinfile module usage and a general syntax for Ad hoc commands.
What you are looking for is:
ansible target_node -b -m lineinfile -a 'dest=/etc/motd line="This server is managed by Ansible"'
in extended form:
ansible target_node --become --module-name=lineinfile --args='dest=/etc/motd line="This server is managed by Ansible"'
Explanation:
target_node is the hostname or group name as defined in the Ansible inventory file
--become (-b) instructs Ansible to use sudo
-module-name (-m) specifies the module to run (lineinfile here)
--args (-a) passes arguments to the module (these change depending on a module)
dest points to the destination file
line instructs Ansible to ensure a particular line is in the file
If you would like to replace the whole contents of the /etc/motd you should use copy module.
ansible target_node -b -m copy -a 'dest=/etc/motd content="This server is managed by Ansible"'
Notice one of the arguments is changed accordingly.

Related

create a directory test at /home/user using ansible-adhoc command

I am trying to create directory using below command
ansible app -m file -a "path=/home/user/test mode = 777 state = directory" -b
I am getting below error message. Could any one advise me what I am doing wrong here?
ERROR! this task 'file' has extra params, which is only allowed in the
following modules: ansible.builtin.raw, ansible.legacy.add_host,
ansible.builtin.meta, ansible.legacy.include,
ansible.legacy.import_role, script, ansible.legacy.raw, group_by,
ansible.builtin.shell, ansible.legacy.win_command, include, shell,
include_vars, ansible.builtin.import_tasks, add_host,
ansible.builtin.include_vars, ansible.legacy.include_role,
ansible.builtin.include_role, ansible.legacy.include_vars,
ansible.legacy.win_shell, ansible.legacy.group_by, import_tasks,
ansible.builtin.set_fact, ansible.builtin.command,
ansible.builtin.include_tasks, include_tasks, ansible.builtin.script,
ansible.builtin.include, raw, meta, ansible.legacy.set_fact,
ansible.builtin.add_host, ansible.legacy.script,
ansible.legacy.import_tasks, win_command, ansible.builtin.win_shell,
include_role, win_shell, set_fact, ansible.legacy.shell,
ansible.legacy.command, import_role, ansible.legacy.meta,
ansible.builtin.import_role, ansible.legacy.include_tasks,
ansible.builtin.group_by, ansible.builtin.win_command, command
The simplest example of an ad hoc command to create a directory using Ansible is
ansible nodes -a "mkdir /BYANSIBLE"
Try this:
ansible app -m file -a "dest=/home/user/test state=directory"
or
ansible localhost -m file -a "dest=/home/user/test state=directory"
In general, for an Ansible ad-hoc command, any module arguments are supplied using the -a option, as one single string. Due to this, the way Ansible distinguishes the individual arguments, is by spaces.
You need to remove the extra spaces you have on either side of the equal signs for the ad-hoc command to work.
Hence, the following would work:
ansible app -m file -a "path=/home/user/test mode=755 state=directory" -b
Also, check out some of the related examples in the Ansible docs.
Side note: I've modified the permissions to 755 (read/write/execute for owner + read/execute for group & others). Its almost never a good idea to give 777 permissions (full read/write/execute) on directories.

Could i use ansible playbook(tgz,zip format ) like helm charts?

I want to use ansible to manage multi service deployment, each service have many roles. Now I want to package one service as a tgz or zip file(same as helm charts), and use one file to override variables when use ansible execute(like helm install xx.tgz --values values.yml)
I am new to ansible, and have checked ansible-playbook -h, but no find some related information
Usage: ansible-playbook [options] playbook.yml [playbook2 ...]
Runs Ansible playbooks, executing the defined tasks on the targeted hosts.
Options:
--ask-vault-pass ask for vault password
-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
-e EXTRA_VARS, --extra-vars=EXTRA_VARS
set additional variables as key=value or YAML/JSON, if
filename prepend with #
--flush-cache clear the fact cache for every host in inventory
--force-handlers run handlers even if a task fails
-f FORKS, --forks=FORKS
specify number of parallel processes to use
(default=5)
-h, --help show this help message and exit
-i INVENTORY, --inventory=INVENTORY, --inventory-file=INVENTORY
specify inventory host path or comma separated host
list. --inventory-file is deprecated
-l SUBSET, --limit=SUBSET
further limit selected hosts to an additional pattern
--list-hosts outputs a list of matching hosts; does not execute
anything else
--list-tags list all available tags
--list-tasks list all tasks that would be executed
-M MODULE_PATH, --module-path=MODULE_PATH
prepend colon-separated path(s) to module library (def
ault=~/.ansible/plugins/modules:/usr/share/ansible/plu
gins/modules)
--skip-tags=SKIP_TAGS
only run plays and tasks whose tags do not match these
values
--start-at-task=START_AT_TASK
start the playbook at the task matching this name
--step one-step-at-a-time: confirm each task before running
--syntax-check perform a syntax check on the playbook, but do not
execute it
-t TAGS, --tags=TAGS only run plays and tasks tagged with these values
--vault-id=VAULT_IDS the vault identity to use
--vault-password-file=VAULT_PASSWORD_FILES
vault password file
-v, --verbose verbose mode (-vvv for more, -vvvv to enable
connection debugging)
--version show program's version number, config file location,
configured module search path, module location,
executable location and exit
Connection Options:
control as whom and how to connect to hosts
-k, --ask-pass ask for connection password
--private-key=PRIVATE_KEY_FILE, --key-file=PRIVATE_KEY_FILE
use this file to authenticate the connection
-u REMOTE_USER, --user=REMOTE_USER
connect as this user (default=None)
-c CONNECTION, --connection=CONNECTION
connection type to use (default=smart)
-T TIMEOUT, --timeout=TIMEOUT
override the connection timeout in seconds
(default=10)
--ssh-common-args=SSH_COMMON_ARGS
specify common arguments to pass to sftp/scp/ssh (e.g.
ProxyCommand)
--sftp-extra-args=SFTP_EXTRA_ARGS
specify extra arguments to pass to sftp only (e.g. -f,
-l)
--scp-extra-args=SCP_EXTRA_ARGS
specify extra arguments to pass to scp only (e.g. -l)
--ssh-extra-args=SSH_EXTRA_ARGS
specify extra arguments to pass to ssh only (e.g. -R)
Privilege Escalation Options:
control how and which user you become as on target hosts
-b, --become run operations with become (does not imply password
prompting)
--become-method=BECOME_METHOD
privilege escalation method to use (default=sudo), use
`ansible-doc -t become -l` to list valid choices.
--become-user=BECOME_USER
run operations as this user (default=root)
-K, --ask-become-pass
ask for privilege escalation password
version:ansible 2.8.0
You can't package playbooks as zip or tgz files. One option would be to use ansible-pull. It checks a repository out and runs a specified playbook: https://docs.ansible.com/ansible/latest/cli/ansible-pull.html

Pass multiple commands in ad-hoc mode in Cisco ios_command module

I would like to know how can I pass multiple show commands in ios_command module in ad-hoc mode.
Sample with just one command:
ansible all -m ios_command -a "commands='show version'"
Now here I would like to send another command, say show run or any other.
Any suggestions on this would be appreciated.
You need to pass a list and you can do it using JSON string:
ansible all -m ios_command -a "commands='[ \"show version\", \"show run\" ]'"
If you leave the spaces out, you can squeeze to 'commands=["show version","show run"]'
I use the following:
ansible ios-device -m ios_command -a commands="{{ lookup('file', 'commands.txt') }}" -u username -k
where commands.txt contains
show version
You can add more commands on each line of the 'commands.txt' file.

Ansible Run Command

Fairly new to Ansible.
I have a text file which contains a list of ec2 instance ip addresses. Secondly, I have a .yml file which applies tags to an array of ec2 ips.
Does anyone what run command I would use to pass the list of ips stored as a text file? I did it once a while back before. I forgot the run command, and cannot find it in my history.
I have a text file which contains a list of ec2 instance ip addresses: you probably mean an inventory file.
to include it in your ansible command execution, add -i <file name>
example:
ansible-playbook -i <inventory file> <yml file>
You could create an inventory with your IP's, for example, something like /tmp/my-ec2-vms:
[my-ec2-vms]
10.1.2.10
10.1.2.11
10.1.2.12
10.1.2.13
...
[my-ec2-vms:vars]
ansible_python_interpreter=/usr/local/bin/python
Then for testing, you could use the ping module, for example:
ansible -i /tmp/my-ec2-vms -m ping all
If that works later you could just run your playbooks:
ansible-playbook -i /tmp/my-ec2-vms my-playbook.yml

Ansible: include group vars .yml file from command line

Our vendor sends us Ansible playbooks and scripts for deployment. I need to check availability of some IP for ansible inventory groups, like:
ansible mgm -i inventories/vrxinventory -m shell -a 'ping http://10.33.63.66/vrx/' -u user -k -v.
The destination is changing depending on the environment. Those destination IPs to ping are in group vars .yml file.
Is it possible to use variables from this group_var.yml file, through command line like:
ansible mgm -i inventories/vrxinventory -m shell -a "ping {{ vrm_repo_url }}" -u user -k -v ?
I really don't want to mess with their playbooks modification using sed/awk, during CI.
Is it possible to use variables from this group_var.yml file, through command line like: ansible mgm -i inventories/vrxinventory -m shell -a "ping {{ vrm_repo_url }}" -u user -k -v
Yes, group variables will be read by ansible command for ad-hoc commands.
No, it makes no sense to execute ping {{ vrm_repo_url }} through shell module in Ansible:
firstly, because by default ping runs infinitely (this can be mitigated with parameters);
secondly, because you won't see any output of the ping command.
What you most likely want is to use:
a wait_for module with low connect_timeout parameter to check the connectivity between the target and some other machine;
or a get_url module (as you seemingly want to check availability of web services).

Resources