Skip to main content

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.

Filter by
Sorted by
Tagged with
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?
Gaurav Agarwal's user avatar
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 ...
Slava Fomin II's user avatar
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 ...
Ross's user avatar
  • 46.8k
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="...
Édouard Lopez's user avatar
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 ...
joemaller's user avatar
  • 20.2k
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 ...
Christian Berendt's user avatar
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 ...
Billz's user avatar
  • 8,017
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 ***************************...
Johan's user avatar
  • 39.5k
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: ...
Abbas's user avatar
  • 3,224
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 ...
nwinkler's user avatar
  • 53.8k
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 ...
Michael's user avatar
  • 8,627
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 /...
E Dine Sh's user avatar
  • 2,117
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] ****************************************************************** ...
jdavis's user avatar
  • 1,987
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'] }...
Subtubes's user avatar
  • 16.6k
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 ...
rgrinberg's user avatar
  • 9,768
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 ...
neves's user avatar
  • 37.5k
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. ...
Tanner Semerad's user avatar
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 ...
Bernardo Vale's user avatar
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 '{...
sakhunzai's user avatar
  • 14.4k
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....
user1994660's user avatar
  • 5,473
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 ...
SJC's user avatar
  • 2,877
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 ...
Josh Unger's user avatar
  • 7,024
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 ...
pdeva's user avatar
  • 44.8k
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 ...
mbdev's user avatar
  • 6,493
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,...
Asfand Qazi's user avatar
  • 6,765
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 ...
Gleeb's user avatar
  • 11.1k
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 ...
luqo33's user avatar
  • 8,251
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 ...
Brian's user avatar
  • 13.2k
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. ...
dokaspar's user avatar
  • 8,476
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.
Mark K.'s user avatar
  • 2,039
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 ...
Keith Adler's user avatar
  • 21.1k
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 ...
smeeb's user avatar
  • 28.9k
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 ...
Karl's user avatar
  • 3,053
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 ...
raphael_turtle's user avatar
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 ...
Forivin's user avatar
  • 15.2k
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 ...
TesterJeff's user avatar
  • 1,660
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 {{ ...
rgrinberg's user avatar
  • 9,768
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 ...
user3228188's user avatar
  • 1,701
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 ...
Tessa Lau's user avatar
  • 1,609
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. ...
Kestemont Max's user avatar
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 '{{ ....
Davide Guerri's user avatar
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 ...
user469652's user avatar
  • 50.5k
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 ...
sorin's user avatar
  • 167k
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 ...
Kashyap's user avatar
  • 16.4k
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 ...
Todd Ellner's user avatar
  • 1,131
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 ...
John Doe's user avatar
  • 1,619
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": &...
Guido's user avatar
  • 47.4k
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 ...
Ngoc Tran's user avatar
  • 1,411
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&...
mndhr's user avatar
  • 1,459
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: [...
pldimitrov's user avatar
  • 1,687

1
2 3 4 5
458