Questions tagged [ansible]
On-topic questions are concerned with the use of the tool itself. Installing Ansible and prerequisites, connection issues, ... are off-topic. Red Hat Ansible is a model-driven, configuration management, multi-node deployment/orchestration and remote task execution system. It uses SSH by default, so there is no special software to be installed on the nodes you manage. Ansible is written in Python but can be extended in any language.
ansible
22,867
questions
508
votes
26
answers
670k
views
How to create a directory using Ansible
How do you create a directory www at /srv on a Debian-based system using an Ansible playbook?
298
votes
27
answers
476k
views
Specify sudo password for Ansible
How do I specify a sudo password for Ansible in non-interactive way?
I'm running Ansible playbook like this:
$ ansible-playbook playbook.yml -i inventory.ini \
--user=username --ask-sudo-pass
...
296
votes
8
answers
285k
views
Run command on the Ansible host
Is it possible to run commands on the Ansible controller node?
My scenario is that I want to take a checkout from a git server that is hosted internally (and isn't accessible outside the company ...
295
votes
11
answers
451k
views
How can I pass variable to ansible playbook in the command line?
How can one pass variable to ansible playbook in the command line?
The following command didn't work:
$ ansible-playbook -i '10.0.0.1,' yada-yada.yml --tags 'loaddata' django_fixtures="...
277
votes
14
answers
325k
views
Safely limiting Ansible playbooks to a single machine?
I'm using Ansible for some simple user management tasks with a small group of computers. Currently, I have my playbooks set to hosts: all and my hosts file is just a single group with all machines ...
266
votes
14
answers
473k
views
How to move/rename a file using an Ansible task on a remote system
How is it possible to move/rename a file/directory using an Ansible module on a remote system? I don't want to use the command/shell tasks and I don't want to copy the file from the local system to ...
252
votes
7
answers
281k
views
How to run only one task in ansible playbook?
Is there a way to only run one task in ansible playbook?
For example, in roles/hadoop_primary/tasks/hadoop_master.yml. I have "start hadoop job tracker services" task. Can I just run that ...
250
votes
17
answers
279k
views
How to ignore ansible SSH authenticity checking?
Is there a way to ignore the SSH authenticity checking made by Ansible? For example when I've just setup a new server I have to answer yes to this question:
GATHERING FACTS ***************************...
227
votes
26
answers
586k
views
Ansible: How to delete files and folders inside a directory?
The below code only deletes the first file it gets inside the web dir. I want to remove all the files and folders inside the web directory and retain the web directory. How can I do that?
- name: ...
213
votes
5
answers
87k
views
What's the difference between defaults and vars in an Ansible role?
When creating a new Ansible role, the template creates both a vars and a defaults directory with an empty main.yml file. When defining my role, I can place variable definitions in either of these, and ...
211
votes
11
answers
273k
views
ansible: lineinfile for several lines?
The same way there is a module lineinfile to add one line in a file, is there a way to add several lines?
I do not want to use a template because you have to provide the whole file.
I just want to ...
210
votes
13
answers
405k
views
How to check if a file exists in Ansible?
I have to check whether a file exists in /etc/. If the file exists then I have to skip the task.
Here is the code I am using:
- name: checking the file exists
command: touch file.txt
when: $(! -s /...
194
votes
20
answers
116k
views
Ansible fails with /bin/sh: 1: /usr/bin/python: not found
I'm running into an error I've never seen before. Here is the command and the error:
$ ansible-playbook create_api.yml
PLAY [straw] ******************************************************************
...
193
votes
7
answers
229k
views
How to do multiline shell script in Ansible
right now I am using a shell script in ansible that would be much more readable if it was on multiple lines
- name: iterate user groups
shell: groupmod -o -g {{ item['guid'] }} {{ item['username'] }...
191
votes
5
answers
278k
views
How to switch a user per task or set of tasks?
A recurring theme that's in my ansible playbooks is that I often must execute a command with sudo privileges (sudo: yes) because I'd like to do it for a certain user. Ideally I'd much rather use sudo ...
189
votes
10
answers
258k
views
Where can I get a list of Ansible pre-defined variables?
I see that Ansible provide some pre-defined variables that we can use in playbooks and template files. For example, the host IP address is ansible_eth0.ipv4.address. Googleing and searching the docs I ...
187
votes
3
answers
231k
views
How to get the host name of the current machine as defined in the Ansible hosts file?
I'm setting up an Ansible playbook to set up a couple servers. There are a couple of tasks that I only want to run if the current host is my local dev host, named "local" in my hosts file. ...
182
votes
9
answers
295k
views
Use a default if a variable is not defined
I'm customizing Linux users creation inside my role. I need to let users of my role customize home_directory, group_name, name, password.
I was wondering if there's a more flexible way to cope with ...
177
votes
4
answers
360k
views
How to run a task when variable is undefined in ansible?
I am looking for a way to perform a task when Ansible variable is not registers or undefined.
E.g.:
- name: some task
command: sed -n '5p' "{{app.dirs.includes}}/BUILD.info" | awk '{...
175
votes
8
answers
193k
views
What's the easy way to auto create non existing dir in ansible
In my Ansible playbook many times i need to create a file:
- name: Copy file
template:
src: code.conf.j2
dest: "{{ project_root }}/conf/code.conf"
Many times conf dir is not there....
165
votes
14
answers
473k
views
Ansible: get current target host's IP address
How do you get the current host's IP address in a role?
I know you can get the list of groups the host is a member of and the hostname of the host but I am unable to find a solution to getting the IP ...
165
votes
6
answers
212k
views
Variable that has the path to the current ansible-playbook that is executing?
Is there an Ansible variable that has the absolute path to the current playbook that is executing?
Some context: I'm running/creating an Ansible script against localhost to configure a MySQL Docker ...
165
votes
14
answers
117k
views
How to automatically install Ansible Galaxy roles?
All my Ansible playbooks/roles are checked in to my git repo.
However, for Ansible Galaxy roles I always have to explicitly download them one by one on every machine I want to run Ansible from.
It's ...
164
votes
8
answers
330k
views
How to set host_key_checking=false in ansible inventory file?
I would like to use ansible-playbook command instead of 'vagrant provision'. However setting host_key_checking=false in the hosts file does not seem to work.
# hosts file
vagrant ...
161
votes
5
answers
51k
views
How do you stop Ansible from creating .retry files in the home directory?
When Ansible has problems running plays against a host, it will output the name of the host into a file in the user's home directory ending in '.retry'. These are often not used and just cause clutter,...
158
votes
7
answers
303k
views
How to set Linux environment variables with Ansible
Hi I am trying to find out how to set environment variable with Ansible.
something that a simple shell command like this:
EXPORT LC_ALL=C
tried as shell command and got an error
tried using the ...
156
votes
12
answers
218k
views
Override hosts variable of Ansible playbook from the command line
This is a fragment of a playbook that I'm using (server.yml):
- name: Determine Remote User
hosts: web
gather_facts: false
roles:
- { role: remote-user, tags: [remote-user, always] }
My ...
156
votes
3
answers
354k
views
Specifying ssh key in ansible playbook file
Ansible playbook can specify the key used for ssh connection using --key-file on the command line.
ansible-playbook -i hosts playbook.yml --key-file "~/.ssh/mykey.pem"
Is it possible to specify the ...
153
votes
10
answers
166k
views
How to create an empty file with Ansible?
What is the easiest way to create an empty file using Ansible? I know I can save an empty file into the files directory and then copy it to the remote host, but I find that somewhat unsatisfactory.
...
149
votes
16
answers
261k
views
Copy multiple files with Ansible
How can I copy more than a single file into remote nodes by Ansible in a task?
I've tried to duplicate the copy module line in my task to define files but it only copies the first file.
146
votes
4
answers
273k
views
Write variable to a file in Ansible
I am pulling JSON via the URI module and want to write the received content out to a file. I am able to get the content and output it to the debugger so I know the content has been received, but I do ...
141
votes
6
answers
90k
views
Ansible Playbooks vs Roles
According to the Ansible docs, a Playbook
is:
...the basis for a really simple configuration management and multi-machine deployment system, unlike any that already exist, and one that is very well ...
138
votes
10
answers
168k
views
Ansible: Can I execute role from command line?
Suppose I have a role called "apache"
Now I want to execute that role on host 192.168.0.10 from the command line from Ansible host
ansible-playbook -i "192.168.0.10" --role "path to role"
Is ...
134
votes
25
answers
332k
views
Creating a new user and password with Ansible
I have an ansible task which creates a new user on ubuntu 12.04;
- name: Add deployment user
action: user name=deployer password=mypassword
it completes as expected but when I login as that user ...
133
votes
6
answers
334k
views
Ansible: Store command's stdout in new variable?
Inside my playbook I'd like to create a variable holding the output of an external command. Afterwards I want to make use of that variable in a couple of templates.
Here are the relevant parts of ...
133
votes
4
answers
216k
views
Ansible: Set variable to file content
I'm using the ec2 module with ansible-playbook I want to set a variable to the contents of a file. Here's how I'm currently doing it.
Var with the filename
shell task to cat the file
use the result ...
131
votes
3
answers
183k
views
Run task only if host does not belong to a group
I'd like to able to run an ansible task only if the host of the current playbook does not belong to a certain group. In semi pseudo code:
- name: my command
command: echo stuff
when: "if {{ ...
126
votes
9
answers
218k
views
How can I copy files between two managed nodes using Ansible?
I need to copy a file between two remote nodes:
node A is a managed node where the file exists
node B is a managed node where the file should be copied
Please note that my control node, from where I ...
124
votes
7
answers
149k
views
How do I get a variable with the name of the user running ansible?
I'm scripting a deployment process that takes the name of the user running the ansible script (e.g. tlau) and creates a deployment directory on the remote system based on that username and the current ...
123
votes
8
answers
168k
views
How to use template module with different set of variables?
My use case is the following :
I have a template file, and I would like to create 2 different files from that template, with the variables being filled by a different set of variables for each file.
...
120
votes
10
answers
89k
views
Escaping double curly braces in Ansible
How to escape double curly braces in Ansible 1.9.2?
For instance, how can I escape double curly braces in the following shell command?
- name: Test
shell: "docker inspect --format '{{ ....
115
votes
15
answers
257k
views
Missing sudo password in Ansible
Ansible asks for sudo password from following code, it tries to create a new postgres user.
Error message:
fatal: [xxx.xxx.xxx.xxx] => Missing sudo password
main.yml
- name: 'Provision a ...
114
votes
2
answers
127k
views
How to copy files with ansible relatively to the role?
I have a copy task inside a role and I was expecting that the src location would be relative to the role itself, not the playbook that calls the roles.
How do I make this work and use the files from ...
114
votes
7
answers
283k
views
How do I get logs/details of ansible-playbook module executions?
Say I execute the following.
$ cat test.sh
#!/bin/bash
echo Hello World
exit 0
$ cat Hello.yml
---
- hosts: MyTestHost
tasks:
- name: Hello yourself
script: test.sh
$ ansible-playbook ...
113
votes
8
answers
236k
views
error retrieving current directory: getcwd: cannot access parent directories
I have a simple script:
#!/bin/bash
for server in $(~/.ansible/ansible_hosts)
do
ssh $server "hostname; readlink /opt/mydir/mylink;"
done
It works fine - the program returns the correct hostname ...
112
votes
6
answers
298k
views
ansible : how to pass multiple commands
I tried this:
- command: ./configure chdir=/src/package/
- command: /usr/bin/make chdir=/src/package/
- command: /usr/bin/make install chdir=/src/package/
which works, but I was hoping for ...
112
votes
3
answers
166k
views
Ansible: filter a list by its attributes
I have variable named "network" registered in Ansible:
{
"addresses": {
"private_ext": [
{
"type": &...
111
votes
6
answers
160k
views
How to run Ansible without specifying the inventory but the host directly?
I want to run Ansible in Python without specifying the inventory file through (ANSIBLE_HOST) but just by:
ansible.run.Runner(
module_name='ping',
host='www.google.com'
)
I can actually do this ...
107
votes
10
answers
341k
views
Run an Ansible task only when the variable contains a specific string
I have multiple tasks depend from the value of variable1. I want to check if the value is in {{ variable1 }} but I get an error:
- name: do something when the value in variable1
command: <command&...
106
votes
10
answers
99k
views
Not possible to source .bashrc with Ansible
I can SSH to the remote host and do a source /home/username/.bashrc — everything works fine.
However if I do:
- name: source bashrc
action: command source /home/username/.bashrc
I get:
failed: [...